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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#![cfg_attr(rustfmt, rustfmt::skip)]
//! `#[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 : Clone> Clone
    for Box_<T>
{
    #[inline]
    fn clone(self: &'_ Self)
      -> Self
    {
        Self::new(T::clone(self))
    }
}

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;

pub
type Box<T> = <T as FitForCBox>::CBoxWrapped;

pub
trait FitForCBox {
    type CBoxWrapped;
}

impl<T : Sized> FitForCBox for T {
    type CBoxWrapped = Box_<T>;
}

impl<T : Sized> FitForCBox for [T] {
    type CBoxWrapped = c_slice::Box<T>;
}

pub
trait FitForCArc {
    type CArcWrapped;
}