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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! A pointer type which allows for safe transformations of its content without reallocation.
//! This crate does not depend on the standard library, and can be used in `#![no_std]` contexts.
//! It does however require the `alloc` crate.
//!
//! For more details look at the documentation of [`EvolveBox`].
//!
//! [`EvolveBox`]: ./struct.EvolveBox.html

extern crate alloc;

use core::{
    cmp::{Eq, PartialEq},
    fmt,
    marker::PhantomData,
    mem,
    ops::{Deref, DerefMut},
    ptr::{self, NonNull},
};

use alloc::alloc::{self as a, Layout};

/// A trait used to calculate the size and alignment of an [`EvolveBox`].
/// This trait is unsafe to implement, as returning the wrong alignment or size
/// for any type in the list can easily result in memory unsafety.
///
/// As this trait is already implemented for [`()`] and [`L`] there should be no need to
/// implement this as a user of this crate.
///
/// [`EvolveBox`]: ./struct.EvolveBox.html
/// [`()`]: https://doc.rust-lang.org/std/primitive.unit.html
/// [`L`]: ./struct.L.html
pub unsafe trait ListLayout {
    fn size() -> usize;
    fn align() -> usize;
}

unsafe impl ListLayout for () {
    fn size() -> usize {
        0
    }

    fn align() -> usize {
        1
    }
}

unsafe impl<E, C: ListLayout> ListLayout for L<E, C> {
    fn size() -> usize {
        mem::size_of::<E>().max(C::size())
    }
    fn align() -> usize {
        mem::align_of::<E>().max(C::align())
    }
}

/// A trait used to calculate the current type contained in the [`EvolveBox`] at compile time,
/// meaning that there should not be a runtime cost when using an [`EvolveBox`].
///
/// This trait is an implementation detail of this crate and can be mostly ignored.
///
/// # Examples
///
/// ```
/// use evobox::{L, List};
///
/// fn element_types(value: &L<u8, L<u16, L<u32>>>) {
///     fn first(_: &impl List<(), Value = u8>) {}
///     first(value);
///
///     fn second(_: &impl List<L<()>, Value = u16>) {}
///     second(value);
///
///     fn third(_: &impl List<L<L<()>>, Value = u32>) {}
///     third(value);
/// }
/// ```
///
/// [`EvolveBox`]: ./struct.EvolveBox.html
pub trait List<V>: ListLayout {
    type Value;
}

impl<E, C: ListLayout> List<()> for L<E, C> {
    type Value = E;
}

impl<E, B, C: List<B>> List<L<B>> for L<E, C> {
    type Value = C::Value;
}

/// A singly linked list containing types, indexed by itself using the [`List`] trait and
/// used by [`EvolveBox`] to store all possible types.
///
/// For examples of its actual usage please look at the documentation for the [`EvolveBox`].
///
/// # Examples
///
/// ```
/// use evobox::{L, List};
///
/// fn element_types(value: &L<u8, L<u16, L<u32>>>) {
///     fn first(_: &impl List<(), Value = u8>) {}
///     first(value);
///
///     fn second(_: &impl List<L<()>, Value = u16>) {}
///     second(value);
///
///     fn third(_: &impl List<L<L<()>>, Value = u32>) {}
///     third(value);
/// }
/// ```
/// [`EvolveBox`]: ./struct.EvolveBox.html
/// [`List`]: ./trait.List.html
pub struct L<E, C = ()> {
    _marker: PhantomData<fn(E, C)>,
}

