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

Implement a version of Schema types that are owned #170

Merged
merged 12 commits into from
Oct 14, 2024
40 changes: 28 additions & 12 deletions source/postcard-derive/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,21 @@ fn generate_struct(fields: &Fields) -> TokenStream {
]) }
}
syn::Fields::Unnamed(fields) => {
let fields = fields.unnamed.iter().map(|f| {
if fields.unnamed.len() == 1 {
let f = fields.unnamed[0].clone();
let ty = &f.ty;
quote_spanned!(f.span() => <#ty as ::postcard::experimental::schema::Schema>::SCHEMA)
});
quote! { &::postcard::experimental::schema::SdmTy::TupleStruct(&[
#( #fields ),*
]) }
let qs = quote_spanned!(f.span() => <#ty as ::postcard::experimental::schema::Schema>::SCHEMA);

quote! { &::postcard::experimental::schema::SdmTy::NewtypeStruct(#qs) }
} else {
let fields = fields.unnamed.iter().map(|f| {
let ty = &f.ty;
quote_spanned!(f.span() => <#ty as ::postcard::experimental::schema::Schema>::SCHEMA)
});
quote! { &::postcard::experimental::schema::SdmTy::TupleStruct(&[
#( #fields ),*
]) }
}
}
syn::Fields::Unit => {
quote! { &::postcard::experimental::schema::SdmTy::UnitStruct }
Expand All @@ -96,13 +104,21 @@ fn generate_variants(fields: &Fields) -> TokenStream {
]) }
}
syn::Fields::Unnamed(fields) => {
let fields = fields.unnamed.iter().map(|f| {
if fields.unnamed.len() == 1 {
let f = fields.unnamed[0].clone();
let ty = &f.ty;
quote_spanned!(f.span() => <#ty as ::postcard::experimental::schema::Schema>::SCHEMA)
});
quote! { &::postcard::experimental::schema::SdmTy::TupleVariant(&[
#( #fields ),*
]) }
let qs = quote_spanned!(f.span() => <#ty as ::postcard::experimental::schema::Schema>::SCHEMA);

quote! { &::postcard::experimental::schema::SdmTy::NewtypeVariant(#qs) }
} else {
let fields = fields.unnamed.iter().map(|f| {
let ty = &f.ty;
quote_spanned!(f.span() => <#ty as ::postcard::experimental::schema::Schema>::SCHEMA)
});
quote! { &::postcard::experimental::schema::SdmTy::TupleVariant(&[
#( #fields ),*
]) }
}
}
syn::Fields::Unit => {
quote! { &::postcard::experimental::schema::SdmTy::UnitVariant }
Expand Down
13 changes: 13 additions & 0 deletions source/postcard/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ optional = true
version = "1.0.12"
optional = true

[dependencies.uuid]
version = "1.0"
default-features = false
optional = true

[dependencies.chrono]
version = "0.4"
default-features = false
optional = true

[features]
default = ["heapless-cas"]

Expand All @@ -83,6 +93,9 @@ alloc = ["serde/alloc", "embedded-io-04?/alloc", "embedded-io-06?/alloc"]
use-defmt = ["defmt"]
use-crc = ["crc", "paste"]

uuid-v1_0 = ["uuid"]
chrono-v0_4 = ["chrono"]

# Experimental features!
#
# NOT subject to SemVer guarantees!
Expand Down
9 changes: 9 additions & 0 deletions source/postcard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ pub mod experimental {
pub use crate::schema::{NamedType, NamedValue, NamedVariant, Schema, SdmTy, Varint};
// NOTE: ...and this is the derive macro
pub use postcard_derive::Schema;

#[cfg(any(feature = "use-std", feature = "alloc"))]
pub use crate::schema::{
fmt::{fmt_owned_nt_to_buf, is_prim},
owned::{OwnedNamedType, OwnedNamedValue, OwnedNamedVariant, OwnedSdmTy},
};

#[cfg(feature = "use-std")]
pub use crate::schema::fmt::{discover_tys, discover_tys_sdm};
}
}

Expand Down
Loading