Skip to content

Commit 35f49ec

Browse files
committed
Make Array and Map immutable
This is done in order to try different optimizations. The idiomatic way to build such collections is either to use their typed equivalents or through `try_from_iter` and related constructors.
1 parent f3116d9 commit 35f49ec

File tree

10 files changed

+169
-716
lines changed

10 files changed

+169
-716
lines changed

engine/src/ast/field_expr.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ impl Expr for ComparisonExpr {
788788
mod tests {
789789
use super::*;
790790
use crate::{
791-
BytesFormat, FieldRef, LhsValue, ParserSettings,
791+
BytesFormat, FieldRef, LhsValue, ParserSettings, TypedMap,
792792
ast::{
793793
function_expr::{FunctionCallArgExpr, FunctionCallExpr},
794794
logical_expr::LogicalExpr,
@@ -1586,18 +1586,18 @@ mod tests {
15861586
let expr = expr.compile();
15871587
let ctx = &mut ExecutionContext::new(&SCHEME);
15881588

1589-
let headers = LhsValue::Map({
1590-
let mut map = Map::new(Type::Bytes);
1591-
map.insert(b"host", "example.org").unwrap();
1589+
let headers = LhsValue::from({
1590+
let mut map = TypedMap::new();
1591+
map.insert(b"host".to_vec().into(), "example.org");
15921592
map
15931593
});
15941594

15951595
ctx.set_field_value(field("http.headers"), headers).unwrap();
15961596
assert_eq!(expr.execute_one(ctx), false);
15971597

1598-
let headers = LhsValue::Map({
1599-
let mut map = Map::new(Type::Bytes);
1600-
map.insert(b"host", "abc.net.au").unwrap();
1598+
let headers = LhsValue::from({
1599+
let mut map = TypedMap::new();
1600+
map.insert(b"host".to_vec().into(), "abc.net.au");
16011601
map
16021602
});
16031603

@@ -2186,11 +2186,11 @@ mod tests {
21862186
let expr = expr.compile();
21872187
let ctx = &mut ExecutionContext::new(&SCHEME);
21882188

2189-
let headers = LhsValue::Map({
2190-
let mut map = Map::new(Type::Bytes);
2191-
map.insert(b"0", "one").unwrap();
2192-
map.insert(b"1", "two").unwrap();
2193-
map.insert(b"2", "three").unwrap();
2189+
let headers = LhsValue::from({
2190+
let mut map = TypedMap::new();
2191+
map.insert(b"0".to_vec().into(), "one");
2192+
map.insert(b"1".to_vec().into(), "two");
2193+
map.insert(b"2".to_vec().into(), "three");
21942194
map
21952195
});
21962196
ctx.set_field_value(field("http.headers"), headers).unwrap();
@@ -2267,11 +2267,11 @@ mod tests {
22672267
let expr = expr.compile();
22682268
let ctx = &mut ExecutionContext::new(&SCHEME);
22692269

2270-
let headers = LhsValue::Map({
2271-
let mut map = Map::new(Type::Bytes);
2272-
map.insert(b"0", "one").unwrap();
2273-
map.insert(b"1", "two").unwrap();
2274-
map.insert(b"2", "three").unwrap();
2270+
let headers = LhsValue::from({
2271+
let mut map = TypedMap::new();
2272+
map.insert(b"0".to_vec().into(), "one");
2273+
map.insert(b"1".to_vec().into(), "two");
2274+
map.insert(b"2".to_vec().into(), "three");
22752275
map
22762276
});
22772277
ctx.set_field_value(field("http.headers"), headers).unwrap();

engine/src/execution_context.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ fn test_scheme_mismatch() {
425425

426426
#[test]
427427
fn test_serde() {
428-
use crate::lhs_types::{Array, Map};
429-
use crate::types::Type;
428+
use crate::lhs_types::{Array, TypedMap};
430429
use std::net::IpAddr;
431430
use std::str::FromStr;
432431

@@ -491,9 +490,9 @@ fn test_serde() {
491490

492491
assert_eq!(
493492
ctx.set_field_value(scheme.get_field("map").unwrap(), {
494-
let mut map = Map::new(Type::Int);
495-
map.insert(b"leet", 1337).unwrap();
496-
map.insert(b"tabs", 25).unwrap();
493+
let mut map = TypedMap::<i64>::new();
494+
map.insert(b"leet".to_vec().into(), 1337);
495+
map.insert(b"tabs".to_vec().into(), 25);
497496
map
498497
}),
499498
Ok(None),
@@ -535,16 +534,16 @@ fn test_serde() {
535534

536535
assert_eq!(
537536
ctx.set_field_value(scheme.get_field("map").unwrap(), {
538-
let mut map = Map::new(Type::Int);
539-
map.insert(b"leet", 1337).unwrap();
540-
map.insert(b"tabs", 25).unwrap();
541-
map.insert(b"a\xFF\xFFb", 17).unwrap();
537+
let mut map = TypedMap::<i64>::new();
538+
map.insert(b"leet".to_vec().into(), 1337);
539+
map.insert(b"tabs".to_vec().into(), 25);
540+
map.insert(b"a\xFF\xFFb".to_vec().into(), 17);
542541
map
543542
}),
544543
Ok(Some({
545-
let mut map = Map::new(Type::Int);
546-
map.insert(b"leet", 1337).unwrap();
547-
map.insert(b"tabs", 25).unwrap();
544+
let mut map = TypedMap::<i64>::new();
545+
map.insert(b"leet".to_vec().into(), 1337);
546+
map.insert(b"tabs".to_vec().into(), 25);
548547
map.into()
549548
})),
550549
);

engine/src/lhs_types/array.rs

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
use crate::{
22
lhs_types::AsRefIterator,
3-
types::{
4-
CompoundType, GetType, IntoValue, LhsValue, LhsValueMut, LhsValueSeed, Type,
5-
TypeMismatchError,
6-
},
3+
types::{CompoundType, GetType, IntoValue, LhsValue, LhsValueSeed, Type, TypeMismatchError},
74
};
85
use serde::{
96
Serialize, Serializer,
@@ -53,11 +50,6 @@ impl<'a> InnerArray<'a> {
5350
self.as_vec().get_mut(idx)
5451
}
5552

56-
#[inline]
57-
fn insert(&mut self, idx: usize, value: LhsValue<'a>) {
58-
self.as_vec().insert(idx, value)
59-
}
60-
6153
#[inline]
6254
fn push(&mut self, value: LhsValue<'a>) {
6355
self.as_vec().push(value)
@@ -121,43 +113,6 @@ impl<'a> Array<'a> {
121113
self.data.get(idx)
122114
}
123115

124-
/// Get a mutable reference to an element if it exists
125-
pub fn get_mut(&mut self, idx: usize) -> Option<LhsValueMut<'_, 'a>> {
126-
self.data.get_mut(idx).map(LhsValueMut::from)
127-
}
128-
129-
/// Inserts an element at index `idx`
130-
pub fn insert(
131-
&mut self,
132-
idx: usize,
133-
value: impl Into<LhsValue<'a>>,
134-
) -> Result<(), TypeMismatchError> {
135-
let value = value.into();
136-
let value_type = value.get_type();
137-
if value_type != self.val_type.into() {
138-
return Err(TypeMismatchError {
139-
expected: Type::from(self.val_type).into(),
140-
actual: value_type,
141-
});
142-
}
143-
self.data.insert(idx, value);
144-
Ok(())
145-
}
146-
147-
/// Push an element to the back of the array
148-
pub fn push(&mut self, value: impl Into<LhsValue<'a>>) -> Result<(), TypeMismatchError> {
149-
let value = value.into();
150-
let value_type = value.get_type();
151-
if value_type != self.val_type.into() {
152-
return Err(TypeMismatchError {
153-
expected: Type::from(self.val_type).into(),
154-
actual: value_type,
155-
});
156-
}
157-
self.data.push(value);
158-
Ok(())
159-
}
160-
161116
pub(crate) fn as_ref(&'a self) -> Array<'a> {
162117
Array {
163118
val_type: self.val_type,
@@ -445,51 +400,6 @@ impl<'de> DeserializeSeed<'de> for &mut Array<'de> {
445400
}
446401
}
447402

448-
/// Wrapper type around mutable `Array` to prevent
449-
/// illegal operations like changing the type of
450-
/// its values.
451-
pub struct ArrayMut<'a, 'b>(&'a mut Array<'b>);
452-
453-
impl<'a, 'b> ArrayMut<'a, 'b> {
454-
/// Push an element to the back of the array
455-
#[inline]
456-
pub fn push(&mut self, value: impl Into<LhsValue<'b>>) -> Result<(), TypeMismatchError> {
457-
self.0.push(value)
458-
}
459-
460-
/// Inserts an element at index `idx`
461-
#[inline]
462-
pub fn insert(
463-
&mut self,
464-
idx: usize,
465-
value: impl Into<LhsValue<'b>>,
466-
) -> Result<(), TypeMismatchError> {
467-
self.0.insert(idx, value)
468-
}
469-
470-
/// Get a mutable reference to an element if it exists
471-
#[inline]
472-
pub fn get_mut(&'a mut self, idx: usize) -> Option<LhsValueMut<'a, 'b>> {
473-
self.0.get_mut(idx)
474-
}
475-
}
476-
477-
impl<'b> Deref for ArrayMut<'_, 'b> {
478-
type Target = Array<'b>;
479-
480-
#[inline]
481-
fn deref(&self) -> &Self::Target {
482-
self.0
483-
}
484-
}
485-
486-
impl<'a, 'b> From<&'a mut Array<'b>> for ArrayMut<'a, 'b> {
487-
#[inline]
488-
fn from(arr: &'a mut Array<'b>) -> Self {
489-
Self(arr)
490-
}
491-
}
492-
493403
/// Typed wrapper over an `Array` which provides
494404
/// infaillible operations.
495405
#[repr(transparent)]
@@ -703,11 +613,11 @@ mod tests {
703613

704614
#[test]
705615
fn test_borrowed_eq_owned() {
706-
let mut owned = Array::new(Type::Bytes);
616+
let mut arr = TypedArray::new();
617+
618+
arr.push("borrowed");
707619

708-
owned
709-
.push(LhsValue::Bytes("borrowed".as_bytes().into()))
710-
.unwrap();
620+
let owned = Array::from(arr);
711621

712622
let borrowed = owned.as_ref();
713623

0 commit comments

Comments
 (0)