From 927c3e924cd894cb4b32efff666f949a294ab7c7 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Mon, 31 Oct 2016 19:37:21 -0700 Subject: Add a generic Ref type --- openssl/src/types.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 openssl/src/types.rs (limited to 'openssl/src/types.rs') diff --git a/openssl/src/types.rs b/openssl/src/types.rs new file mode 100644 index 00000000..16829ea4 --- /dev/null +++ b/openssl/src/types.rs @@ -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(Opaque, PhantomData); + +impl Ref { + pub unsafe fn from_ptr<'a>(ptr: *mut T::CType) -> &'a Ref { + &*(ptr as *mut _) + } + + pub unsafe fn from_ptr_mut<'a>(ptr: *mut T::CType) -> &'a mut Ref { + &mut *(ptr as *mut _) + } + + pub fn as_ptr(&self) -> *mut T::CType { + self as *const _ as *mut _ + } +} -- cgit v1.2.3