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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use core::fmt;
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;
use core::ptr::drop_in_place;
use core::mem::ManuallyDrop;
use mbedtls_sys::types::raw_types::c_void;
extern "C" {
pub(crate) fn forward_mbedtls_free(n: *mut mbedtls_sys::types::raw_types::c_void);
}
#[repr(transparent)]
pub struct Box<T> {
pub(crate) inner: NonNull<T>
}
impl<T> Box<T> {
pub(crate) fn into_raw(self) -> *mut T {
let v = ManuallyDrop::new(self);
v.inner.as_ptr()
}
}
impl<T> Deref for Box<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { self.inner.as_ref() }
}
}
impl<T> DerefMut for Box<T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { self.inner.as_mut() }
}
}
impl<T: fmt::Debug> fmt::Debug for Box<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<T> Drop for Box<T> {
fn drop(&mut self) {
unsafe {
drop_in_place(self.inner.as_ptr());
forward_mbedtls_free(self.inner.as_ptr() as *mut c_void)
}
}
}
#[repr(transparent)]
pub struct List<T> {
pub(crate) inner: Option<Box<T>>
}