Skip to content

Commit

Permalink
Add impl for unit types; Fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Mar 25, 2024
1 parent ab5aa61 commit 9680f9f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
8 changes: 5 additions & 3 deletions proc/src/tl_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ fn build_enum(container: &ast::Container, variants: &[ast::Variant]) -> TokenStr
container,
variants,
|variant, field| {
variant.attrs.size_hint.is_none() && field.attrs.size_hint.is_none()
!field.attrs.flags
&& variant.attrs.size_hint.is_none()
&& field.attrs.size_hint.is_none()
},
|destructed, variant| {
let body = build_max_size_hint(
Expand All @@ -97,7 +99,7 @@ fn build_enum(container: &ast::Container, variants: &[ast::Variant]) -> TokenStr
let variants = map_variants(
container,
variants,
|_, _| true,
|_, field| !field.attrs.flags,
|destructed, variant| {
let body = build_write_to(
&variant.fields,
Expand Down Expand Up @@ -177,7 +179,7 @@ where
}
ast::Style::Tuple => {
let fields = variant.fields.iter().enumerate().map(|(i, field)| {
if field.attrs.skip_write {
if field.attrs.skip_write || !filter_fields(variant, field) {
quote! { _ }
} else {
quote::format_ident!("field_{}", i).to_token_stream()
Expand Down
24 changes: 24 additions & 0 deletions src/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
use crate::traits::*;
use crate::util::*;

impl TlRead<'_> for () {
type Repr = Bare;

fn read_from(_packet: &'_ [u8], _offset: &mut usize) -> TlResult<Self> {
Ok(())
}
}

impl TlWrite for () {
type Repr = Bare;

#[inline]
fn max_size_hint(&self) -> usize {
0
}

#[inline]
fn write_to<P>(&self, _packet: &mut P)
where
P: TlPacket,
{
}
}

impl TlRead<'_> for bool {
type Repr = Boxed;

Expand Down
21 changes: 17 additions & 4 deletions test_suite/tests/tl_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ mod tests {
flags: (),
#[tl(flags_bit = 0)]
value_1: Option<u32>,
#[tl(flags_bit = 1)]
value_2: Option<()>,
#[tl(flags_bit = 31)]
value_2: Option<bool>,
value_3: Option<bool>,
}

#[derive(TlRead)]
Expand Down Expand Up @@ -116,12 +118,23 @@ mod tests {
}

let target = [
0, 0, 0, 0x80, // flags
181, 117, 114, 153, // value_2: Some
0, 0, 0, 0x80, // flags (little-endian)
181, 117, 114, 153, // value_3: Some
];
let data: StructWithFlags = tl_proto::deserialize(&target).unwrap();
assert_eq!(data.value_1, None);
assert_eq!(data.value_2, Some(true));
assert_eq!(data.value_2, None);
assert_eq!(data.value_3, Some(true));

let target = [
0b11, 0, 0, 0x80, // flags (little-endian)
123, 0, 0, 0, // value_2: Some
181, 117, 114, 153, // value_3: Some
];
let data: StructWithFlags = tl_proto::deserialize(&target).unwrap();
assert_eq!(data.value_1, Some(123));
assert_eq!(data.value_2, Some(()));
assert_eq!(data.value_3, Some(true));

let target = [
0u8, 0, 0, 0b01000000, // flags
Expand Down
13 changes: 7 additions & 6 deletions test_suite/tests/tl_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ mod tests {
flags: (),
#[tl(flags_bit = 0)]
value_1: Option<u32>,
#[tl(flags_bit = 1)]
value_2: Option<()>,
#[tl(flags_bit = "flags.31")]
value_2: Option<bool>,
value_3: Option<bool>,
}

#[derive(TlWrite)]
enum EnumWithFlags {
Named {
#[tl(flags)]
#[allow(unused)]
flags: (),
#[tl(flags_bit = 0)]
value_1: Option<u32>,
Expand All @@ -115,7 +116,6 @@ mod tests {
#[derive(TlWrite)]
struct StructWithMultipleFlags {
#[tl(flags, default_flags = 0x40000000)]
#[allow(unused)]
flags: (),
#[tl(flags_bit = "flags.0")]
value_0: Option<u32>,
Expand Down Expand Up @@ -207,12 +207,13 @@ mod tests {
let object = StructWithFlags {
flags: (),
value_1: None,
value_2: Some(true),
value_2: Some(()),
value_3: Some(true),
};
assert_eq!(object.max_size_hint(), 4 + 4);
let target = [
0u8, 0, 0, 0b11000000, // flags
181, 117, 114, 153, // value_2: Some
2, 0, 0, 0b11000000, // flags
181, 117, 114, 153, // value_3: Some
];
let data = tl_proto::serialize(object);
assert_eq!(&data, &target);
Expand Down

0 comments on commit 9680f9f

Please sign in to comment.