1 #![cfg_attr(feature = "used_linker", feature(used_with_arg))] 2 #![allow(unknown_lints, non_local_definitions)] // FIXME 3 #![deny(rust_2024_compatibility, unsafe_op_in_unsafe_fn)] 4 5 use linkme::distributed_slice; 6 use once_cell::sync::Lazy; 7 8 #[distributed_slice] 9 static SHENANIGANS: [i32]; 10 11 #[distributed_slice(SHENANIGANS)] 12 static N: i32 = 9; 13 14 #[distributed_slice(SHENANIGANS)] 15 static NN: i32 = 99; 16 17 #[distributed_slice(SHENANIGANS)] 18 static NNN: i32 = 999; 19 20 #[test] test()21fn test() { 22 assert_eq!(SHENANIGANS.len(), 3); 23 24 let mut sum = 0; 25 for n in SHENANIGANS { 26 sum += n; 27 } 28 29 assert_eq!(sum, 9 + 99 + 999); 30 } 31 32 #[test] test_empty()33fn test_empty() { 34 #[distributed_slice] 35 static EMPTY: [i32]; 36 37 assert!(EMPTY.is_empty()); 38 } 39 40 #[test] test_non_copy()41fn test_non_copy() { 42 pub struct NonCopy(#[allow(dead_code)] pub i32); 43 44 #[distributed_slice] 45 static NONCOPY: [NonCopy]; 46 47 #[distributed_slice(NONCOPY)] 48 static ELEMENT: NonCopy = NonCopy(9); 49 50 assert!(!NONCOPY.is_empty()); 51 } 52 53 #[test] test_interior_mutable()54fn test_interior_mutable() { 55 #[distributed_slice] 56 static MUTABLE: [Lazy<i32>]; 57 58 #[distributed_slice(MUTABLE)] 59 static ELEMENT: Lazy<i32> = Lazy::new(|| -1); 60 61 assert!(MUTABLE.len() == 1); 62 assert!(*MUTABLE[0] == -1); 63 } 64 65 #[test] test_elided_lifetime()66fn test_elided_lifetime() { 67 #[distributed_slice] 68 pub static MYSLICE: [&str]; 69 70 #[distributed_slice(MYSLICE)] 71 static ELEMENT: &str = "..."; 72 73 assert!(!MYSLICE.is_empty()); 74 assert_eq!(MYSLICE[0], "..."); 75 } 76 77 #[test] test_legacy_syntax()78fn test_legacy_syntax() { 79 // Rustc older than 1.43 requires an initializer expression. 80 #[distributed_slice] 81 pub static LEGACY: [&str] = [..]; 82 } 83