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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
use core::cmp::Ordering;
use core::fmt::{self, Debug, Display, Formatter};
use core::hash::{Hash, Hasher};
use core::marker::PhantomData;
use core::mem;
use core::ptr;
use core::slice;
use core::str;
use crate::block::Layout;
use crate::alloc::{Hold, HoldError, Stow, TryClone};
use crate::lease::Lease;
use crate::resident::{Resident, ResidentFromValue, ResidentFromClone,
                      ResidentFromCopy, ResidentUnwrap, ResidentDeref,
                      ResidentDerefMut, ResidentAsRef, ResidentAsMut,
                      ResidentPartialEq, ResidentEq, ResidentPartialOrd,
                      ResidentOrd, ResidentHash, ResidentDisplay,
                      ResidentDebug, ResidentClone, ResidentStow};

/// A single value, residing in a memory `Lease`. A `Box` is a `Resident`
/// typeclass; it doesn't store any data in its internal structure. Rather,
/// `Box` implements an access pattern for memory blocks managed by a `Lease`.
/// A composing `Lease` type defines the memory allocation and ownership
/// semantics of the composed `Box` type.
///
/// `Lease` implementations commonly composed with a `Box` include:
/// - `Own<Box<T>>`: the exclusive owner of a relocatable value.
/// - `Mut<Box<T>>`: a mutably dereferenceable strong owner of an
///   unrelocatable, reference counted value.
/// - `Ref<Box<T>>`: an immutably dereferenceable strong owner of an
///   unrelocatable, reference counted value.
/// - `Hard<Box<T>>`: an undereferenceable strong owner of a relocatable,
///   reference counted value.
/// - `Soft<Box<T>>`: an undereferenceable weak owner of a relocatable,
///   reference counted value.
///
/// `Box` implements `LeaseDeref`, and `LeaseDerefMut`, which enables deref
/// coercions from `Own<Box<T>>` and `Mut<Box<T>>` to `&T` and `&mut T`,
/// and from `Ref<Box<T>>` to `&T`. This makes leased boxes "feel" like they
/// contain a value, even though `Box` is just a typeclass for accessing the
/// value stored in the composing memory `Lease`.
pub struct Box<T: ?Sized, M = ()> {
    /// Variant over T, with drop check.
    data_marker: PhantomData<T>,
    /// Variant over M, with drop check.
    meta_marker: PhantomData<M>,
}

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

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

impl<T: ?Sized, M> Box<T, M> {
    #[inline]
    fn as_ref(lease: &impl Lease<Data=T, Meta=M>) -> &T {
        unsafe { &*lease.data() }
    }

    #[inline]
    fn as_mut(lease: &mut impl Lease<Data=T, Meta=M>) -> &mut T {
        unsafe { &mut *lease.data() }
    }
}

impl<T: ?Sized, M> Resident for Box<T, M> {
    type Data = T;

    type Meta = M;

    #[inline]
    unsafe fn resident_size(data: *mut T, _meta: *mut M) -> usize {
        mem::size_of_val(&*data)
    }

    #[inline]
    unsafe fn resident_drop(data: *mut T, _meta: *mut M) {
        ptr::drop_in_place(data)
    }
}

impl<L: Lease<Data=T, Meta=M>, T, M> ResidentFromValue<L, T, M> for Box<T, M> {
    #[inline]
    fn new_resident_layout(data: &T, _meta: &M) -> Layout {
        Layout::for_value(data)
    }

    #[inline]
    fn new_resident_ptr(raw: *mut u8, _data: &T, _meta: &M) -> *mut T {
        raw as *mut T
    }

    #[inline]
    fn new_resident(lease: &mut L, data: T, meta: M) {
        unsafe {
            ptr::write(lease.meta(), meta);
            ptr::write(lease.data(), data);
        }
    }
}

impl<L: Lease<Data=[T], Meta=M>, T: Clone, M> ResidentFromClone<L, [T], M> for Box<[T], M> {
    #[inline]
    fn new_resident_layout(data: &[T], _meta: &M) -> Layout {
        Layout::for_value(data)
    }

    #[inline]
    fn new_resident_ptr(raw: *mut u8, data: &[T], _meta: &M) -> *mut [T] {
        unsafe { slice::from_raw_parts_mut(raw as *mut T, data.len()) }
    }

    #[inline]
    fn new_resident(lease: &mut L, data: &[T], meta: M) {
        unsafe {
            ptr::write(lease.meta(), meta);
            (*lease.data()).clone_from_slice(data);
        }
    }
}

