1use std::sync::Arc;
10
11use flow_types::{Constructor, EnumShape, RecordShape};
12
13use crate::value::{Meta, Value};
14
15#[derive(Clone, Debug)]
17pub struct Record {
18 shape: Arc<RecordShape>,
19 fields: Arc<[Value]>,
20 meta: Meta,
21}
22
23impl Record {
24 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 pub fn shape(&self) -> &Arc<RecordShape> {
37 &self.shape
38 }
39
40 pub fn fields(&self) -> &[Value] {
42 &self.fields
43 }
44
45 pub(crate) fn meta(&self) -> Meta {
48 self.meta
49 }
50
51 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 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#[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 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 pub fn shape(&self) -> &Arc<EnumShape> {
104 &self.shape
105 }
106
107 pub fn index(&self) -> usize {
109 #[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 pub fn constructor(&self) -> &Constructor {
121 &self.shape.constructors()[self.index()]
122 }
123
124 pub fn payload(&self) -> &[Value] {
126 &self.payload
127 }
128
129 pub(crate) fn meta(&self) -> Meta {
132 self.meta
133 }
134
135 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}