Debian Codegraph @

debian/rust-vendor/link-section/examples/empty.rs

raw
1 //! Regression test: sections with no `#[in_section]` items must still link.
2 //!
3 //!
4 //! Each section kind that routes through `get_section!` is exercised empty.
5 #![warn(missing_docs)]
6 #![allow(unsafe_code)]
7
8 use link_section::{
9 section, Ref, TypedMovableSection, TypedMutableSection, TypedReferenceSection, TypedSection,
10 };
11
12 /// An empty typed section.
13 #[section(typed)]
14 pub static EMPTY_TYPED: TypedSection<u32>;
15
16 /// An empty mutable section.
17 #[section(mutable)]
18 pub static EMPTY_MUTABLE: TypedMutableSection<u32>;
19
20 /// An empty movable section (exercises the value *and* backref symbols).
21 #[section(movable)]
22 pub static EMPTY_MOVABLE: TypedMovableSection<u32>;
23
24 /// An empty reference section.
25 #[section(reference)]
26 pub static EMPTY_REFERENCE: TypedReferenceSection<Ref<u32>>;
27
28 /// Asserts every empty section links and reports zero items.
29 pub fn main() {
30 assert!(EMPTY_TYPED.is_empty(), "typed");
31 assert_eq!(EMPTY_TYPED.as_slice(), &[] as &[u32]);
32
33 assert!(EMPTY_MUTABLE.is_empty(), "mutable");
34
35 assert!(EMPTY_MOVABLE.is_empty(), "movable");
36 // Sorting an empty movable section must also be a no-op rather than touching
37 // the (null) backref bounds.
38 unsafe { EMPTY_MOVABLE.sort_unstable() };
39 assert!(EMPTY_MOVABLE.is_empty(), "movable after sort");
40
41 assert!(EMPTY_REFERENCE.is_empty(), "reference");
42
43 eprintln!("OK: all empty sections linked and report empty");
44 }