Skip to content

Commit fe688ad

Browse files
committed
Make the Context trait unimplementable
1 parent 5de62f8 commit fe688ad

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/context.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use Secp256k1;
88
pub use self::std_only::*;
99

1010
/// A trait for all kinds of Context's that Lets you define the exact flags and a function to deallocate memory.
11-
/// * DO NOT * implement it for your own types.
12-
pub unsafe trait Context {
11+
/// It shouldn't be possible to implement this for types outside this crate.
12+
pub unsafe trait Context : private::Sealed {
1313
/// Flags for the ffi.
1414
const FLAGS: c_uint;
1515
/// A constant description of the context.
@@ -39,8 +39,24 @@ pub struct AllPreallocated<'buf> {
3939
phantom: PhantomData<&'buf ()>,
4040
}
4141

42+
mod private {
43+
use super::*;
44+
// A trick to prevent users from implementing a trait.
45+
// on one hand this trait is public, on the other it's in a private module
46+
// so it's not visible to anyone besides it's parent (the context module)
47+
pub trait Sealed {}
48+
49+
impl<'buf> Sealed for AllPreallocated<'buf> {}
50+
impl<'buf> Sealed for VerifyOnlyPreallocated<'buf> {}
51+
impl<'buf> Sealed for SignOnlyPreallocated<'buf> {}
52+
}
53+
4254
#[cfg(feature = "std")]
4355
mod std_only {
56+
impl private::Sealed for SignOnly {}
57+
impl private::Sealed for All {}
58+
impl private::Sealed for VerifyOnly {}
59+
4460
use super::*;
4561

4662
/// Represents the set of capabilities needed for signing.

0 commit comments

Comments
 (0)