Add a generic Ref type
This commit is contained in:
parent
f229553158
commit
927c3e924c
|
|
@ -23,16 +23,7 @@ pub enum RNGProperty {
|
||||||
TwoMsbOne = 1,
|
TwoMsbOne = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A context object for `BigNum` operations.
|
type_!(BnCtx, ffi::BN_CTX, ffi::BN_CTX_free);
|
||||||
pub struct BnCtx(*mut ffi::BN_CTX);
|
|
||||||
|
|
||||||
impl Drop for BnCtx {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
unsafe {
|
|
||||||
ffi::BN_CTX_free(self.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl BnCtx {
|
impl BnCtx {
|
||||||
/// Returns a new `BnCtx`.
|
/// Returns a new `BnCtx`.
|
||||||
|
|
@ -40,10 +31,6 @@ impl BnCtx {
|
||||||
unsafe { cvt_p(ffi::BN_CTX_new()).map(BnCtx) }
|
unsafe { cvt_p(ffi::BN_CTX_new()).map(BnCtx) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_ptr(&self) -> *mut ffi::BN_CTX {
|
|
||||||
self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Places the result of `a * b` in `r`.
|
/// Places the result of `a * b` in `r`.
|
||||||
pub fn mul(&mut self,
|
pub fn mul(&mut self,
|
||||||
r: &mut BigNumRef,
|
r: &mut BigNumRef,
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,44 @@ use libc::c_int;
|
||||||
|
|
||||||
use error::ErrorStack;
|
use error::ErrorStack;
|
||||||
|
|
||||||
|
macro_rules! type_ {
|
||||||
|
($n:ident, $c:path, $d:path) => {
|
||||||
|
pub struct $n(*mut $c);
|
||||||
|
|
||||||
|
unsafe impl ::types::OpenSslType for $n {
|
||||||
|
type CType = $c;
|
||||||
|
|
||||||
|
unsafe fn from_ptr(ptr: *mut $c) -> $n {
|
||||||
|
$n(ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_ptr(&self) -> *mut $c {
|
||||||
|
self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for $n {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe { $d(self.0) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::std::ops::Deref for $n {
|
||||||
|
type Target = ::types::Ref<$n>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &::types::Ref<$n> {
|
||||||
|
unsafe { ::types::Ref::from_ptr(self.0) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ::std::ops::DerefMut for $n {
|
||||||
|
fn deref_mut(&mut self) -> &mut ::types::Ref<$n> {
|
||||||
|
unsafe { ::types::Ref::from_ptr_mut(self.0) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mod bio;
|
mod bio;
|
||||||
mod opaque;
|
mod opaque;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
@ -37,6 +75,7 @@ pub mod pkcs12;
|
||||||
pub mod pkcs5;
|
pub mod pkcs5;
|
||||||
pub mod pkey;
|
pub mod pkey;
|
||||||
pub mod rand;
|
pub mod rand;
|
||||||
|
pub mod types;
|
||||||
pub mod rsa;
|
pub mod rsa;
|
||||||
pub mod sign;
|
pub mod sign;
|
||||||
pub mod ssl;
|
pub mod ssl;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use opaque::Opaque;
|
||||||
|
|
||||||
|
pub unsafe trait OpenSslType {
|
||||||
|
type CType;
|
||||||
|
|
||||||
|
unsafe fn from_ptr(ptr: *mut Self::CType) -> Self;
|
||||||
|
|
||||||
|
fn as_ptr(&self) -> *mut Self::CType;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Ref<T>(Opaque, PhantomData<T>);
|
||||||
|
|
||||||
|
impl<T: OpenSslType> Ref<T> {
|
||||||
|
pub unsafe fn from_ptr<'a>(ptr: *mut T::CType) -> &'a Ref<T> {
|
||||||
|
&*(ptr as *mut _)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe fn from_ptr_mut<'a>(ptr: *mut T::CType) -> &'a mut Ref<T> {
|
||||||
|
&mut *(ptr as *mut _)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_ptr(&self) -> *mut T::CType {
|
||||||
|
self as *const _ as *mut _
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue