Sha256: 1c94b1444ec8d29b5ff72668003829c2d3a160a2d137722d7272c54d63e1a390

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

/// Sod stands for Static or Dynamic. An enum to encapsulate values which
/// are either dynamically, heap-allocated values, or statics.
///
/// This allows us to define default primitives which are used throughout
/// without the overhead of reference counting, while still supporting the
/// flexibility to create primitives dynamically.
///
/// Thanks to the `Deref` implementation, either variants are treated like
/// the inner type without needing to worry about which it is.
///
/// Many thanks to [panicbit](https://github.com/panicbit) for helping to
/// get the `Deref` implementation working to make all the magic happen.

use std::ops::Deref;
use std::sync::Arc;

#[derive(Debug, PartialEq, PartialOrd)]
/// Enum to hold either static references or reference-counted owned objects.
/// Implements `Deref` to `T` for ease of use.
/// Since internal data is either a static reference, or an `Arc`, cloning
/// is a cheap operation.
pub enum Sod<T: ?Sized + 'static> {
    /// Static reference to T
    Static(&'static T),
    /// Dynamically allocated T, on the heap, atomically reference-counted.
    Dynamic(Arc<Box<T>>),
}

impl<T: ?Sized> Deref for Sod<T> {
    type Target = T;
    fn deref(&self) -> &T {
        match *self {
            Sod::Static(t) => t,
            Sod::Dynamic(ref t) => t,
        }
    }
}

impl<T: ?Sized> Clone for Sod<T> {
    fn clone(&self) -> Self {
        match *self {
            Sod::Static(t) => Sod::Static(t),
            Sod::Dynamic(ref t) => Sod::Dynamic(Arc::clone(t)),
        }
    }
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
libpasta-0.0.6-x86_64-linux ext/pasta-bindings/libpasta/src/sod.rs
libpasta-0.0.5 ext/pasta-bindings/libpasta/src/sod.rs
libpasta-0.0.5-x86_64-linux ext/pasta-bindings/libpasta/src/sod.rs