| 1 |
//! Reference-section example for `link-section`. |
| 2 |
#![warn(missing_docs)] |
| 3 |
|
| 4 |
use link_section::section; |
| 5 |
|
| 6 |
/// Operations. |
| 7 |
#[section(movable, unsafe, name = OPERATIONS)] |
| 8 |
static OPERATIONS: link_section::TypedMovableSection<Operation>; |
| 9 |
|
| 10 |
#[derive(Debug, PartialEq, Eq, Ord, PartialOrd)] |
| 11 |
struct Operation(u32); |
| 12 |
|
| 13 |
pub(crate) mod operations { |
| 14 |
use super::Operation; |
| 15 |
use link_section::in_section; |
| 16 |
|
| 17 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 18 |
pub static OPERATION_A: Operation = Operation(1); |
| 19 |
|
| 20 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 21 |
pub static OPERATION_B: Operation = Operation(3); |
| 22 |
|
| 23 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 24 |
pub static OPERATION_C: Operation = Operation(6); |
| 25 |
|
| 26 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 27 |
pub static OPERATION_D: Operation = Operation(10); |
| 28 |
|
| 29 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 30 |
pub static OPERATION_E: Operation = Operation(8); |
| 31 |
|
| 32 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 33 |
pub static OPERATION_F: Operation = Operation(2); |
| 34 |
|
| 35 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 36 |
pub static OPERATION_G: Operation = Operation(4); |
| 37 |
|
| 38 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 39 |
pub static OPERATION_H: Operation = Operation(5); |
| 40 |
|
| 41 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 42 |
pub static OPERATION_I: Operation = Operation(9); |
| 43 |
|
| 44 |
#[in_section(unsafe, name = OPERATIONS, type = movable)] |
| 45 |
pub static OPERATION_J: Operation = Operation(7); |
| 46 |
} |
| 47 |
|
| 48 |
#[allow(unsafe_code)] |
| 49 |
fn sort_operations() { |
| 50 |
unsafe { OPERATIONS.sort_unstable() }; |
| 51 |
} |
| 52 |
|
| 53 |
fn main() { |
| 54 |
// This should normally be done in a `ctor`, but for this example we know |
| 55 |
// there are no other live threads and we do it here. |
| 56 |
sort_operations(); |
| 57 |
|
| 58 |
for op in OPERATIONS { |
| 59 |
println!("Operation: {op:?}"); |
| 60 |
} |
| 61 |
|
| 62 |
println!("OPERATION_A: {:?}", *operations::OPERATION_A); |
| 63 |
println!("OPERATION_B: {:?}", *operations::OPERATION_B); |
| 64 |
println!("OPERATION_C: {:?}", *operations::OPERATION_C); |
| 65 |
println!("OPERATION_D: {:?}", *operations::OPERATION_D); |
| 66 |
println!("OPERATION_E: {:?}", *operations::OPERATION_E); |
| 67 |
println!("OPERATION_F: {:?}", *operations::OPERATION_F); |
| 68 |
println!("OPERATION_G: {:?}", *operations::OPERATION_G); |
| 69 |
println!("OPERATION_H: {:?}", *operations::OPERATION_H); |
| 70 |
println!("OPERATION_I: {:?}", *operations::OPERATION_I); |
| 71 |
println!("OPERATION_J: {:?}", *operations::OPERATION_J); |
| 72 |
} |