Exposed generic functions to create the Context
This commit is contained in:
parent
49f0cc1c46
commit
96ca40faed
|
@ -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<C: Context> Secp256k1<C> {
|
||||
fn gen_new() -> Secp256k1<C> {
|
||||
/// Lets you create a context in a generic manner(sign/verify/all)
|
||||
pub fn gen_new() -> Secp256k1<C> {
|
||||
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<C> {
|
||||
fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, 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<Secp256k1<C>, Error> {
|
||||
if buf.len() < Self::preallocate_size_gen() {
|
||||
return Err(Error::NotEnoughMemory);
|
||||
}
|
||||
|
|
|
@ -570,8 +570,8 @@ impl<C: Context> Secp256k1<C> {
|
|||
&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) }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue