Skip to content

Commit 1501209

Browse files
committed
Introduce custom Bytes type
Instead of relying on `Cow<'_, [u8]>`. It makes the code easier to read but also allows us to implement the various traits we need the way we want them to. And it also makes experimenting with different represention more easier.
1 parent afa9568 commit 1501209

File tree

8 files changed

+416
-72
lines changed

8 files changed

+416
-72
lines changed

engine/benches/bench.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::alloc::System;
66
static A: System = System;
77

88
use criterion::{Bencher, Criterion, criterion_group, criterion_main};
9-
use std::{borrow::Cow, clone::Clone, fmt::Debug, net::IpAddr};
9+
use std::{clone::Clone, fmt::Debug, net::IpAddr};
1010
use wirefilter::{
11-
ExecutionContext, FilterAst, FunctionArgs, GetType, LhsValue, SchemeBuilder,
11+
Bytes, ExecutionContext, FilterAst, FunctionArgs, GetType, LhsValue, SchemeBuilder,
1212
SimpleFunctionArgKind, SimpleFunctionDefinition, SimpleFunctionImpl, SimpleFunctionParam, Type,
1313
};
1414

@@ -17,7 +17,7 @@ fn lowercase<'a>(args: FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> {
1717
match input {
1818
LhsValue::Bytes(mut bytes) => {
1919
let make_lowercase = match bytes {
20-
Cow::Borrowed(bytes) => bytes.iter().any(u8::is_ascii_uppercase),
20+
Bytes::Borrowed(bytes) => bytes.iter().any(u8::is_ascii_uppercase),
2121
_ => true,
2222
};
2323
if make_lowercase {
@@ -34,7 +34,7 @@ fn uppercase<'a>(args: FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> {
3434
match input {
3535
LhsValue::Bytes(mut bytes) => {
3636
let make_uppercase = match bytes {
37-
Cow::Borrowed(bytes) => bytes.iter().any(u8::is_ascii_lowercase),
37+
Bytes::Borrowed(bytes) => bytes.iter().any(u8::is_ascii_lowercase),
3838
_ => true,
3939
};
4040
if make_uppercase {

engine/src/ast/function_expr.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,13 +562,10 @@ mod tests {
562562
}
563563

564564
fn lower_function<'a>(args: FunctionArgs<'_, 'a>) -> Option<LhsValue<'a>> {
565-
use std::borrow::Cow;
566-
567565
match args.next()? {
568566
Ok(LhsValue::Bytes(mut b)) => {
569-
let mut text: Vec<u8> = b.to_mut().to_vec();
570-
text.make_ascii_lowercase();
571-
Some(LhsValue::Bytes(Cow::Owned(text)))
567+
b.to_mut().make_ascii_lowercase();
568+
Some(LhsValue::Bytes(b))
572569
}
573570
Err(Type::Bytes) => None,
574571
_ => unreachable!(),

engine/src/functions/concat.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
2-
Array, ExpectedType, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
2+
Array, Bytes, ExpectedType, FunctionArgs, FunctionDefinition, FunctionDefinitionContext,
33
FunctionParam, FunctionParamError, GetType, LhsValue, ParserSettings, Type,
44
};
5-
use std::{borrow::Cow, iter::once};
5+
use std::iter::once;
66

77
/// A function which, given one or more arrays or byte-strings, returns the
88
/// concatenation of each of them.
@@ -43,15 +43,15 @@ fn concat_array<'a>(accumulator: Array<'a>, args: FunctionArgs<'_, 'a>) -> Array
4343
Array::try_from_vec(val_type, vec).unwrap()
4444
}
4545

46-
fn concat_bytes<'a>(mut accumulator: Cow<'a, [u8]>, args: FunctionArgs<'_, 'a>) -> Cow<'a, [u8]> {
46+
fn concat_bytes<'a>(mut accumulator: Vec<u8>, args: FunctionArgs<'_, 'a>) -> Bytes<'a> {
4747
for arg in args {
4848
match arg {
49-
Ok(LhsValue::Bytes(value)) => accumulator.to_mut().extend(value.iter()),
49+
Ok(LhsValue::Bytes(value)) => accumulator.extend_from_slice(&value),
5050
Err(Type::Bytes) => (),
5151
_ => (),
5252
}
5353
}
54-
accumulator
54+
accumulator.into()
5555
}
5656

5757
pub(crate) const EXPECTED_TYPES: [ExpectedType; 2] =
@@ -103,7 +103,10 @@ impl FunctionDefinition for ConcatFunction {
103103
return Some(LhsValue::Array(concat_array(array, args)));
104104
}
105105
Ok(LhsValue::Bytes(bytes)) => {
106-
return Some(LhsValue::Bytes(concat_bytes(bytes, args)));
106+
return Some(LhsValue::Bytes(concat_bytes(
107+
bytes.into_owned().into(),
108+
args,
109+
)));
107110
}
108111
Err(_) => (),
109112
_ => unreachable!(),
@@ -124,27 +127,27 @@ mod tests {
124127
#[test]
125128
fn test_concat_bytes() {
126129
let mut args = vec![
127-
Ok(LhsValue::Bytes(Cow::Borrowed(b"hello"))),
128-
Ok(LhsValue::Bytes(Cow::Borrowed(b"world"))),
130+
Ok(LhsValue::Bytes(Bytes::Borrowed(b"hello"))),
131+
Ok(LhsValue::Bytes(Bytes::Borrowed(b"world"))),
129132
]
130133
.into_iter();
131134
assert_eq!(
132-
Some(LhsValue::Bytes(Cow::Borrowed(b"helloworld"))),
135+
Some(LhsValue::Bytes(Bytes::Borrowed(b"helloworld"))),
133136
CONCAT_FN.compile(&mut std::iter::empty(), None)(&mut args)
134137
);
135138
}
136139

137140
#[test]
138141
fn test_concat_many_bytes() {
139142
let mut args = vec![
140-
Ok(LhsValue::Bytes(Cow::Borrowed(b"hello"))),
141-
Ok(LhsValue::Bytes(Cow::Borrowed(b"world"))),
142-
Ok(LhsValue::Bytes(Cow::Borrowed(b"hello2"))),
143-
Ok(LhsValue::Bytes(Cow::Borrowed(b"world2"))),
143+
Ok(LhsValue::Bytes(Bytes::Borrowed(b"hello"))),
144+
Ok(LhsValue::Bytes(Bytes::Borrowed(b"world"))),
145+
Ok(LhsValue::Bytes(Bytes::Borrowed(b"hello2"))),
146+
Ok(LhsValue::Bytes(Bytes::Borrowed(b"world2"))),
144147
]
145148
.into_iter();
146149
assert_eq!(
147-
Some(LhsValue::Bytes(Cow::Borrowed(b"helloworldhello2world2"))),
150+
Some(LhsValue::Bytes(Bytes::Borrowed(b"helloworldhello2world2"))),
148151
CONCAT_FN.compile(&mut std::iter::empty(), None)(&mut args)
149152
);
150153
}

0 commit comments

Comments
 (0)