Skip to content

Commit c5aec56

Browse files
committed
Remove length hint from FixedByteSize trait
1 parent e7533a0 commit c5aec56

File tree

4 files changed

+37
-67
lines changed

4 files changed

+37
-67
lines changed

procedural/src/byte/fixed_size.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,21 @@ pub fn derive_fixed_byte_size_struct(data_struct: DataStruct, generics: Generics
1515
syn::Fields::Unit => panic!("unit types are not supported"),
1616
};
1717

18-
let length_hints = fields.into_iter().map(|mut field| {
18+
let sizes = fields.into_iter().zip(types.iter()).map(|(mut field, field_type)| {
1919
get_unique_attribute(&mut field.attrs, "length_hint")
2020
.map(|attribute| match attribute.meta {
2121
syn::Meta::List(list) => list.tokens,
2222
syn::Meta::Path(_) | syn::Meta::NameValue(_) => panic!("expected token stream in attribute"),
2323
})
24-
.map(|length_hint| quote!(Some(((#length_hint) as usize))))
25-
.unwrap_or(quote!(None))
24+
.map(|length_hint| quote!((#length_hint) as usize))
25+
.unwrap_or(quote!(<#field_type as crate::loaders::FixedByteSize>::size_in_bytes()))
2626
});
2727

2828
quote! {
2929
impl #impl_generics const crate::loaders::FixedByteSize for #name #type_generics #where_clause {
30-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
31-
assert!(length_hint.is_none());
32-
30+
fn size_in_bytes() -> usize {
3331
let mut total = 0;
34-
#(total += <#types as crate::loaders::FixedByteSize>::size_in_bytes(#length_hints);)*
32+
#(total += #sizes;)*
3533
total
3634
}
3735
}
@@ -48,10 +46,8 @@ pub fn derive_fixed_byte_size_enum(generics: Generics, mut attributes: Vec<Attri
4846

4947
quote! {
5048
impl #impl_generics const crate::loaders::FixedByteSize for #name #type_generics #where_clause {
51-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
52-
assert!(length_hint.is_none());
53-
54-
<#numeric_type as crate::loaders::FixedByteSize>::size_in_bytes(None)
49+
fn size_in_bytes() -> usize {
50+
<#numeric_type as crate::loaders::FixedByteSize>::size_in_bytes()
5551
}
5652
}
5753
}

procedural/src/byte/helper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn byte_convertable_helper(data_struct: DataStruct) -> (Vec<TokenStream>, Ve
125125
.expect("repeating_remaining is used but no packet_length attribute is set");
126126

127127
quote!({
128-
let repeat_count = (#packet_length - ((byte_stream.get_offset() - base_offset) as u16) - 2) / (<#field_type as crate::loaders::FixedByteSizeWrapper>::size_in_bytes(None) as u16);
128+
let repeat_count = (#packet_length - ((byte_stream.get_offset() - base_offset) as u16) - 2) / (<#field_type as crate::loaders::FixedByteSizeWrapper>::size_in_bytes() as u16);
129129
// TODO: Add check to make sure this allocation is not too big.
130130
let mut vector = Vec::with_capacity(repeat_count as usize);
131131

src/loaders/archive/native/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub struct NativeArchive {
3333

3434
const MAGIC_BYTES: &[u8] = b"Master of Magic\0";
3535
const UNPACKED_SIZE_OF_MAGIC_STRING: usize = MAGIC_BYTES.len();
36-
const UNPACKED_SIZE_OF_ARCHIVEHEADER: usize = Header::size_in_bytes(None);
37-
const UNPACKED_SIZE_OF_FILETABLE: usize = AssetTable::size_in_bytes(None);
36+
const UNPACKED_SIZE_OF_ARCHIVEHEADER: usize = Header::size_in_bytes();
37+
const UNPACKED_SIZE_OF_FILETABLE: usize = AssetTable::size_in_bytes();
3838

3939
impl Archive for NativeArchive {
4040
fn from_path(path: &Path) -> Self {

src/loaders/fixed.rs

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,144 +4,118 @@ use cgmath::{Matrix3, Quaternion, Vector2, Vector3, Vector4};
44

55
#[const_trait]
66
pub trait FixedByteSizeWrapper {
7-
fn size_in_bytes(length_hint: Option<usize>) -> usize;
7+
fn size_in_bytes() -> usize;
88
}
99

1010
impl<T: ~const FixedByteSize> const FixedByteSizeWrapper for Vec<T> {
11-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
12-
T::size_in_bytes(length_hint)
11+
fn size_in_bytes() -> usize {
12+
T::size_in_bytes()
1313
}
1414
}
1515

1616
#[const_trait]
1717
pub trait FixedByteSize {
18-
fn size_in_bytes(length_hint: Option<usize>) -> usize;
18+
fn size_in_bytes() -> usize;
1919
}
2020

2121
impl const FixedByteSize for u8 {
22-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
23-
assert!(length_hint.is_none());
22+
fn size_in_bytes() -> usize {
2423
core::mem::size_of::<Self>()
2524
}
2625
}
2726

2827
impl const FixedByteSize for u16 {
29-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
30-
assert!(length_hint.is_none());
28+
fn size_in_bytes() -> usize {
3129
core::mem::size_of::<Self>()
3230
}
3331
}
3432

3533
impl const FixedByteSize for u32 {
36-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
37-
assert!(length_hint.is_none());
34+
fn size_in_bytes() -> usize {
3835
core::mem::size_of::<Self>()
3936
}
4037
}
4138

4239
impl const FixedByteSize for u64 {
43-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
44-
assert!(length_hint.is_none());
40+
fn size_in_bytes() -> usize {
4541
core::mem::size_of::<Self>()
4642
}
4743
}
4844

4945
impl const FixedByteSize for i8 {
50-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
51-
assert!(length_hint.is_none());
46+
fn size_in_bytes() -> usize {
5247
core::mem::size_of::<Self>()
5348
}
5449
}
5550

5651
impl const FixedByteSize for i16 {
57-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
58-
assert!(length_hint.is_none());
52+
fn size_in_bytes() -> usize {
5953
core::mem::size_of::<Self>()
6054
}
6155
}
6256

6357
impl const FixedByteSize for i32 {
64-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
65-
assert!(length_hint.is_none());
58+
fn size_in_bytes() -> usize {
6659
core::mem::size_of::<Self>()
6760
}
6861
}
6962

7063
impl const FixedByteSize for i64 {
71-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
72-
assert!(length_hint.is_none());
64+
fn size_in_bytes() -> usize {
7365
core::mem::size_of::<Self>()
7466
}
7567
}
7668

7769
impl const FixedByteSize for f32 {
78-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
79-
assert!(length_hint.is_none());
70+
fn size_in_bytes() -> usize {
8071
core::mem::size_of::<Self>()
8172
}
8273
}
8374

8475
impl const FixedByteSize for f64 {
85-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
86-
assert!(length_hint.is_none());
76+
fn size_in_bytes() -> usize {
8777
core::mem::size_of::<Self>()
8878
}
8979
}
9080

9181
impl<T: ~const FixedByteSize, const SIZE: usize> const FixedByteSize for [T; SIZE] {
92-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
93-
assert!(length_hint.is_none());
94-
T::size_in_bytes(None) * SIZE
82+
fn size_in_bytes() -> usize {
83+
T::size_in_bytes() * SIZE
9584
}
9685
}
9786

9887
impl<T: ~const FixedByteSize> const FixedByteSize for Vector2<T> {
99-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
100-
assert!(length_hint.is_none());
101-
T::size_in_bytes(None) * 2
88+
fn size_in_bytes() -> usize {
89+
T::size_in_bytes() * 2
10290
}
10391
}
10492

10593
impl<T: ~const FixedByteSize> const FixedByteSize for Vector3<T> {
106-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
107-
assert!(length_hint.is_none());
108-
T::size_in_bytes(None) * 3
94+
fn size_in_bytes() -> usize {
95+
T::size_in_bytes() * 3
10996
}
11097
}
11198

11299
impl<T: ~const FixedByteSize> const FixedByteSize for Vector4<T> {
113-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
114-
assert!(length_hint.is_none());
115-
T::size_in_bytes(None) * 4
100+
fn size_in_bytes() -> usize {
101+
T::size_in_bytes() * 4
116102
}
117103
}
118104

119105
impl<T: ~const FixedByteSize> const FixedByteSize for Quaternion<T> {
120-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
121-
assert!(length_hint.is_none());
122-
T::size_in_bytes(None) * 4
106+
fn size_in_bytes() -> usize {
107+
T::size_in_bytes() * 4
123108
}
124109
}
125110

126111
impl<T: ~const FixedByteSize> const FixedByteSize for Matrix3<T> {
127-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
128-
assert!(length_hint.is_none());
129-
T::size_in_bytes(None) * 9
112+
fn size_in_bytes() -> usize {
113+
T::size_in_bytes() * 9
130114
}
131115
}
132116

133117
impl const FixedByteSize for Ipv4Addr {
134-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
135-
assert!(length_hint.is_none());
118+
fn size_in_bytes() -> usize {
136119
4
137120
}
138121
}
139-
140-
impl const FixedByteSize for String {
141-
fn size_in_bytes(length_hint: Option<usize>) -> usize {
142-
match length_hint {
143-
Some(length) => length,
144-
None => panic!("fixed size string needs to have a length hint"),
145-
}
146-
}
147-
}

0 commit comments

Comments
 (0)