Skip to main content

flow_value/
shape.rs

1//! Records and enum values (`types.md`, *Records* and *Enums*).
2//!
3//! A value holds its parts in **declaration order**, as slots, beside the
4//! shared description of its declaration, a [`RecordShape`] or an
5//! [`EnumShape`]. The slots are what equality and order read — field by
6//! field in declaration order, and for an enum the constructor's position
7//! first — and the description is what the text form reads.
8
9use std::sync::Arc;
10
11use flow_types::{Constructor, EnumShape, RecordShape};
12
13use crate::value::{Meta, Value};
14
15/// A record value.
16#[derive(Clone, Debug)]
17pub struct Record {
18    shape: Arc<RecordShape>,
19    fields: Arc<[Value]>,
20    meta: Meta,
21}
22
23impl Record {
24    /// Returns the record of `shape` with `fields` in declaration order, or
25    /// `None` when there is not exactly one value per declared field.
26    pub fn new(shape: Arc<RecordShape>, fields: Vec<Value>) -> Option<Self> {
27        let arity = shape.fields().len();
28        (fields.len() == arity).then(|| Record {
29            meta: Meta::around(&fields),
30            shape,
31            fields: fields.into(),
32        })
33    }
34
35    /// Returns the declaration.
36    pub fn shape(&self) -> &Arc<RecordShape> {
37        &self.shape
38    }
39
40    /// Returns the field values, in declaration order.
41    pub fn fields(&self) -> &[Value] {
42        &self.fields
43    }
44
45    /// Returns the field values when nothing else holds them.
46    /// What it records of its fields.
47    pub(crate) fn meta(&self) -> Meta {
48        self.meta
49    }
50
51    /// What tells this record apart from every other alive: its
52    /// declaration and its fields.
53    pub(crate) fn id(&self) -> [usize; 2] {
54        [
55            Arc::as_ptr(&self.shape).addr(),
56            Arc::as_ptr(&self.fields).cast::<Value>().addr(),
57        ]
58    }
59
60    pub(crate) fn fields_mut(&mut self) -> Option<&mut [Value]> {
61        Arc::get_mut(&mut self.fields)
62    }
63
64    /// Returns the field at a declaration index.
65    pub fn field(&self, index: usize) -> Option<&Value> {
66        self.fields.get(index)
67    }
68}
69
70impl Drop for Record {
71    fn drop(&mut self) {
72        if let Some(fields) = self.fields_mut() {
73            crate::value::dismantle(fields.iter_mut());
74        }
75    }
76}
77
78/// An enum value.
79#[derive(Clone, Debug)]
80pub struct Variant {
81    shape: Arc<EnumShape>,
82    index: u32,
83    meta: Meta,
84    payload: Arc<[Value]>,
85}
86
87impl Variant {
88    /// Returns the value of the constructor at a declaration index carrying
89    /// `payload`, or `None` when there is no such constructor or the payload
90    /// does not hold exactly as many values as it declares.
91    pub fn new(shape: Arc<EnumShape>, index: usize, payload: Vec<Value>) -> Option<Self> {
92        let arity = shape.constructor(index)?.payload().arity();
93        let index = u32::try_from(index).ok()?;
94        (payload.len() == arity).then(|| Variant {
95            meta: Meta::around(&payload),
96            shape,
97            index,
98            payload: payload.into(),
99        })
100    }
101
102    /// Returns the declaration.
103    pub fn shape(&self) -> &Arc<EnumShape> {
104        &self.shape
105    }
106
107    /// Returns the constructor's declaration index.
108    pub fn index(&self) -> usize {
109        // Made from a `usize` in `Variant::new`, so widening it back loses
110        // nothing.
111        #[expect(
112            clippy::as_conversions,
113            reason = "a u32 that came from a usize widens back to it losslessly"
114        )]
115        let index = self.index as usize;
116        index
117    }
118
119    /// Returns the constructor.
120    pub fn constructor(&self) -> &Constructor {
121        &self.shape.constructors()[self.index()]
122    }
123
124    /// Returns the payload values, in declaration order.
125    pub fn payload(&self) -> &[Value] {
126        &self.payload
127    }
128
129    /// Returns the payload values when nothing else holds them.
130    /// What it records of its payload.
131    pub(crate) fn meta(&self) -> Meta {
132        self.meta
133    }
134
135    /// What tells this enum value apart from every other alive: its
136    /// declaration, its constructor and its payload.
137    pub(crate) fn id(&self) -> [usize; 3] {
138        [
139            Arc::as_ptr(&self.shape).addr(),
140            self.index(),
141            Arc::as_ptr(&self.payload).cast::<Value>().addr(),
142        ]
143    }
144
145    pub(crate) fn payload_mut(&mut self) -> Option<&mut [Value]> {
146        Arc::get_mut(&mut self.payload)
147    }
148}
149
150impl Drop for Variant {
151    fn drop(&mut self) {
152        if let Some(payload) = self.payload_mut() {
153            crate::value::dismantle(payload.iter_mut());
154        }
155    }
156}