Skip to content

Commit

Permalink
Merge pull request #193 from ChocolateLoverRaj/range-max-size
Browse files Browse the repository at this point in the history
Implement MaxSize for serializable Range structs
  • Loading branch information
jamesmunns authored Dec 3, 2024
2 parents cb14463 + 7c6fd97 commit 493ae45
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
63 changes: 59 additions & 4 deletions source/postcard-schema/src/impls/builtins_nostd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
//! These implementations are always available
use crate::{
schema::{DataModelType, DataModelVariant, NamedType, NamedVariant},
schema::{DataModelType, DataModelVariant, NamedType, NamedValue, NamedVariant},
Schema,
};
use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU8,
use core::{
num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU8,
},
ops::{Range, RangeFrom, RangeInclusive, RangeTo},
};

macro_rules! impl_schema {
Expand Down Expand Up @@ -100,3 +103,55 @@ impl<T: Schema, const N: usize> Schema for [T; N] {
ty: &DataModelType::Tuple(&[T::SCHEMA; N]),
};
}

impl<T: Schema> Schema for Range<T> {
const SCHEMA: &'static crate::schema::NamedType = &NamedType {
name: "Range<T>",
ty: &DataModelType::Struct(&[
&NamedValue {
name: "start",
ty: T::SCHEMA,
},
&NamedValue {
name: "end",
ty: T::SCHEMA,
},
]),
};
}

impl<T: Schema> Schema for RangeInclusive<T> {
const SCHEMA: &'static crate::schema::NamedType = &NamedType {
name: "RangeInclusive<T>",
ty: &DataModelType::Struct(&[
&NamedValue {
name: "start",
ty: T::SCHEMA,
},
&NamedValue {
name: "end",
ty: T::SCHEMA,
},
]),
};
}

impl<T: Schema> Schema for RangeFrom<T> {
const SCHEMA: &'static crate::schema::NamedType = &NamedType {
name: "RangeFrom<T>",
ty: &DataModelType::Struct(&[&NamedValue {
name: "start",
ty: T::SCHEMA,
}]),
};
}

impl<T: Schema> Schema for RangeTo<T> {
const SCHEMA: &'static crate::schema::NamedType = &NamedType {
name: "RangeTo<T>",
ty: &DataModelType::Struct(&[&NamedValue {
name: "end",
ty: T::SCHEMA,
}]),
};
}
17 changes: 17 additions & 0 deletions source/postcard/src/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use core::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroIsize, NonZeroU128,
NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize,
},
ops::{Range, RangeFrom, RangeInclusive, RangeTo},
};

/// This trait is used to enforce the maximum size required to
Expand Down Expand Up @@ -201,6 +202,22 @@ impl<A: MaxSize, B: MaxSize, C: MaxSize, D: MaxSize, E: MaxSize, F: MaxSize> Max
+ F::POSTCARD_MAX_SIZE;
}

impl<T: MaxSize> MaxSize for Range<T> {
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE * 2;
}

impl<T: MaxSize> MaxSize for RangeInclusive<T> {
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE * 2;
}

impl<T: MaxSize> MaxSize for RangeFrom<T> {
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
}

impl<T: MaxSize> MaxSize for RangeTo<T> {
const POSTCARD_MAX_SIZE: usize = T::POSTCARD_MAX_SIZE;
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<T: MaxSize> MaxSize for Box<T> {
Expand Down

0 comments on commit 493ae45

Please # to comment.