Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaxSize as a const fn #179

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions source/postcard-schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! # Postcard Schema

pub mod impls;
pub mod max_size;
pub mod schema;

#[cfg(feature = "derive")]
Expand Down
124 changes: 124 additions & 0 deletions source/postcard-schema/src/max_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//! .

use crate::{
schema::{DataModelType, DataModelVariant, NamedType},
Schema,
};

/// Calculate the max size of a type that impls Schema
pub const fn max_size<T: Schema>() -> Option<usize> {
max_size_nt(T::SCHEMA)
}

/// Calculate the max size of a NamedType
pub const fn max_size_nt(nt: &NamedType) -> Option<usize> {
max_size_dmt(nt.ty)
}

/// Calculate the max size of a DataModelType
pub const fn max_size_dmt(dmt: &DataModelType) -> Option<usize> {
match dmt {
DataModelType::Bool => Some(1),
DataModelType::I8 => Some(1),
DataModelType::U8 => Some(1),
DataModelType::I16 => Some(3),
DataModelType::I32 => Some(5),
DataModelType::I64 => Some(10),
DataModelType::I128 => Some(19),
DataModelType::U16 => Some(3),
DataModelType::U32 => Some(5),
DataModelType::U64 => Some(10),
DataModelType::U128 => Some(19),
DataModelType::Usize => None, // TODO: these don't impl schema and are platform dependent
DataModelType::Isize => None, // TODO: these don't impl schema and are platform dependent
DataModelType::F32 => Some(4),
DataModelType::F64 => Some(8),
DataModelType::Char => Some(5), // I think? 1 len + up to 4 bytes
DataModelType::String => None,
DataModelType::ByteArray => None,
DataModelType::Option(nt) => max_size_nt(nt),
DataModelType::Unit => Some(0),
DataModelType::UnitStruct => Some(0),
DataModelType::NewtypeStruct(nt) => max_size_nt(nt),
DataModelType::Seq(_) => None,
DataModelType::Tuple(nts) | DataModelType::TupleStruct(nts) => {
let mut i = 0;
let mut ct = 0;
while i < nts.len() {
let Some(sz) = max_size_nt(nts[i]) else {
return None;
};
ct += sz;
i += 1;
}
Some(ct)
}
DataModelType::Map { .. } => None,
DataModelType::Struct(nvals) => {
let mut i = 0;
let mut ct = 0;
while i < nvals.len() {
let Some(sz) = max_size_dmt(nvals[i].ty.ty) else {
return None;
};
ct += sz;
i += 1;
}
Some(ct)
}
DataModelType::Enum(nvars) => {
let mut i = 0;
let mut max = 0;
while i < nvars.len() {
let sz = match nvars[i].ty {
DataModelVariant::UnitVariant => 0,
DataModelVariant::NewtypeVariant(nt) => {
let Some(sz) = max_size_nt(nt) else {
return None;
};
sz
}
DataModelVariant::TupleVariant(nts) => {
let mut j = 0;
let mut ct = 0;
while j < nts.len() {
let Some(sz) = max_size_nt(nts[j]) else {
return None;
};
ct += sz;
j += 1;
}
ct
}
DataModelVariant::StructVariant(nvars) => {
let mut j = 0;
let mut ct = 0;
while j < nvars.len() {
let Some(sz) = max_size_dmt(nvars[j].ty.ty) else {
return None;
};
ct += sz;
j += 1;
}
ct
}
};

if sz > max {
max = sz;
}

i += 1;
}
let disc_size = if nvars.is_empty() {
1
} else {
// CEIL(variants / 7)
(nvars.len() + 6) / 7
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't right, it should be 2^7 not 7, we want to check how many variants fit in how many varint bytes.

};
// discriminants are `varint(u32)`
Some(max + disc_size)
}
DataModelType::Schema => None,
}
}
25 changes: 25 additions & 0 deletions source/postcard-schema/tests/max_size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![allow(dead_code)]

use postcard_schema::{max_size::max_size, Schema};

#[derive(Schema)]
enum ExampleE {
Foo,
Bar([u8; 5]),
Baz { a: Result<i32, u32>, b: i128 },
}

#[derive(Schema)]
struct ExampleS {
a: Result<i32, u32>,
b: i128,
c: bool,
}

#[test]
fn smoke() {
let max1 = max_size::<ExampleE>();
let max2 = max_size::<ExampleS>();
assert_eq!(Some(26), max1);
assert_eq!(Some(26), max2);
}