flow-lang-value 0.1.0-rc.5

The Flow runtime value model: data values, their equality, order and text, and the operations std's built-in types declare.
Documentation
//! Records and enum values (`types.md`, *Records* and *Enums*).
//!
//! A value holds its parts in **declaration order**, as slots, beside the
//! shared description of its declaration, a [`RecordShape`] or an
//! [`EnumShape`]. The slots are what equality and order read — field by
//! field in declaration order, and for an enum the constructor's position
//! first — and the description is what the text form reads.

use std::sync::Arc;

use flow_types::{Constructor, EnumShape, RecordShape};

use crate::value::{Meta, Value};

/// A record value.
#[derive(Clone, Debug)]
pub struct Record {
    shape: Arc<RecordShape>,
    fields: Arc<[Value]>,
    meta: Meta,
}

impl Record {
    /// Returns the record of `shape` with `fields` in declaration order, or
    /// `None` when there is not exactly one value per declared field.
    pub fn new(shape: Arc<RecordShape>, fields: Vec<Value>) -> Option<Self> {
        let arity = shape.fields().len();
        (fields.len() == arity).then(|| Record {
            meta: Meta::around(&fields),
            shape,
            fields: fields.into(),
        })
    }

    /// Returns the declaration.
    pub fn shape(&self) -> &Arc<RecordShape> {
        &self.shape
    }

    /// Returns the field values, in declaration order.
    pub fn fields(&self) -> &[Value] {
        &self.fields
    }

    /// Returns the field values when nothing else holds them.
    /// What it records of its fields.
    pub(crate) fn meta(&self) -> Meta {
        self.meta
    }

    /// What tells this record apart from every other alive: its
    /// declaration and its fields.
    pub(crate) fn id(&self) -> [usize; 2] {
        [
            Arc::as_ptr(&self.shape).addr(),
            Arc::as_ptr(&self.fields).cast::<Value>().addr(),
        ]
    }

    pub(crate) fn fields_mut(&mut self) -> Option<&mut [Value]> {
        Arc::get_mut(&mut self.fields)
    }

    /// Returns the field at a declaration index.
    pub fn field(&self, index: usize) -> Option<&Value> {
        self.fields.get(index)
    }
}

impl Drop for Record {
    fn drop(&mut self) {
        if let Some(fields) = self.fields_mut() {
            crate::value::dismantle(fields.iter_mut());
        }
    }
}

/// An enum value.
#[derive(Clone, Debug)]
pub struct Variant {
    shape: Arc<EnumShape>,
    index: u32,
    meta: Meta,
    payload: Arc<[Value]>,
}

impl Variant {
    /// Returns the value of the constructor at a declaration index carrying
    /// `payload`, or `None` when there is no such constructor or the payload
    /// does not hold exactly as many values as it declares.
    pub fn new(shape: Arc<EnumShape>, index: usize, payload: Vec<Value>) -> Option<Self> {
        let arity = shape.constructor(index)?.payload().arity();
        let index = u32::try_from(index).ok()?;
        (payload.len() == arity).then(|| Variant {
            meta: Meta::around(&payload),
            shape,
            index,
            payload: payload.into(),
        })
    }

    /// Returns the declaration.
    pub fn shape(&self) -> &Arc<EnumShape> {
        &self.shape
    }

    /// Returns the constructor's declaration index.
    pub fn index(&self) -> usize {
        // Made from a `usize` in `Variant::new`, so widening it back loses
        // nothing.
        #[expect(
            clippy::as_conversions,
            reason = "a u32 that came from a usize widens back to it losslessly"
        )]
        let index = self.index as usize;
        index
    }

    /// Returns the constructor.
    pub fn constructor(&self) -> &Constructor {
        &self.shape.constructors()[self.index()]
    }

    /// Returns the payload values, in declaration order.
    pub fn payload(&self) -> &[Value] {
        &self.payload
    }

    /// Returns the payload values when nothing else holds them.
    /// What it records of its payload.
    pub(crate) fn meta(&self) -> Meta {
        self.meta
    }

    /// What tells this enum value apart from every other alive: its
    /// declaration, its constructor and its payload.
    pub(crate) fn id(&self) -> [usize; 3] {
        [
            Arc::as_ptr(&self.shape).addr(),
            self.index(),
            Arc::as_ptr(&self.payload).cast::<Value>().addr(),
        ]
    }

    pub(crate) fn payload_mut(&mut self) -> Option<&mut [Value]> {
        Arc::get_mut(&mut self.payload)
    }
}

impl Drop for Variant {
    fn drop(&mut self) {
        if let Some(payload) = self.payload_mut() {
            crate::value::dismantle(payload.iter_mut());
        }
    }
}