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

Render cfg information on docs.rs #108

Merged
merged 2 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ documentation = "https://docs.rs/postcard/"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "doc_cfg"]

[dependencies]

Expand Down
1 change: 1 addition & 0 deletions src/de/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ impl<'de> Flavor<'de> for Slice<'de> {
///
/// More on CRCs: <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>.
#[cfg(feature = "use-crc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-crc")))]
pub mod crc {
use core::convert::TryInto;

Expand Down
2 changes: 2 additions & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ where
///
/// See the `de_flavors::crc` module for the complete set of functions.
#[cfg(feature = "use-crc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-crc")))]
#[inline]
pub fn from_bytes_crc32<'a, T>(s: &'a [u8], digest: crc::Digest<'a, u32>) -> Result<T>
where
Expand All @@ -85,6 +86,7 @@ where
///
/// See the `de_flavors::crc` module for the complete set of functions.
#[cfg(feature = "use-crc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-crc")))]
#[inline]
pub fn take_from_bytes_crc32<'a, T>(
s: &'a [u8],
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg_attr(not(any(test, feature = "use-std")), no_std)]
#![warn(missing_docs)]
#![cfg_attr(not(doctest), doc = include_str!("../README.md"))]
#![cfg_attr(doc_cfg, feature(doc_cfg))]

