-
Notifications
You must be signed in to change notification settings - Fork 88
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
jamesmunns
wants to merge
4
commits into
main
Choose a base branch
from
james/max-size-as-a-const-fn
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
//! # Postcard Schema | ||
|
||
pub mod impls; | ||
pub mod max_size; | ||
pub mod schema; | ||
|
||
#[cfg(feature = "derive")] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
// discriminants are `varint(u32)` | ||
Some(max + disc_size) | ||
} | ||
DataModelType::Schema => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
not7
, we want to check how many variants fit in how many varint bytes.