impl<L: Lease<Data=[T], Meta=M>, T: Copy, M> ResidentFromCopy<L, [T], M> for Box<[T], M> {
    #[inline]
    fn new_resident_layout(data: &[T], _meta: &M) -> Layout {
        Layout::for_value(data)
    }

    #[inline]
    fn new_resident_ptr(raw: *mut u8, data: &[T], _meta: &M) -> *mut [T] {
        unsafe { slice::from_raw_parts_mut(raw as *mut T, data.len()) }
    }

    #[inline]
    fn new_resident(lease: &mut L, data: &[T], meta: M) {
        unsafe {
            ptr::write(lease.meta(), meta);
            (*lease.data()).copy_from_slice(data);
        }
    }
}

impl<L: Lease<Data=str, Meta=M>, M> ResidentFromCopy<L, str, M> for Box<str, M> {
    #[inline]
    fn new_resident_layout(data: &str, _meta: &M) -> Layout {
        Layout::for_value(data)
    }

    #[inline]
    fn new_resident_ptr(raw: *mut u8, data: &str, _meta: &M) -> *mut str {
        unsafe { str::from_utf8_unchecked_mut(slice::from_raw_parts_mut(raw, data.len())) }
    }

    #[inline]
    fn new_resident(lease: &mut L, data: &str, meta: M) {
        unsafe {
            ptr::write(lease.meta(), meta);
            (*lease.data()).as_bytes_mut().copy_from_slice(data.as_bytes());
        }
    }
}

impl<L: Lease> ResidentUnwrap<L> for Box<L::Data, L::Meta> where L::Data: Sized {
    type Target = L::Data;

    #[inline]
    fn resident_unwrap(lease: &L) -> L::Data {
        unsafe { ptr::read(lease.data()) }
    }
}

impl<L: Lease> ResidentDeref<L> for Box<L::Data, L::Meta> {
    type Target = L::Data;

    #[inline]
    fn resident_deref(lease: &L) -> &L::Data {
        Box::as_ref(lease)
    }
}

impl<L: Lease> ResidentDerefMut<L> for Box<L::Data, L::Meta> {
    #[inline]
    fn resident_deref_mut(lease: &mut L) -> &mut L::Data {
        Box::as_mut(lease)
    }
}

impl<L: Lease> ResidentAsRef<L, L::Data> for Box<L::Data, L::Meta> {
    #[inline]
    fn resident_as_ref(lease: &L) -> &L::Data {
        Box::as_ref(lease)
    }
}

impl<L: Lease> ResidentAsMut<L, L::Data> for Box<L::Data, L::Meta> {
    #[inline]
    fn resident_as_mut(lease: &mut L) -> &mut L::Data {
        Box::as_mut(lease)
    }
}

impl<L: Lease> ResidentPartialEq<L> for Box<L::Data, L::Meta> where L::Data: PartialEq {
    #[inline]
    fn resident_eq(lease: &L, other: &L) -> bool {
        Box::as_ref(lease).eq(Box::as_ref(other))
    }

    #[inline]
    fn resident_ne(lease: &L, other: &L) -> bool {
        Box::as_ref(lease).ne(Box::as_ref(other))
    }
}

impl<L: Lease> ResidentEq<L> for Box<L::Data, L::Meta> where L::Data: Eq {
}

impl<L: Lease> ResidentPartialOrd<L> for Box<L::Data, L::Meta> where L::Data: PartialOrd {
    #[inline]
    fn resident_partial_cmp(lease: &L, other: &L) -> Option<Ordering> {
        Box::as_ref(lease).partial_cmp(Box::as_ref(other))
    }

    #[inline]
    fn resident_lt(lease: &L, other: &L) -> bool {
        Box::as_ref(lease).lt(Box::as_ref(other))
    }

    #[inline]
    fn resident_le(lease: &L, other: &L) -> bool {
        Box::as_ref(lease).le(Box::as_ref(other))
    }

    #[inline]
    fn resident_ge(lease: &L, other: &L) -> bool {
        Box::as_ref(lease).ge(Box::as_ref(other))
    }

    #[inline]
    fn resident_gt(lease: &L, other: &L) -> bool {
        Box::as_ref(lease).gt(Box::as_ref(other))
    }
}

impl<L: Lease> ResidentOrd<L> for Box<L::Data, L::Meta> where L::Data: Ord {
    #[inline]
    fn resident_cmp(lease: &L, other: &L) -> Ordering {
        Box::as_ref(lease).cmp(Box::as_ref(other))
    }
}

impl<L: Lease> ResidentHash<L> for Box<L::Data, L::Meta> where L::Data: Hash {
    #[inline]
    fn resident_hash<H: Hasher>(lease: &L, state: &mut H) {
        Box::as_ref(lease).hash(state)
    }
}

