From 7d3a149ca5064147229db147359638cbcb54acdd Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Tue, 14 Sep 2021 17:40:16 +0300 Subject: [PATCH] Move more things from the std feature to the alloc feature --- Cargo.toml | 6 +++--- secp256k1-sys/Cargo.toml | 3 ++- secp256k1-sys/src/lib.rs | 23 +++++++++++++---------- secp256k1-sys/src/types.rs | 2 +- src/context.rs | 13 +++++-------- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 83a44b5..99b7d26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,10 +20,10 @@ rustdoc-args = ["--cfg", "docsrs"] [features] unstable = ["recovery", "rand-std"] default = ["std"] -std = ["secp256k1-sys/std"] +std = ["alloc", "secp256k1-sys/std"] # allow use of Secp256k1::new and related API that requires an allocator -alloc = [] -bitcoin-hashes-std = ["bitcoin_hashes/std"] + alloc = ["secp256k1-sys/alloc"] + bitcoin-hashes-std = ["bitcoin_hashes/std"] rand-std = ["rand/std"] recovery = ["secp256k1-sys/recovery"] lowmemory = ["secp256k1-sys/lowmemory"] diff --git a/secp256k1-sys/Cargo.toml b/secp256k1-sys/Cargo.toml index 8993a16..f5d1b41 100644 --- a/secp256k1-sys/Cargo.toml +++ b/secp256k1-sys/Cargo.toml @@ -30,5 +30,6 @@ libc = "0.2" default = ["std"] recovery = [] lowmemory = [] -std = [] +std = ["alloc"] +alloc = [] diff --git a/secp256k1-sys/src/lib.rs b/secp256k1-sys/src/lib.rs index d560ea6..b700a35 100644 --- a/secp256k1-sys/src/lib.rs +++ b/secp256k1-sys/src/lib.rs @@ -25,6 +25,9 @@ #[cfg(any(test, feature = "std"))] extern crate core; +#[cfg(feature = "alloc")] +extern crate alloc; + #[cfg(fuzzing)] const THIS_UNUSED_CONSTANT_IS_YOUR_WARNING_THAT_ALL_THE_CRYPTO_IN_THIS_LIB_IS_DISABLED_FOR_FUZZING: usize = 0; @@ -540,11 +543,11 @@ extern "C" { /// /// The newly created secp256k1 raw context. #[no_mangle] -#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))] -#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))] +#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))] pub unsafe extern "C" fn rustsecp256k1_v0_5_0_context_create(flags: c_uint) -> *mut Context { use core::mem; - use std::alloc; + use crate::alloc::alloc; assert!(ALIGN_TO >= mem::align_of::()); assert!(ALIGN_TO >= mem::align_of::<&usize>()); assert!(ALIGN_TO >= mem::size_of::()); @@ -560,8 +563,8 @@ pub unsafe extern "C" fn rustsecp256k1_v0_5_0_context_create(flags: c_uint) -> * secp256k1_context_preallocated_create(ptr, flags) } -#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))] -#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))] +#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))] pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context { rustsecp256k1_v0_5_0_context_create(flags) } @@ -573,10 +576,10 @@ pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context { /// The pointer shouldn't be used after passing to this function, consider it as passing it to `free()`. /// #[no_mangle] -#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))] -#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))] +#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))] pub unsafe extern "C" fn rustsecp256k1_v0_5_0_context_destroy(ctx: *mut Context) { - use std::alloc; + use crate::alloc::alloc; secp256k1_context_preallocated_destroy(ctx); let ptr = (ctx as *mut u8).sub(ALIGN_TO); let bytes = (ptr as *mut usize).read(); @@ -584,8 +587,8 @@ pub unsafe extern "C" fn rustsecp256k1_v0_5_0_context_destroy(ctx: *mut Context) alloc::dealloc(ptr, layout); } -#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))] -#[cfg_attr(docsrs, doc(cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))))] +#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))))] pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) { rustsecp256k1_v0_5_0_context_destroy(ctx) } diff --git a/secp256k1-sys/src/types.rs b/secp256k1-sys/src/types.rs index 71467e8..e457ec4 100644 --- a/secp256k1-sys/src/types.rs +++ b/secp256k1-sys/src/types.rs @@ -28,7 +28,7 @@ impl AlignedType { pub const ZERO: AlignedType = AlignedType([0u8; 16]); } -#[cfg(all(feature = "std", not(rust_secp_no_symbol_renaming)))] +#[cfg(all(feature = "alloc", not(rust_secp_no_symbol_renaming)))] pub(crate) const ALIGN_TO: usize = core::mem::align_of::(); #[cfg(test)] diff --git a/src/context.rs b/src/context.rs index e6157de..b5b5525 100644 --- a/src/context.rs +++ b/src/context.rs @@ -5,8 +5,8 @@ use crate::{Error, Secp256k1}; use crate::ffi::{self, CPtr, types::AlignedType}; use crate::ffi::types::{c_uint, c_void}; -#[cfg(any(feature = "std", feature = "alloc"))] -#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] +#[cfg(feature = "alloc")] +#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] pub use self::alloc_only::*; #[cfg(all(feature = "global-context", feature = "std"))] @@ -103,13 +103,10 @@ mod private { impl<'buf> Sealed for SignOnlyPreallocated<'buf> {} } -#[cfg(any(feature = "std", feature = "alloc"))] -#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))] +#[cfg(feature = "alloc")] +#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc"))))] mod alloc_only { - #[cfg(not(feature = "std"))] - use alloc::alloc; - #[cfg(feature = "std")] - use std::alloc; + use crate::alloc::alloc; use core::marker::PhantomData;