Sha256: d247afc21c2ac015b2bb6bd357b29a5c7dae54e24cf1bcc2aaa116e7321e22d1

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 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::cmp::Ordering;
use std::ops::Deref;
use std::sync::Arc;

#[derive(Debug)]
/// 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)),
        }
    }
}

impl<T: PartialEq + ?Sized> PartialEq<Sod<T>> for Sod<T> {
    fn eq(&self, other: &Sod<T>) -> bool {
        self.deref().eq(other.deref())
    }
}

impl<T: PartialOrd + ?Sized> PartialOrd<Sod<T>> for Sod<T> {
    fn partial_cmp(&self, other: &Sod<T>) -> Option<Ordering> {
        self.deref().partial_cmp(other.deref())
    }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libpasta-0.1.0.pre.rc0-x86_64-linux ext/pasta-bindings/libpasta/src/sod.rs