Skip to content

Commit

Permalink
feat: add non-zero collection types
Browse files Browse the repository at this point in the history
This expands from non-zero integer values to non-zero collections,
meaning `non_zero<T>` specializations where `T` is one of `string`,
`bytes`, `vec<T>`, `hash_map<K, V>` or `hash_set<T>`.

Any other `non_zero<T>` types are now refused as there is no clear way
of defining what non-zero means for other types (or at least not a
single _correct_ way).

Also, snapshot tests includes missing adjustments from the last commits
that introduced further `#[allow(...)]` attributes in the Rust
implementation.
  • Loading branch information
dnaka91 committed Oct 20, 2023
1 parent 21b2f69 commit e0fc43e
Show file tree
Hide file tree
Showing 28 changed files with 963 additions and 54 deletions.
26 changes: 21 additions & 5 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ fn compile_data_type(ty: &DataType<'_>) -> TokenStream {
let ty = compile_data_type(ty);
quote! { ::stef::buf::decode_option(r, |r| { #ty }) }
}
DataType::NonZero(ty) => match **ty {
DataType::NonZero(ty) => match &**ty {
DataType::U8 => quote! { ::std::num::NonZeroU8::decode(r) },
DataType::U16 => quote! { ::std::num::NonZeroU16::decode(r) },
DataType::U32 => quote! { ::std::num::NonZeroU32::decode(r) },
Expand All @@ -280,10 +280,26 @@ fn compile_data_type(ty: &DataType<'_>) -> TokenStream {
DataType::I32 => quote! { ::std::num::NonZeroI32::decode(r) },
DataType::I64 => quote! { ::std::num::NonZeroI64::decode(r) },
DataType::I128 => quote! { ::std::num::NonZeroI128::decode(r) },
_ => quote! {
let _r = r;
todo!();
},
DataType::String | DataType::StringRef => {
quote! { ::stef::buf::decode_non_zero_string(r) }
}
DataType::Bytes | DataType::BytesRef => {
quote! { ::stef::buf::decode_non_zero_bytes(r) }
}
DataType::Vec(ty) => {
let ty = compile_data_type(ty);
quote! { ::stef::buf::decode_non_zero_vec(r, |r| { #ty }) }
}
DataType::HashMap(kv) => {
let ty_k = compile_data_type(&kv.0);
let ty_v = compile_data_type(&kv.1);
quote! { ::stef::buf::decode_non_zero_hash_map(r, |r| { #ty_k }, |r| { #ty_v }) }
}
DataType::HashSet(ty) => {
let ty = compile_data_type(ty);
quote! { ::stef::buf::decode_non_zero_hash_set(r, |r| { #ty }) }
}
ty => todo!("compiler should catch invalid {ty:?} type"),
},
DataType::BoxString => quote! { Box::<str>::decode(r) },
DataType::BoxBytes => quote! { Box::<[u8]>::decode(r) },
Expand Down
25 changes: 22 additions & 3 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ pub(super) fn compile_data_type(ty: &DataType<'_>) -> TokenStream {
let ty = compile_data_type(ty);
quote! { Option<#ty> }
}
DataType::NonZero(ty) => match **ty {
DataType::NonZero(ty) => match &**ty {
DataType::U8 => quote! { ::std::num::NonZeroU8 },
DataType::U16 => quote! { ::std::num::NonZeroU16 },
DataType::U32 => quote! { ::std::num::NonZeroU32 },
Expand All @@ -296,7 +296,22 @@ pub(super) fn compile_data_type(ty: &DataType<'_>) -> TokenStream {
DataType::I32 => quote! { ::std::num::NonZeroI32 },
DataType::I64 => quote! { ::std::num::NonZeroI64 },
DataType::I128 => quote! { ::std::num::NonZeroI128 },
_ => compile_data_type(ty),
DataType::String | DataType::StringRef => quote! { ::stef::NonZeroString },
DataType::Bytes | DataType::BytesRef => quote! { ::stef::NonZeroBytes },
DataType::Vec(ty) => {
let ty = compile_data_type(ty);
quote! { ::stef::NonZeroVec<#ty> }
}
DataType::HashMap(kv) => {
let k = compile_data_type(&kv.0);
let v = compile_data_type(&kv.1);
quote! { ::stef::NonZeroHashMap<#k, #v> }
}
DataType::HashSet(ty) => {
let ty = compile_data_type(ty);
quote! { ::stef::NonZeroHashSet<#ty> }
}
ty => todo!("compiler should catch invalid {ty:?} type"),
},
DataType::BoxString => quote! { Box<str> },
DataType::BoxBytes => quote! { Box<[u8]> },
Expand Down Expand Up @@ -421,7 +436,11 @@ mod tests {
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
39 changes: 20 additions & 19 deletions crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,25 +307,26 @@ fn compile_data_type(ty: &DataType<'_>, name: TokenStream) -> TokenStream {
);
quote! { ::stef::buf::encode_option(w, &#name, |w, v| { #ty; }) }
}
DataType::NonZero(ty) => {
if matches!(
**ty,
DataType::U8
| DataType::U16
| DataType::U32
| DataType::U64
| DataType::U128
| DataType::I8
| DataType::I16
| DataType::I32
| DataType::I64
| DataType::I128
) {
quote! { (#name).encode(w) }
} else {
compile_data_type(ty, name)
}
}
DataType::NonZero(ty) => match &**ty {
DataType::U8
| DataType::U16
| DataType::U32
| DataType::U64
| DataType::U128
| DataType::I8
| DataType::I16
| DataType::I32
| DataType::I64
| DataType::I128 => quote! { (#name).encode(w) },
DataType::String
| DataType::StringRef
| DataType::Bytes
| DataType::BytesRef
| DataType::Vec(_)
| DataType::HashMap(_)
| DataType::HashSet(_) => compile_data_type(ty, quote! { #name.get() }),
ty => todo!("compiler should catch invalid {ty:?} type"),
},

DataType::BoxString => quote! { ::stef::buf::encode_string(w, &*#name) },
DataType::BoxBytes => quote! { ::stef::buf::encode_bytes(w, &*#name) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use ::stef::buf::{Decode, Encode};
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use ::stef::buf::{Decode, Encode};
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use ::stef::buf::{Decode, Encode};
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use ::stef::buf::{Decode, Encode};
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ use ::stef::buf::{Decode, Encode};
pub struct Sample;
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {}
}
#[automatically_derived]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ pub mod a {
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ pub struct SampleStruct {
}
#[automatically_derived]
impl ::stef::Encode for SampleStruct {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ pub struct Sample {
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ where
K: ::stef::buf::Encode,
V: ::stef::buf::Encode,
{
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ where
A: ::stef::buf::Encode,
B: ::stef::buf::Encode,
{
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ impl<T> ::stef::Encode for Sample<T>
where
T: ::stef::buf::Encode,
{
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use ::stef::buf::{Decode, Encode};
pub struct Sample(u32, bool);
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.0) });
::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_bool(w, self.1) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ pub struct Sample {
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ pub struct Sample {
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ input_file: crates/stef-parser/tests/inputs/types-nested.stef
use ::stef::buf::{Decode, Encode};
#[derive(Clone, Debug, PartialEq)]
pub struct Sample {
pub value: Vec<Option<::std::collections::HashMap<i64, Box<str>>>>,
pub value: Vec<Option<::stef::NonZeroHashMap<i64, Box<str>>>>,
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::needless_borrow, clippy::explicit_auto_deref)]
#[allow(
clippy::borrow_deref_ref,
clippy::explicit_auto_deref,
clippy::needless_borrow,
)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
::stef::buf::encode_field(
w,
Expand All @@ -27,7 +31,7 @@ impl ::stef::Encode for Sample {
|w, v| {
::stef::buf::encode_hash_map(
w,
&v,
&v.get(),
|w, k| {
::stef::buf::encode_i64(w, *k);
},
Expand All @@ -48,7 +52,7 @@ impl ::stef::Encode for Sample {
impl ::stef::Decode for Sample {
#[allow(clippy::type_complexity)]
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
let mut value: Option<Vec<Option<::std::collections::HashMap<i64, Box<str>>>>> = None;
let mut value: Option<Vec<Option<::stef::NonZeroHashMap<i64, Box<str>>>>> = None;
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
Expand All @@ -60,8 +64,11 @@ impl ::stef::Decode for Sample {
::stef::buf::decode_option(
r,
|r| {
let _r = r;
todo!();
::stef::buf::decode_non_zero_hash_map(
r,
|r| { ::stef::buf::decode_i64(r) },
|r| { Box::<str>::decode(r) },
)
},
)
},
Expand Down
Loading

0 comments on commit e0fc43e

Please sign in to comment.