Skip to content

Commit 9522f7e

Browse files
committed
Make Context::deallocate unsafe fn
1 parent fe688ad commit 9522f7e

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/context.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub unsafe trait Context : private::Sealed {
1515
/// A constant description of the context.
1616
const DESCRIPTION: &'static str;
1717
/// A function to deallocate the memory when the context is dropped.
18-
fn deallocate(ptr: *mut [u8]);
18+
unsafe fn deallocate(ptr: *mut [u8]);
1919
}
2020

2121
/// Marker trait for indicating that an instance of `Secp256k1` can be used for signing.
@@ -78,26 +78,26 @@ mod std_only {
7878
const FLAGS: c_uint = ffi::SECP256K1_START_SIGN;
7979
const DESCRIPTION: &'static str = "signing only";
8080

81-
fn deallocate(ptr: *mut [u8]) {
82-
let _ = unsafe { Box::from_raw(ptr) };
81+
unsafe fn deallocate(ptr: *mut [u8]) {
82+
let _ = Box::from_raw(ptr);
8383
}
8484
}
8585

8686
unsafe impl Context for VerifyOnly {
8787
const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY;
8888
const DESCRIPTION: &'static str = "verification only";
8989

90-
fn deallocate(ptr: *mut [u8]) {
91-
let _ = unsafe { Box::from_raw(ptr) };
90+
unsafe fn deallocate(ptr: *mut [u8]) {
91+
let _ = Box::from_raw(ptr);
9292
}
9393
}
9494

9595
unsafe impl Context for All {
9696
const FLAGS: c_uint = VerifyOnly::FLAGS | SignOnly::FLAGS;
9797
const DESCRIPTION: &'static str = "all capabilities";
9898

99-
fn deallocate(ptr: *mut [u8]) {
100-
let _ = unsafe { Box::from_raw(ptr) };
99+
unsafe fn deallocate(ptr: *mut [u8]) {
100+
let _ = Box::from_raw(ptr);
101101
}
102102
}
103103

@@ -152,7 +152,6 @@ mod std_only {
152152
}
153153
}
154154
}
155-
156155
}
157156

158157
impl<'buf> Signing for SignOnlyPreallocated<'buf> {}
@@ -165,7 +164,7 @@ unsafe impl<'buf> Context for SignOnlyPreallocated<'buf> {
165164
const FLAGS: c_uint = ffi::SECP256K1_START_SIGN;
166165
const DESCRIPTION: &'static str = "signing only";
167166

168-
fn deallocate(_ptr: *mut [u8]) {
167+
unsafe fn deallocate(_ptr: *mut [u8]) {
169168
// Allocated by the user
170169
}
171170
}
@@ -174,7 +173,7 @@ unsafe impl<'buf> Context for VerifyOnlyPreallocated<'buf> {
174173
const FLAGS: c_uint = ffi::SECP256K1_START_VERIFY;
175174
const DESCRIPTION: &'static str = "verification only";
176175

177-
fn deallocate(_ptr: *mut [u8]) {
176+
unsafe fn deallocate(_ptr: *mut [u8]) {
178177
// Allocated by the user
179178
}
180179
}
@@ -183,7 +182,7 @@ unsafe impl<'buf> Context for AllPreallocated<'buf> {
183182
const FLAGS: c_uint = SignOnlyPreallocated::FLAGS | VerifyOnlyPreallocated::FLAGS;
184183
const DESCRIPTION: &'static str = "all capabilities";
185184

186-
fn deallocate(_ptr: *mut [u8]) {
185+
unsafe fn deallocate(_ptr: *mut [u8]) {
187186
// Allocated by the user
188187
}
189188
}

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,10 @@ impl<C: Context> Eq for Secp256k1<C> { }
571571

572572
impl<C: Context> Drop for Secp256k1<C> {
573573
fn drop(&mut self) {
574-
unsafe { ffi::secp256k1_context_preallocated_destroy(self.ctx) };
575-
C::deallocate(self.buf);
574+
unsafe {
575+
ffi::secp256k1_context_preallocated_destroy(self.ctx);
576+
C::deallocate(self.buf);
577+
}
576578
}
577579
}
578580

0 commit comments

Comments
 (0)