-
Notifications
You must be signed in to change notification settings - Fork 53
/
record.rs
429 lines (386 loc) · 13.3 KB
/
record.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
* Copyright 2019 The Starlark in Rust Authors.
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! A `record` type, comprising of a fixed set of fields.
//!
//! Calling `record()` produces a [`RecordType`]. Calling [`RecordType`] produces a [`Record`].
//! The field names of the record are only stored once, potentially reducing memory usage.
//! Created in Starlark using the `record()` function, which accepts keyword arguments.
//! The keys become field names, and values are the types. Calling the resulting
//! function produces an actual record.
//!
//! ```
//! # starlark::assert::is_true(r#"
//! IpAddress = record(host=str.type, port=int.type)
//! rec = IpAddress(host="localhost", port=80)
//! rec.port == 80
//! # "#);
//! ```
//!
//! It is also possible to use `field(type, default)` type to give defaults:
//!
//! ```
//! # starlark::assert::is_true(r#"
//! IpAddress = record(host=str.type, port=field(int.type, 80))
//! rec = IpAddress(host="localhost")
//! rec.port == 80
//! # "#);
//! ```
use std::cell::RefCell;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::hash::Hash;
use either::Either;
use gazebo::any::ProvidesStaticType;
use gazebo::cell::AsARef;
use gazebo::coerce::coerce;
use gazebo::coerce::Coerce;
use gazebo::display::display_keyed_container;
use gazebo::prelude::*;
use serde::Serialize;
use crate as starlark;
use crate::collections::Hashed;
use crate::collections::SmallMap;
use crate::collections::StarlarkHasher;
use crate::eval::Arguments;
use crate::eval::Evaluator;
use crate::eval::ParametersSpec;
use crate::values::comparison::equals_slice;
use crate::values::function::FUNCTION_TYPE;
use crate::values::typing::TypeCompiled;
use crate::values::Freeze;
use crate::values::Freezer;
use crate::values::FrozenValue;
use crate::values::Heap;
use crate::values::StarlarkValue;
use crate::values::Trace;
use crate::values::Value;
use crate::values::ValueLike;
/// The result of `field()`.
#[derive(
Clone,
Debug,
Dupe,
Trace,
Freeze,
NoSerialize,
ProvidesStaticType,
StarlarkDocs
)]
#[starlark_docs_attrs(builtin = "extension")]
pub struct FieldGen<V> {
pub(crate) typ: V,
default: Option<V>,
}
impl<V: Display> Display for FieldGen<V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "field(")?;
self.typ.fmt(f)?;
if let Some(d) = &self.default {
write!(f, ", ")?;
d.fmt(f)?;
}
write!(f, ")")
}
}
// Manual because no instance for Option<V>
unsafe impl<From: Coerce<To>, To> Coerce<FieldGen<To>> for FieldGen<From> {}
/// The result of `record()`, being the type of records.
#[derive(Debug, Trace, NoSerialize, ProvidesStaticType, StarlarkDocs)]
#[starlark_docs_attrs(builtin = "extension")]
pub struct RecordTypeGen<V, Typ> {
/// The name of this type, e.g. MyRecord
/// Either `Option<String>` or a `RefCell` thereof.
typ: Typ,
/// The V is the type the field must satisfy (e.g. `"string"`)
fields: SmallMap<String, (FieldGen<V>, TypeCompiled)>,
/// Creating these on every invoke is pretty expensive (profiling shows)
/// so compute them in advance and cache.
parameter_spec: ParametersSpec<FrozenValue>,
}
impl<V: Display, Typ> Display for RecordTypeGen<V, Typ> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
display_keyed_container(
f,
"record(",
")",
"=",
self.fields.iter().map(|(name, typ)| (name, &typ.0)),
)
}
}
/// Type of a record in a heap.
pub type RecordType<'v> = RecordTypeGen<Value<'v>, RefCell<Option<String>>>;
/// Type of a record in a frozen heap.
pub type FrozenRecordType = RecordTypeGen<FrozenValue, Option<String>>;
/// An actual record.
#[derive(Clone, Debug, Trace, Coerce, Freeze, ProvidesStaticType)]
#[repr(C)]
pub struct RecordGen<V> {
typ: V, // Must be RecordType
values: Vec<V>,
}
impl<'v, V: ValueLike<'v>> Display for RecordGen<V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
display_keyed_container(f, "record(", ")", "=", self.iter())
}
}
starlark_complex_value!(pub(crate) Field);
starlark_complex_values!(RecordType);
starlark_complex_value!(pub Record);
impl<V> FieldGen<V> {
pub(crate) fn new(typ: V, default: Option<V>) -> Self {
Self { typ, default }
}
}
fn record_fields<'v>(
x: Either<&'v RecordType<'v>, &'v FrozenRecordType>,
) -> &'v SmallMap<String, (FieldGen<Value<'v>>, TypeCompiled)> {
x.either(|x| &x.fields, |x| coerce(&x.fields))
}
impl<'v> RecordType<'v> {
pub(crate) fn new(fields: SmallMap<String, (FieldGen<Value<'v>>, TypeCompiled)>) -> Self {
let parameter_spec = Self::make_parameter_spec(&fields);
Self {
typ: RefCell::new(None),
fields,
parameter_spec,
}
}
fn make_parameter_spec(
fields: &SmallMap<String, (FieldGen<Value<'v>>, TypeCompiled)>,
) -> ParametersSpec<FrozenValue> {
let mut parameters = ParametersSpec::with_capacity("record".to_owned(), fields.len());
parameters.no_more_positional_args();
for (name, field) in fields {
if field.0.default.is_some() {
parameters.optional(name);
} else {
parameters.required(name);
}
}
parameters.finish()
}
}
impl<'v, V: ValueLike<'v>> RecordGen<V> {
/// `type(x)` for records.
pub const TYPE: &'static str = "record";
fn get_record_type(&self) -> Either<&'v RecordType<'v>, &'v FrozenRecordType> {
// Safe to unwrap because we always ensure typ is RecordType
RecordType::from_value(self.typ.to_value()).unwrap()
}
fn get_record_fields(&self) -> &'v SmallMap<String, (FieldGen<Value<'v>>, TypeCompiled)> {
record_fields(self.get_record_type())
}
/// Iterate over the elements in the record.
pub fn iter<'a>(&'a self) -> impl ExactSizeIterator<Item = (&'v str, V)> + 'a
where
'v: 'a,
{
self.get_record_fields()
.keys()
.map(String::as_str)
.zip(self.values.iter().copied())
}
}
impl<'v, V: ValueLike<'v> + 'v> StarlarkValue<'v> for FieldGen<V>
where
Self: ProvidesStaticType,
{
starlark_type!("field");
fn write_hash(&self, hasher: &mut StarlarkHasher) -> anyhow::Result<()> {
self.typ.write_hash(hasher)?;
self.default.is_some().hash(hasher);
if let Some(d) = self.default {
d.write_hash(hasher)?;
}
Ok(())
}
}
impl<'v> Freeze for RecordType<'v> {
type Frozen = FrozenRecordType;
fn freeze(self, freezer: &Freezer) -> anyhow::Result<Self::Frozen> {
let mut fields = SmallMap::with_capacity(self.fields.len());
for (k, t) in self.fields.into_iter_hashed() {
fields.insert_hashed(k, (t.0.freeze(freezer)?, t.1));
}
Ok(FrozenRecordType {
typ: self.typ.into_inner(),
fields,
parameter_spec: self.parameter_spec,
})
}
}
impl<'v, Typ: 'v, V: ValueLike<'v> + 'v> StarlarkValue<'v> for RecordTypeGen<V, Typ>
where
Self: ProvidesStaticType,
FieldGen<V>: ProvidesStaticType,
Typ: AsARef<Option<String>> + Debug,
{
starlark_type!(FUNCTION_TYPE);
fn write_hash(&self, hasher: &mut StarlarkHasher) -> anyhow::Result<()> {
for (name, typ) in &self.fields {
name.hash(hasher);
// No need to hash typ.1, since it was computed from typ.0
typ.0.write_hash(hasher)?;
}
Ok(())
}
fn invoke(
&self,
me: Value<'v>,
args: &Arguments<'v, '_>,
eval: &mut Evaluator<'v, '_>,
) -> anyhow::Result<Value<'v>> {
let this = me;
self.parameter_spec
.parser(args, eval, |mut param_parser, eval| {
let fields = record_fields(RecordType::from_value(this).unwrap());
let mut values = Vec::with_capacity(fields.len());
for (name, field) in fields.iter() {
match field.0.default {
None => {
let v: Value = param_parser.next(name)?;
v.check_type_compiled(field.0.typ, &field.1, Some(name))?;
values.push(v);
}
Some(default) => {
let v: Option<Value> = param_parser.next_opt(name)?;
match v {
None => values.push(default),
Some(v) => {
v.check_type_compiled(field.0.typ, &field.1, Some(name))?;
values.push(v);
}
}
}
}
}
Ok(eval.heap().alloc_complex(Record { typ: this, values }))
})
}
fn extra_memory(&self) -> usize {
// We don't capture the memory beneath the TypeCompiled, since we don't know how big
// those closures are.
let typ = AsARef::as_aref(&self.typ);
typ.as_ref().map_or(0, |s| s.capacity()) + self.fields.extra_memory()
}
fn dir_attr(&self) -> Vec<String> {
vec!["type".to_owned()]
}
fn has_attr(&self, attribute: &str, _heap: &'v Heap) -> bool {
attribute == "type"
}
fn get_attr(&self, attribute: &str, heap: &'v Heap) -> Option<Value<'v>> {
if attribute == "type" {
Some(
heap.alloc(
AsARef::as_aref(&self.typ)
.as_deref()
.unwrap_or(Record::TYPE),
),
)
} else {
None
}
}
fn equals(&self, other: Value<'v>) -> anyhow::Result<bool> {
fn eq<'v>(
a: &RecordTypeGen<impl ValueLike<'v>, impl AsARef<Option<String>>>,
b: &RecordTypeGen<impl ValueLike<'v>, impl AsARef<Option<String>>>,
) -> anyhow::Result<bool> {
if AsARef::as_aref(&a.typ) != AsARef::as_aref(&b.typ) {
return Ok(false);
};
if a.fields.len() != b.fields.len() {
return Ok(false);
};
for ((k1, t1), (k2, t2)) in a.fields.iter().zip(b.fields.iter()) {
// We require that the types and defaults are both equal.
if k1 != k2
|| !t1.0.typ.equals(t2.0.typ.to_value())?
|| t1.0.default.map(ValueLike::to_value)
!= t2.0.default.map(ValueLike::to_value)
{
return Ok(false);
}
}
Ok(true)
}
match RecordType::from_value(other) {
Some(Either::Left(other)) => eq(self, other),
Some(Either::Right(other)) => eq(self, other),
_ => Ok(false),
}
}
fn export_as(&self, variable_name: &str, _eval: &mut Evaluator<'v, '_>) {
if let Some(typ) = AsARef::as_ref_cell(&self.typ) {
let mut typ = typ.borrow_mut();
if typ.is_none() {
*typ = Some(variable_name.to_owned())
}
}
}
}
impl<'v, V: ValueLike<'v> + 'v> StarlarkValue<'v> for RecordGen<V>
where
Self: ProvidesStaticType,
{
starlark_type!(Record::TYPE);
fn matches_type(&self, ty: &str) -> bool {
if ty == Record::TYPE {
return true;
}
match self.get_record_type() {
Either::Left(x) => Some(ty) == x.typ.borrow().as_deref(),
Either::Right(x) => Some(ty) == x.typ.as_deref(),
}
}
fn equals(&self, other: Value<'v>) -> anyhow::Result<bool> {
match Record::from_value(other) {
Some(other) if self.typ.equals(other.typ)? => {
equals_slice(&self.values, &other.values, |x, y| x.equals(*y))
}
_ => Ok(false),
}
}
fn get_attr(&self, attribute: &str, heap: &'v Heap) -> Option<Value<'v>> {
self.get_attr_hashed(Hashed::new(attribute), heap)
}
fn get_attr_hashed(&self, attribute: Hashed<&str>, _heap: &'v Heap) -> Option<Value<'v>> {
let i = self.get_record_fields().get_index_of_hashed(attribute)?;
Some(self.values[i].to_value())
}
fn write_hash(&self, hasher: &mut StarlarkHasher) -> anyhow::Result<()> {
self.typ.write_hash(hasher)?;
for v in &self.values {
v.write_hash(hasher)?;
}
Ok(())
}
fn dir_attr(&self) -> Vec<String> {
self.get_record_fields().keys().cloned().collect()
}
}
impl<'v, V: ValueLike<'v>> Serialize for RecordGen<V> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_map(self.iter())
}
}