/// A pointer type which allows for safe transformations of its content without reallocation.
///
/// An `EvolveBox` has the same size as a [`Box`] and has the smallest size and alignment on the heap needed
/// to store its largest possible variant.
///
/// Therefore `EvolveBox` should be a zero cost abstraction,
/// meaning that there should be no runtime difference between a [`Box`] pointing at an [untagged union] and an `EvolveBox`,
/// while using an `EvolveBox` is also safe.
///
/// The size and alignment of the allocated memory is stored in the type `S`, which is a list of all used types,  
/// as it is required for deallocation.
///
/// # Examples
///
/// Using `EvolveBox` inside of a function.
///
/// ```rust
/// use evobox::{EvolveBox, L};
///
/// let s: EvolveBox<L<&str, L<String, L<u32>>>> = EvolveBox::new("7");
/// let owned = s.evolve(|v| v.to_string());
/// assert_eq!(owned.as_str(), "7");
///
/// let seven = owned.try_evolve(|s| s.parse()).expect("invalid integer");
/// assert_eq!(*seven, 7);
/// ```
///
/// Storing it in a generic struct.
///
/// ```rust
/// use evobox::{EvolveBox, List, L};
///
/// enum Ty {
///     Integer,
///     String,
///     Boolean,
/// }
///
/// #[derive(Debug)]
/// struct TypeError;
///
/// struct Variable<'a, N, T>
/// where
///     L<&'a str, L<usize>>: List<N>,
///     L<&'a str, L<Ty>>: List<T>,
/// {
///     name: EvolveBox<L<&'a str, L<usize>>, N>,
///     ty: EvolveBox<L<&'a str, L<Ty>>, T>,
/// }
///
/// impl<'a> Variable<'a, (), ()> {
///     fn new(name: &'a str, ty: &'a str) -> Self {
///         Self {
///             name: EvolveBox::new(name),
///             ty: EvolveBox::new(ty),
///         }
///     }
/// }
///
/// impl<'a, T> Variable<'a, (), T>
/// where
///     L<&'a str, L<Ty>>: List<T>,
/// {
///     fn resolve_names(self, names: &mut Vec<&'a str>) -> Variable<'a, L<()>, T> {
///         let id = names.len();
///         let name = self.name.evolve(|name| {
///             names.push(name);
///             id
///         });
///
///         Variable { name, ty: self.ty }
///     }
/// }
///
/// impl<'a, N> Variable<'a, N, ()>
/// where
///     L<&'a str, L<usize>>: List<N>,
/// {
///     fn resolve_types(self) -> Result<Variable<'a, N, L<()>>, TypeError> {
///         let ty = self.ty.try_evolve(|ty|
///             match ty {
///                 "int" => Ok(Ty::Integer),
///                 "string" => Ok(Ty::String),
///                 "bool" => Ok(Ty::Boolean),
///                 _ => Err(TypeError)
///             }
///         )?;
///
///         Ok(Variable { name: self.name, ty })
///     }
/// }
///
/// let a = Variable::new("a", "int");
/// let b = Variable::new("b", "string");
///
/// let mut names = Vec::new();
/// let a = a.resolve_names(&mut names);
/// let _ = a.resolve_types().expect("unknown type");
/// let b = b.resolve_types().expect("unknown type");
/// let _ = b.resolve_names(&mut names);
/// ```
/// [`Box`]: https://doc.rust-lang.org/std/boxed/struct.Box.html
/// [untagged union]: https://doc.rust-lang.org/std/keyword.union.html
pub struct EvolveBox<S: List<P>, P = ()> {
    _marker: PhantomData<fn(S, P)>,
    current: NonNull<S::Value>,
}

unsafe impl<S: List<P>, P> Send for EvolveBox<S, P> where S::Value: Send {}
unsafe impl<S: List<P>, P> Sync for EvolveBox<S, P> where S::Value: Sync {}

impl<S: List<()>> EvolveBox<S, ()> {
    /// Allocates memory on the heap and then places `x` into it.
    /// This does not actually allocate if all types used in `S` are zero-sized.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use evobox::{EvolveBox, L};
    /// let value = EvolveBox::<L<_>>::new(7);
    ///
    /// assert_eq!(*value, 7);
    /// ```
    pub fn new(x: S::Value) -> Self {
        if let Some(layout) = Self::calculate_layout() {
            unsafe {
                let current = a::alloc(layout).cast::<S::Value>();
                if current.is_null() {
                    a::handle_alloc_error(layout);
                }
                ptr::write(current, x);

                Self {
                    _marker: PhantomData,
                    current: NonNull::new_unchecked(current),
                }
            }
        } else {
            Self {
                _marker: PhantomData,
                current: NonNull::dangling(),
            }
        }
    }
}

