1
1
// SPDX-License-Identifier: CC0-1.0
2
2
3
+ use core:: fmt;
3
4
use core:: marker:: PhantomData ;
4
5
use core:: mem:: ManuallyDrop ;
5
6
use core:: ptr:: NonNull ;
@@ -8,7 +9,7 @@ use core::ptr::NonNull;
8
9
pub use self :: alloc_only:: * ;
9
10
use crate :: ffi:: types:: { c_uint, c_void, AlignedType } ;
10
11
use crate :: ffi:: { self , CPtr } ;
11
- use crate :: { Error , Secp256k1 } ;
12
+ use crate :: Secp256k1 ;
12
13
13
14
#[ cfg( all( feature = "global-context" , feature = "std" ) ) ]
14
15
/// Module implementing a singleton pattern for a global `Secp256k1` context.
@@ -320,14 +321,33 @@ unsafe impl<'buf> PreallocatedContext<'buf> for AllPreallocated<'buf> {}
320
321
unsafe impl < ' buf > PreallocatedContext < ' buf > for SignOnlyPreallocated < ' buf > { }
321
322
unsafe impl < ' buf > PreallocatedContext < ' buf > for VerifyOnlyPreallocated < ' buf > { }
322
323
324
+ /// Not enough preallocated memory for the requested buffer size.
325
+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
326
+ #[ non_exhaustive]
327
+ #[ allow( missing_copy_implementations) ] // Don't implement Copy when we use non_exhaustive.
328
+ pub struct NotEnoughMemoryError ;
329
+
330
+ impl fmt:: Display for NotEnoughMemoryError {
331
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
332
+ f. write_str ( "not enough preallocated memory for the requested buffer size" )
333
+ }
334
+ }
335
+
336
+ #[ cfg( feature = "std" ) ]
337
+ impl std:: error:: Error for NotEnoughMemoryError {
338
+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > { None }
339
+ }
340
+
323
341
impl < ' buf , C : Context + PreallocatedContext < ' buf > > Secp256k1 < C > {
324
342
/// Lets you create a context with a preallocated buffer in a generic manner (sign/verify/all).
325
- pub fn preallocated_gen_new ( buf : & ' buf mut [ AlignedType ] ) -> Result < Secp256k1 < C > , Error > {
343
+ pub fn preallocated_gen_new (
344
+ buf : & ' buf mut [ AlignedType ] ,
345
+ ) -> Result < Secp256k1 < C > , NotEnoughMemoryError > {
326
346
#[ cfg( target_arch = "wasm32" ) ]
327
347
ffi:: types:: sanity_checks_for_wasm ( ) ;
328
348
329
349
if buf. len ( ) < Self :: preallocate_size_gen ( ) {
330
- return Err ( Error :: NotEnoughMemory ) ;
350
+ return Err ( NotEnoughMemoryError ) ;
331
351
}
332
352
// Safe because buf is not null since it is not empty.
333
353
let buf = unsafe { NonNull :: new_unchecked ( buf. as_mut_c_ptr ( ) as * mut c_void ) } ;
@@ -343,7 +363,7 @@ impl<'buf> Secp256k1<AllPreallocated<'buf>> {
343
363
/// Creates a new Secp256k1 context with all capabilities.
344
364
pub fn preallocated_new (
345
365
buf : & ' buf mut [ AlignedType ] ,
346
- ) -> Result < Secp256k1 < AllPreallocated < ' buf > > , Error > {
366
+ ) -> Result < Secp256k1 < AllPreallocated < ' buf > > , NotEnoughMemoryError > {
347
367
Secp256k1 :: preallocated_gen_new ( buf)
348
368
}
349
369
/// Uses the ffi `secp256k1_context_preallocated_size` to check the memory size needed for a context.
@@ -378,7 +398,7 @@ impl<'buf> Secp256k1<SignOnlyPreallocated<'buf>> {
378
398
/// Creates a new Secp256k1 context that can only be used for signing.
379
399
pub fn preallocated_signing_only (
380
400
buf : & ' buf mut [ AlignedType ] ,
381
- ) -> Result < Secp256k1 < SignOnlyPreallocated < ' buf > > , Error > {
401
+ ) -> Result < Secp256k1 < SignOnlyPreallocated < ' buf > > , NotEnoughMemoryError > {
382
402
Secp256k1 :: preallocated_gen_new ( buf)
383
403
}
384
404
@@ -402,7 +422,7 @@ impl<'buf> Secp256k1<VerifyOnlyPreallocated<'buf>> {
402
422
/// Creates a new Secp256k1 context that can only be used for verification
403
423
pub fn preallocated_verification_only (
404
424
buf : & ' buf mut [ AlignedType ] ,
405
- ) -> Result < Secp256k1 < VerifyOnlyPreallocated < ' buf > > , Error > {
425
+ ) -> Result < Secp256k1 < VerifyOnlyPreallocated < ' buf > > , NotEnoughMemoryError > {
406
426
Secp256k1 :: preallocated_gen_new ( buf)
407
427
}
408
428
0 commit comments