pub mod accumulator;
mod de;
Expand Down Expand Up @@ -56,6 +57,7 @@ pub(crate) mod schema;
pub mod experimental {
/// Compile time max-serialization size calculation
#[cfg(feature = "experimental-derive")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "experimental-derive")))]
pub mod max_size {
// NOTE: This is the trait...
pub use crate::max_size::MaxSize;
Expand All @@ -67,6 +69,7 @@ pub mod experimental {

/// Compile time Schema generation
#[cfg(feature = "experimental-derive")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "experimental-derive")))]
pub mod schema {
// NOTE: This is the trait...
pub use crate::schema::{NamedType, NamedValue, NamedVariant, Schema, SdmTy, Varint};
Expand Down
2 changes: 2 additions & 0 deletions src/max_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ impl<A: MaxSize, B: MaxSize, C: MaxSize, D: MaxSize, E: MaxSize, F: MaxSize> Max
}

#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
impl<T: MaxSize, const N: usize> MaxSize for heapless::Vec<T, N> {
const POSTCARD_MAX_SIZE: usize = <[T; N]>::POSTCARD_MAX_SIZE + varint_size(N);
}

#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
impl<const N: usize> MaxSize for heapless::String<N> {
const POSTCARD_MAX_SIZE: usize = <[u8; N]>::POSTCARD_MAX_SIZE + varint_size(N);
}
Expand Down
4 changes: 4 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,15 @@ impl<T: Schema, const N: usize> Schema for [T; N] {
}

#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
impl<T: Schema, const N: usize> Schema for heapless::Vec<T, N> {
const SCHEMA: &'static NamedType = &NamedType {
name: "heapless::Vec<T, N>",
ty: &SdmTy::Seq(T::SCHEMA),
};
}
#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
impl<const N: usize> Schema for heapless::String<N> {
const SCHEMA: &'static NamedType = &NamedType {
name: "heapless::String<N>",
Expand All @@ -243,6 +245,7 @@ impl<const N: usize> Schema for heapless::String<N> {
}

#[cfg(feature = "use-std")]
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
impl<T: Schema> Schema for std::vec::Vec<T> {
const SCHEMA: &'static NamedType = &NamedType {
name: "Vec<T>",
Expand All @@ -251,6 +254,7 @@ impl<T: Schema> Schema for std::vec::Vec<T> {
}

#[cfg(feature = "use-std")]
#[cfg_attr(doc_cfg, doc(cfg(any(feature = "alloc", feature = "use-std"))))]
impl Schema for std::string::String {
const SCHEMA: &'static NamedType = &NamedType {
name: "String",
Expand Down
4 changes: 3 additions & 1 deletion src/ser/flavors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ where
///
/// More on CRCs: <https://en.wikipedia.org/wiki/Cyclic_redundancy_check>.
#[cfg(feature = "use-crc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-crc")))]
pub mod crc {
use crc::Digest;
use crc::Width;
Expand Down Expand Up @@ -506,8 +507,8 @@ pub mod crc {

/// Serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
/// data followed by a CRC. The CRC bytes are included in the output `Vec`.
/// Requires the (default) `heapless` feature.
#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
pub fn [<to_vec_ $int>]<'a, T, const B: usize>(
value: &T,
digest: Digest<'a, $int>,
Expand All @@ -523,6 +524,7 @@ pub mod crc {
/// Serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
/// data followed by a CRC. The CRC bytes are included in the output `Vec`.
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn [<to_allocvec_ $int>]<'a, T>(value: &T, digest: Digest<'a, $int>) -> Result<alloc::vec::Vec<u8>>
where
T: Serialize + ?Sized,
Expand Down
23 changes: 16 additions & 7 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where

/// Serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
/// data in a serialized then COBS encoded format. The terminating sentinel
/// `0x00` byte is included in the output `Vec`. Requires the (default) `heapless` feature.
/// `0x00` byte is included in the output `Vec`.
///
/// ## Example
///
Expand All @@ -116,6 +116,7 @@ where
/// assert_eq!(ser.deref(), &[0x02, 0x01, 0x03, 0x20, 0x30, 0x00]);
/// ```
#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
pub fn to_vec_cobs<T, const B: usize>(value: &T) -> Result<Vec<u8, B>>
where
T: Serialize + ?Sized,
Expand All @@ -124,7 +125,7 @@ where
}

/// Serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
/// data in a serialized format. Requires the (default) `heapless` feature.
/// data in a serialized format.
///
/// ## Example
///
Expand All @@ -149,14 +150,15 @@ where
/// assert_eq!(ser.deref(), &[0x01, 0x00, 0x20, 0x30]);
/// ```
#[cfg(feature = "heapless")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "heapless")))]
pub fn to_vec<T, const B: usize>(value: &T) -> Result<Vec<u8, B>>
where
T: Serialize + ?Sized,
{
serialize_with_flavor::<T, HVec<B>, Vec<u8, B>>(value, HVec::default())
}

/// Serialize a `T` to a `std::vec::Vec<u8>`. Requires the `use-std` feature.
/// Serialize a `T` to a `std::vec::Vec<u8>`.
///
/// ## Example
///
Expand All @@ -170,6 +172,7 @@ where
/// assert_eq!(ser.as_slice(), &[0x03, b'H', b'i', b'!']);
/// ```
#[cfg(feature = "use-std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-std")))]
#[inline]
pub fn to_stdvec<T>(value: &T) -> Result<std::vec::Vec<u8>>
where
Expand All @@ -178,7 +181,7 @@ where
to_allocvec(value)
}

/// Serialize and COBS encode a `T` to a `std::vec::Vec<u8>`. Requires the `use-std` feature.
/// Serialize and COBS encode a `T` to a `std::vec::Vec<u8>`.
///
/// The terminating sentinel `0x00` byte is included in the output.
///
Expand All @@ -194,6 +197,7 @@ where
/// assert_eq!(ser.as_slice(), &[0x05, 0x03, b'H', b'i', b'!', 0x00]);
/// ```
#[cfg(feature = "use-std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-std")))]
#[inline]
pub fn to_stdvec_cobs<T>(value: &T) -> Result<std::vec::Vec<u8>>
where
Expand All @@ -202,7 +206,7 @@ where
to_allocvec_cobs(value)
}

/// Serialize a `T` to an `alloc::vec::Vec<u8>`. Requires the `alloc` feature.
/// Serialize a `T` to an `alloc::vec::Vec<u8>`.
///
/// ## Example
///
Expand All @@ -216,14 +220,15 @@ where
/// assert_eq!(ser.as_slice(), &[0x03, b'H', b'i', b'!']);
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn to_allocvec<T>(value: &T) -> Result<alloc::vec::Vec<u8>>
where
T: Serialize + ?Sized,
{
serialize_with_flavor::<T, AllocVec, alloc::vec::Vec<u8>>(value, AllocVec::new())
}

/// Serialize and COBS encode a `T` to an `alloc::vec::Vec<u8>`. Requires the `alloc` feature.
/// Serialize and COBS encode a `T` to an `alloc::vec::Vec<u8>`.
///
/// The terminating sentinel `0x00` byte is included in the output.
///
Expand All @@ -239,6 +244,7 @@ where
/// assert_eq!(ser.as_slice(), &[0x05, 0x03, b'H', b'i', b'!', 0x00]);
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
pub fn to_allocvec_cobs<T>(value: &T) -> Result<alloc::vec::Vec<u8>>
where
T: Serialize + ?Sized,
Expand Down Expand Up @@ -270,6 +276,7 @@ where
///
/// See the `ser_flavors::crc` module for the complete set of functions.
#[cfg(feature = "use-crc")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "use-crc")))]
#[inline]
pub fn to_slice_crc32<'a, 'b, T>(
value: &'b T,
Expand All @@ -284,7 +291,6 @@ where

/// Conveniently serialize a `T` to a `heapless::Vec<u8>`, with the `Vec` containing
/// data followed by a 32-bit CRC. The CRC bytes are included in the output `Vec`.
/// Requires the (default) `heapless` feature.
///
/// ## Example
///
Expand All @@ -306,6 +312,7 @@ where
///
/// See the `ser_flavors::crc` module for the complete set of functions.
#[cfg(all(feature = "use-crc", feature = "heapless"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "use-crc", feature = "heapless"))))]
#[inline]
pub fn to_vec_crc32<'a, T, const B: usize>(
value: &T,
Expand Down Expand Up @@ -339,6 +346,7 @@ where
///
/// See the `ser_flavors::crc` module for the complete set of functions.
#[cfg(all(feature = "use-crc", feature = "use-std"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "use-crc", feature = "use-std"))))]
#[inline]
pub fn to_stdvec_crc32<'a, T>(value: &T, digest: crc::Digest<'a, u32>) -> Result<std::vec::Vec<u8>>
where
Expand Down Expand Up @@ -369,6 +377,7 @@ where
///
/// See the `ser_flavors::crc` module for the complete set of functions.
#[cfg(all(feature = "use-crc", feature = "alloc"))]
#[cfg_attr(doc_cfg, doc(cfg(all(feature = "use-crc", feature = "alloc"))))]
#[inline]
pub fn to_allocvec_crc32<'a, T>(
value: &T,
Expand Down