diff options
| author | Steven Fackler <[email protected]> | 2016-10-31 19:37:21 -0700 |
|---|---|---|
| committer | Steven Fackler <[email protected]> | 2016-10-31 20:02:24 -0700 |
| commit | 927c3e924cd894cb4b32efff666f949a294ab7c7 (patch) | |
| tree | 2a5667608e85469e4115a9d358c8a4119af7fada /openssl/src/types.rs | |
| parent | Mention 0.7 README (diff) | |
| download | rust-openssl-927c3e924cd894cb4b32efff666f949a294ab7c7.tar.xz rust-openssl-927c3e924cd894cb4b32efff666f949a294ab7c7.zip | |
Add a generic Ref type
Diffstat (limited to 'openssl/src/types.rs')
| -rw-r--r-- | openssl/src/types.rs | 27 |
1 files changed, 27 insertions, 0 deletions
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<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 _ + } +} |