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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! `#[repr(C)]` [`Box`][`rust::Box`]ed types.

use_prelude!();

ReprC! {
    #[repr(transparent)]
    /// Same as [`Box<T>`][`rust::Box`], (_e.g._, same `#[repr(C)]` layout), but
    /// with **no non-aliasing guarantee**.
    pub
    struct Box[T] (
        ptr::NonNullOwned<T>,
    );
}

impl<T> From<rust::Box<T>>
    for Box<T>
{
    #[inline]
    fn from (boxed: rust::Box<T>)
      -> Box<T>
    {
        Self(
            ptr::NonNull::from(rust::Box::leak(boxed))
                .into()
        )
    }
}

impl<T> Box<T> {
    #[inline]
    pub
    fn new (value: T)
      -> Self
    {
        rust::Box::new(value)
            .into()
    }

    #[inline]
    pub
    fn into (self: Box<T>)
      -> rust::Box<T>
    {
        let mut this = mem::ManuallyDrop::new(self);
        unsafe {
            rust::Box::from_raw(this.0.as_mut_ptr())
        }
    }
}

impl<T> Drop
    for Box<T>
{
    #[inline]
    fn drop (self: &'_ mut Box<T>)
    {
        unsafe {
            drop::<rust::Box<T>>(
                rust::Box::from_raw(self.0.as_mut_ptr())
            );
        }
    }
}

impl<T> Deref
    for Box<T>
{
    type Target = T;

    #[inline]
    fn deref (self: &'_ Box<T>)
      -> &'_ T
    {
        unsafe {
            &*self.0.as_ptr()
        }
    }
}

impl<T> DerefMut
    for Box<T>
{
    #[inline]
    fn deref_mut (self: &'_ mut Box<T>)
      -> &'_ mut T
    {
        unsafe {
            &mut *(self.0.as_mut_ptr())
        }
    }
}

unsafe impl<T> Send
    for Box<T>
where
    rust::Box<T> : Send,
{}

unsafe impl<T> Sync
    for Box<T>
where
    rust::Box<T> : Sync,
{}

impl<T : fmt::Debug> fmt::Debug
    for Box<T>
{
    fn fmt (self: &'_ Self, fmt: &'_ mut fmt::Formatter<'_>)
      -> fmt::Result
    {
        T::fmt(self, fmt)
    }
}

#[doc(no_inline)]
pub use crate::slice::slice_boxed;

#[doc(no_inline)]
pub use crate::string::str_boxed;