From 96ca40faedfe2c2de988f65873a9baaf9eb7d415 Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Thu, 4 Jul 2019 21:18:36 -0400 Subject: [PATCH] Exposed generic functions to create the Context --- src/context.rs | 20 +++++++++++--------- src/lib.rs | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/context.rs b/src/context.rs index aa35d1e..5dec624 100644 --- a/src/context.rs +++ b/src/context.rs @@ -7,7 +7,7 @@ use Secp256k1; #[cfg(feature = "std")] pub use self::std_only::*; -/// A trait for all kinds of Context's that let's you define the exact flags and a function to deallocate memory. +/// A trait for all kinds of Context's that Lets you define the exact flags and a function to deallocate memory. /// * DO NOT * implement it for your own types. pub unsafe trait Context { /// Flags for the ffi. @@ -86,7 +86,8 @@ mod std_only { } impl Secp256k1 { - fn gen_new() -> Secp256k1 { + /// Lets you create a context in a generic manner(sign/verify/all) + pub fn gen_new() -> Secp256k1 { let buf = vec![0u8; Self::preallocate_size_gen()].into_boxed_slice(); let ptr = Box::into_raw(buf); Secp256k1 { @@ -148,8 +149,8 @@ unsafe impl<'buf> Context for SignOnlyPreallocated<'buf> { const FLAGS: c_uint = ffi::SECP256K1_START_SIGN; const DESCRIPTION: &'static str = "signing only"; - fn deallocate(ptr: *mut [u8]) { - let _ = ptr; + fn deallocate(_ptr: *mut [u8]) { + // Allocated by the user } } @@ -157,8 +158,8 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> { const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY; const DESCRIPTION: &'static str = "verification only"; - fn deallocate(ptr: *mut [u8]) { - let _ = ptr; + fn deallocate(_ptr: *mut [u8]) { + // Allocated by the user } } @@ -166,13 +167,14 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> { const FLAGS: c_uint = SignOnlyPreallocated::FLAGS | VerifyOnlyPreallocated::FLAGS; const DESCRIPTION: &'static str = "all capabilities"; - fn deallocate(ptr: *mut [u8]) { - let _ = ptr; + fn deallocate(_ptr: *mut [u8]) { + // Allocated by the user } } impl<'buf, C: Context + 'buf> Secp256k1 { - fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result, Error> { + /// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all) + pub fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result, Error> { if buf.len() < Self::preallocate_size_gen() { return Err(Error::NotEnoughMemory); } diff --git a/src/lib.rs b/src/lib.rs index d82e359..6651bc2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -570,8 +570,8 @@ impl Secp256k1 { &self.ctx } - /// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context - pub(crate) fn preallocate_size_gen() -> usize { + /// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all) + pub fn preallocate_size_gen() -> usize { unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) } }