Skip to content

Commit 6a89320

Browse files
committed
Merge #345: Add a static immutable zero aligned type
5e6d0f1 Switch to associated constant (Jonathan Underwood) 9cf552e Add a static immutable zero aligned type (junderw) Pull request description: The `zeroed` fn can not be used in static assignments. In environments where it is no_std and no allocator are present, the only way to get a slice of AlignedTypes is dynamically, so `preallocated_gen_new` can't be used. By offering this as a static, it can be used in static assignments as such: ```rust #[cfg(target_pointer_width = "32")] static mut CONTEXT_BUFFER: [AlignedType; 69645] = [ZERO_ALIGNED; 69645]; #[cfg(target_pointer_width = "64")] static mut CONTEXT_BUFFER: [AlignedType; 69646] = [ZERO_ALIGNED; 69646]; static mut SECP256K1: Option<Secp256k1<AllPreallocated>> = None; pub fn get_context(seed: Option<&[u8; 32]>) -> &'static Secp256k1<AllPreallocated<'static>> { unsafe { if SECP256K1.is_none() { SECP256K1 = Some( Secp256k1::preallocated_gen_new(&mut CONTEXT_BUFFER) .expect("CONTEXT_BUFFER size is wrong"), ); } if let Some(seed) = seed { SECP256K1.as_mut().unwrap().seeded_randomize(seed); } SECP256K1.as_ref().unwrap() } } ``` ACKs for top commit: apoelstra: ACK 5e6d0f1 Tree-SHA512: fc800f8c5c637fc7f81312da17f0a96d17cd087a2e6876f4dedbefffbe92b3625deb93636265f334f9fbd7ac38baa529d4ec72857dae662e26d753f32f91d394
2 parents 50034cc + 5e6d0f1 commit 6a89320

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

secp256k1-sys/src/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ impl AlignedType {
4040
pub fn zeroed() -> Self {
4141
AlignedType([0u8; 16])
4242
}
43+
44+
/// A static zeroed out AlignedType for use in static assignments of [AlignedType; _]
45+
const ZERO: AlignedType = AlignedType([0u8; 16]);
4346
}
4447

4548
#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))]

0 commit comments

Comments
 (0)