Skip to content

Commit 96ca40f

Browse files
committed
Exposed generic functions to create the Context
1 parent 49f0cc1 commit 96ca40f

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

Diff for: src/context.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use Secp256k1;
77
#[cfg(feature = "std")]
88
pub use self::std_only::*;
99

10-
/// A trait for all kinds of Context's that let's you define the exact flags and a function to deallocate memory.
10+
/// A trait for all kinds of Context's that Lets you define the exact flags and a function to deallocate memory.
1111
/// * DO NOT * implement it for your own types.
1212
pub unsafe trait Context {
1313
/// Flags for the ffi.
@@ -86,7 +86,8 @@ mod std_only {
8686
}
8787

8888
impl<C: Context> Secp256k1<C> {
89-
fn gen_new() -> Secp256k1<C> {
89+
/// Lets you create a context in a generic manner(sign/verify/all)
90+
pub fn gen_new() -> Secp256k1<C> {
9091
let buf = vec![0u8; Self::preallocate_size_gen()].into_boxed_slice();
9192
let ptr = Box::into_raw(buf);
9293
Secp256k1 {
@@ -148,31 +149,32 @@ unsafe impl<'buf> Context for SignOnlyPreallocated<'buf> {
148149
const FLAGS: c_uint = ffi::SECP256K1_START_SIGN;
149150
const DESCRIPTION: &'static str = "signing only";
150151

151-
fn deallocate(ptr: *mut [u8]) {
152-
let _ = ptr;
152+
fn deallocate(_ptr: *mut [u8]) {
153+
// Allocated by the user
153154
}
154155
}
155156

156157
unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
157158
const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY;
158159
const DESCRIPTION: &'static str = "verification only";
159160

160-
fn deallocate(ptr: *mut [u8]) {
161-
let _ = ptr;
161+
fn deallocate(_ptr: *mut [u8]) {
162+
// Allocated by the user
162163
}
163164
}
164165

165166
unsafe impl<'buf> Context for AllPreallocated<'buf> {
166167
const FLAGS: c_uint = SignOnlyPreallocated::FLAGS | VerifyOnlyPreallocated::FLAGS;
167168
const DESCRIPTION: &'static str = "all capabilities";
168169

169-
fn deallocate(ptr: *mut [u8]) {
170-
let _ = ptr;
170+
fn deallocate(_ptr: *mut [u8]) {
171+
// Allocated by the user
171172
}
172173
}
173174

174175
impl<'buf, C: Context + 'buf> Secp256k1<C> {
175-
fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
176+
/// Lets you create a context with preallocated buffer in a generic manner(sign/verify/all)
177+
pub fn preallocated_gen_new(buf: &'buf mut [u8]) -> Result<Secp256k1<C>, Error> {
176178
if buf.len() < Self::preallocate_size_gen() {
177179
return Err(Error::NotEnoughMemory);
178180
}

Diff for: src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,8 @@ impl<C: Context> Secp256k1<C> {
570570
&self.ctx
571571
}
572572

573-
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context
574-
pub(crate) fn preallocate_size_gen() -> usize {
573+
/// Returns the required memory for a preallocated context buffer in a generic manner(sign/verify/all)
574+
pub fn preallocate_size_gen() -> usize {
575575
unsafe { ffi::secp256k1_context_preallocated_size(C::FLAGS) }
576576
}
577577

0 commit comments

Comments
 (0)