aboutsummaryrefslogtreecommitdiff
path: root/openssl/src/types.rs
blob: 40831ee80ba2507bd3ea3481095aba68bec03d1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//! Items used by other types.

use std::cell::UnsafeCell;
use std::marker::PhantomData;

/// A type implemented by wrappers over OpenSSL types.
///
/// This should not be implemented by anything outside of this crate; new methods may be added at
/// any time.
pub unsafe trait OpenSslType {
    /// The raw C type.
    type CType;

    /// Constructs an instance of this type from its raw type.
    unsafe fn from_ptr(ptr: *mut Self::CType) -> Self;

    /// Returns a pointer to its raw type.
    fn as_ptr(&self) -> *mut Self::CType;
}

/// A reference to an OpenSSL type.
pub struct Ref<T>(UnsafeCell<()>, PhantomData<T>);

impl<T: OpenSslType> Ref<T> {
    /// Constructs a shared reference to this type from its raw type.
    pub unsafe fn from_ptr<'a>(ptr: *mut T::CType) -> &'a Ref<T> {
        &*(ptr as *mut _)
    }

    /// Constructs a mutable reference to this type from its raw type.
    pub unsafe fn from_ptr_mut<'a>(ptr: *mut T::CType) -> &'a mut Ref<T> {
        &mut *(ptr as *mut _)
    }

    /// Returns a pointer to its raw type.
    pub fn as_ptr(&self) -> *mut T::CType {
        self as *const _ as *mut _
    }
}