o3 0.62.0

shared-nothing primitives
Documentation
use crate::{base::marker::ThreadBound, mem};

/// Owns an optional value and its lifecycle metadata.
#[derive(Debug)]
#[repr(C)]
pub struct Storage<T, M = ()> {
    pub(in crate::mem) value: Option<T>,
    metadata: M,
    bound: ThreadBound,
}

impl<T, M> Storage<T, M> {
    /// # Safety
    /// This exact cell is initialized and its lifecycle is frozen for the loan.
    pub unsafe fn project(&self) -> &T {
        unsafe { mem::raw::initialized(&self.value) }
    }

    /// # Safety
    /// This exact cell is initialized and remains initialized for the returned loan.
    pub unsafe fn project_mut(&mut self) -> &mut T {
        unsafe { mem::raw::initialized_mut(&mut self.value) }
    }

    /// # Safety
    /// This exact cell is initialized. End every authority depending on that
    /// initialization before transferring this exclusive take capability.
    pub unsafe fn occupy(&mut self) -> mem::raw::Occupied<'_, T> {
        mem::raw::Occupied {
            source: &mut self.value,
            bound: ThreadBound::NEW,
        }
    }

    /// # Safety
    /// This exact cell is vacant and exclusively borrowed until initialization.
    pub unsafe fn vacancy(&mut self) -> mem::raw::Vacant<'_, T> {
        mem::raw::Vacant {
            source: &mut self.value,
            bound: ThreadBound::NEW,
        }
    }

    pub const fn new(metadata: M) -> Self {
        Self {
            value: None,
            metadata,
            bound: ThreadBound::NEW,
        }
    }

    pub const fn filled(value: T, metadata: M) -> Self {
        Self {
            value: Some(value),
            metadata,
            bound: ThreadBound::NEW,
        }
    }

    pub fn insert(&mut self, value: T) -> mem::raw::Occupied<'_, T> {
        self.value = Some(value);
        unsafe { self.occupy() }
    }

    pub const fn metadata(&self) -> &M {
        &self.metadata
    }

    pub fn metadata_mut(&mut self) -> &mut M {
        &mut self.metadata
    }

    pub const fn get(&self) -> Option<&T> {
        self.value.as_ref()
    }

    pub fn get_mut(&mut self) -> Option<&mut T> {
        self.value.as_mut()
    }

    pub fn occupied(&mut self) -> Option<mem::raw::Occupied<'_, T>> {
        self.value.as_ref()?;
        Some(unsafe { self.occupy() })
    }

    pub fn vacant(&mut self) -> Option<mem::raw::Vacant<'_, T>> {
        if self.value.is_some() {
            return None;
        }
        Some(unsafe { self.vacancy() })
    }
}