impl<S: List<P>, P> EvolveBox<S, P> {
    fn calculate_layout() -> Option<Layout> {
        let size = S::size();
        let align = S::align();
        if size > 0 {
            debug_assert!(Layout::from_size_align(size, align).is_ok());
            unsafe { Some(Layout::from_size_align_unchecked(size, align)) }
        } else {
            None
        }
    }

    /// Consumes this pointer, returning the current value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use evobox::{EvolveBox, L};
    /// let evolve_box = EvolveBox::<L<_>>::new("hi");
    /// assert_eq!("hi", evolve_box.into_inner());
    /// ```
    pub fn into_inner(self) -> S::Value {
        let pointer = self.current.as_ptr();
        mem::forget(self);
        unsafe {
            let value = ptr::read(pointer);
            if let Some(layout) = Self::calculate_layout() {
                // SAFETY: self.current is valid and will not be
                // used after this function
                a::dealloc(pointer.cast(), layout);
            }
            value
        }
    }
}

impl<S: List<P>, P> EvolveBox<S, P> {
    /// Converts the current value to the next one
    /// without requiring a new allocation.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use evobox::{EvolveBox, L};
    ///
    /// let int_box: EvolveBox<L<u32, L<String>>> = EvolveBox::new(7);
    /// let str_box = int_box.evolve(|i| format!("{}", i));
    /// assert_eq!(str_box.as_str(), "7");
    /// ```
    pub fn evolve<F>(self, f: F) -> EvolveBox<S, L<P>>
    where
        F: FnOnce(<S as List<P>>::Value) -> <S as List<L<P>>>::Value,
        S: List<L<P>>,
    {
        let pointer = self.current;
        // SAFETY: prevent `self` from being dropped
        mem::forget(self);
        unsafe {
            let value = ptr::read(pointer.as_ptr());
            let next = f(value);
            // SAFETY: thanks to `calculate_layout` the pointer must
            // support the type `V` as well. So this case is safe

            // As the pointer data of `C` is now added to `P`, `calculate_layout`
            // does not change its return value
            let mut pointer = pointer.cast();
            ptr::write(pointer.as_mut(), next);
            EvolveBox {
                _marker: PhantomData,
                current: pointer,
            }
        }
    }

    /// Tries to convert the current value to the next one without
    /// requiring a new allocation. The error is propagated outwards in case
    /// the conversion fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use evobox::{EvolveBox, L};
    /// use std::convert::TryFrom;
    ///
    /// let origin: EvolveBox<L<u32, L<u16, L<u8>>>> = EvolveBox::new(256);
    ///
    /// let success = origin.try_evolve(u16::try_from).unwrap();
    /// assert_eq!(*success, 256);
    ///
    /// let error = success.try_evolve(u8::try_from);
    /// assert!(error.is_err());
    /// ```
    pub fn try_evolve<F, E>(self, f: F) -> Result<EvolveBox<S, L<P>>, E>
    where
        F: FnOnce(<S as List<P>>::Value) -> Result<<S as List<L<P>>>::Value, E>,
        S: List<L<P>>,
    {
        let pointer = self.current;
        // SAFETY: prevent `self` from being dropped
        mem::forget(self);
        unsafe {
            let value = ptr::read(pointer.as_ptr());
            match f(value) {
                Ok(next) => {
                    // SAFETY: thanks to `calculate_layout` the pointer must
                    // support the type `V` as well. So this cast is safe
                    let mut pointer = pointer.cast();
                    ptr::write(pointer.as_mut(), next);
                    Ok(EvolveBox {
                        _marker: PhantomData,
                        current: pointer,
                    })
                }
                Err(e) => {
                    if let Some(layout) = Self::calculate_layout() {
                        a::dealloc(pointer.as_ptr().cast(), layout);
                    }
                    Err(e)
                }
            }
        }
    }
}