impl<L: Lease> ResidentDisplay<L> for Box<L::Data, L::Meta> where L::Data: Display {
    #[inline]
    fn resident_fmt(lease: &L, f: &mut Formatter) -> fmt::Result {
        Display::fmt(Box::as_ref(lease), f)
    }
}

impl<L: Lease> ResidentDebug<L> for Box<L::Data, L::Meta> where L::Data: Debug {
    #[inline]
    fn resident_fmt(lease: &L, f: &mut Formatter) -> fmt::Result {
        Debug::fmt(Box::as_ref(lease), f)
    }
}

impl<L1, L2, T, M> ResidentClone<L1, L2> for Box<T, M>
    where L1: Lease<Data=T, Meta=M>,
          L2: Lease<Data=T, Meta=M>,
          T: ?Sized + TryClone,
          M: TryClone,
{
    #[inline]
    fn new_resident_layout(lease: &L1) -> Layout {
        Layout::for_value(Box::as_ref(lease))
    }

    #[inline]
    fn resident_clone(src: &L1, dst: &mut L2) -> Result<(), HoldError> {
        unsafe {
            ptr::write(dst.meta(), (*src.meta()).try_clone()?);
            ptr::write(dst.data(), (*src.data()).try_clone()?);
            Ok(())
        }
    }
}

impl<L1, L2, T, M> ResidentClone<L1, L2> for Box<[T], M>
    where L1: Lease<Data=[T], Meta=M>,
          L2: Lease<Data=[T], Meta=M>,
          T: TryClone,
          M: TryClone,
{
    #[inline]
    fn new_resident_layout(lease: &L1) -> Layout {
        Layout::for_value(Box::as_ref(lease))
    }

    #[inline]
    fn resident_clone(src: &L1, dst: &mut L2) -> Result<(), HoldError> {
        unsafe {
            ptr::write(dst.meta(), (*src.meta()).try_clone()?);
            let src_data = src.data();
            let dst_data = dst.data();
            let len = (*src_data).len();
            let mut src_data = src_data as *const T;
            let mut dst_data = dst_data as *mut T;
            let mut i = 0;
            while i < len {
                let src_elem = match (*dst_data).try_clone() {
                    Ok(elem) => elem,
                    Err(error) => {
                        while i > 0 {
                            dst_data = dst_data.wrapping_sub(1);
                            i = i.wrapping_sub(1);
                            ptr::drop_in_place(dst_data);
                        }
                        return Err(error);
                    }
                };
                ptr::write(dst_data, src_elem);
                src_data = src_data.wrapping_add(1);
                dst_data = dst_data.wrapping_add(1);
                i = i.wrapping_add(1);
            }
            Ok(())
        }
    }
}

impl<L1, L2, M> ResidentClone<L1, L2> for Box<str, L1::Meta>
    where L1: Lease<Data=str, Meta=M>,
          L2: Lease<Data=str, Meta=M>,
          M: TryClone,
{
    #[inline]
    fn new_resident_layout(lease: &L1) -> Layout {
        Layout::for_value(Box::as_ref(lease))
    }

    #[inline]
    fn resident_clone(src: &L1, dst: &mut L2) -> Result<(), HoldError> {
        unsafe {
            ptr::write(dst.meta(), (*src.meta()).try_clone()?);
            let src_data = src.data();
            let dst_data = dst.data();
            let len = (*src_data).len();
            ptr::copy_nonoverlapping(src_data as *const u8, dst_data as *mut u8, len);
            Ok(())
        }
    }
}

impl<'b, L1: Lease, L2: Lease> ResidentStow<'b, L1, L2> for Box<L1::Data, L1::Meta>
    where L1::Data: Stow<'b, L2::Data>,
          L1::Meta: Stow<'b, L2::Meta>,
{
    #[inline]
    fn new_resident_layout(lease: &L1) -> Layout {
        Layout::for_value(Box::as_ref(lease))
    }

    #[inline]
    unsafe fn resident_stow(src: &mut L1, dst: &mut L2, hold: &Hold<'b>) -> Result<(), HoldError> {
        if let err @ Err(_) = L1::Meta::stow(src.meta(), dst.meta(), hold) {
            return err;
        }
        if let err @ Err(_) = L1::Data::stow(src.data(), dst.data(), hold) {
            L1::Meta::unstow(src.meta(), dst.meta());
            return err;
        }
        Ok(())
    }

    #[inline]
    unsafe fn resident_unstow(src: &mut L1, dst: &mut L2) {
        L1::Data::unstow(src.data(), dst.data());
        L1::Meta::unstow(src.meta(), dst.meta());
    }
}