impl<S: List<P>, P> AsRef<S::Value> for EvolveBox<S, P> {
    fn as_ref(&self) -> &S::Value {
        self.deref()
    }
}

impl<S: List<P>, P> AsMut<S::Value> for EvolveBox<S, P> {
    fn as_mut(&mut self) -> &mut S::Value {
        self.deref_mut()
    }
}

impl<S: List<P>, P> Deref for EvolveBox<S, P> {
    type Target = S::Value;

    fn deref(&self) -> &S::Value {
        unsafe { self.current.as_ref() }
    }
}

impl<S: List<P>, P> DerefMut for EvolveBox<S, P> {
    fn deref_mut(&mut self) -> &mut S::Value {
        unsafe { self.current.as_mut() }
    }
}

impl<S1: List<P1>, P1, S2: List<P2>, P2> PartialEq<EvolveBox<S2, P2>> for EvolveBox<S1, P1>
where
    S1::Value: PartialEq<S2::Value>,
{
    fn eq(&self, other: &EvolveBox<S2, P2>) -> bool {
        self.deref() == other.deref()
    }
}

impl<S: List<P>, P> Eq for EvolveBox<S, P> where S::Value: Eq {}

impl<S: List<P>, P> fmt::Debug for EvolveBox<S, P>
where
    S::Value: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.deref().fmt(f)
    }
}

impl<S: List<P>, P> Drop for EvolveBox<S, P> {
    fn drop(&mut self) {
        if let Some(layout) = Self::calculate_layout() {
            unsafe {
                // SAFETY: self.current is valid and will not be
                // used after this function
                let pointer = self.current.as_ptr();
                let value = ptr::read(pointer);
                a::dealloc(pointer.cast(), layout);
                mem::drop(value);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use alloc::rc::Rc;
    use core::{cell::Cell, convert::TryFrom};

    /// THE WELL KNOWN DOUBLE DROP DETECTOR STRIKES ONCE AGAIN.
    #[derive(Default, Clone)]
    struct DDT(Rc<Cell<bool>>);

    impl Drop for DDT {
        fn drop(&mut self) {
            if self.0.replace(true) {
                panic!("double drop");
            }
        }
    }

    #[test]
    fn simple() {
        let mut t = EvolveBox::<L<u32>>::new(7);
        *t = 9;
        let r = t.as_mut();
        *r += 11;
        assert_eq!(20, *t);
    }

    #[test]
    fn zero_sized() {
        #[derive(Debug, PartialEq)]
        struct Empty;
        struct Foo;
        #[derive(Debug, PartialEq)]
        struct Bar;

        let mut t = EvolveBox::<L<Empty, L<Foo, L<Bar>>>>::new(Empty);
        *t = Empty;

        assert_eq!(
            Bar,
            t.evolve(|t| {
                assert_eq!(Empty, t);
                Foo
            })
            .evolve(|_| Bar)
            .into_inner()
        );
    }

    #[test]
    fn evolve() {
        let int_box = EvolveBox::<L<u32, L<String>>>::new(7);
        let str_box = int_box.evolve(|i| format!("{}", i));
        assert_eq!(str_box.as_str(), "7");
    }

    #[test]
    fn double_free() {
        let ddt = EvolveBox::<L<DDT>>::new(DDT::default());
        ddt.into_inner();
    }

    #[test]
    fn sizes() {
        let evo = EvolveBox::<L<u8, L<u16, L<u32, L<u64, L<u8, L<u16>>>>>>>::new(7);
        assert_eq!(
            7,
            evo.evolve(From::from)
                .evolve(From::from)
                .evolve(From::from)
                .try_evolve(TryFrom::try_from)
                .unwrap()
                .evolve(From::from)
                .into_inner()
        );
    }
}