From a21b7d0aa030bf719d146a0561fec1295b0f66fa Mon Sep 17 00:00:00 2001 From: Dylan Bowker Date: Sun, 12 May 2024 14:32:32 -0600 Subject: [PATCH] docs: moved notes on codecs to `README.md` --- libraries/native_model/README.md | 47 ++++++++++++++++++++++++ libraries/native_model/src/lib.rs | 59 ------------------------------- 2 files changed, 47 insertions(+), 59 deletions(-) diff --git a/libraries/native_model/README.md b/libraries/native_model/README.md index d102d5fe..9699c9e5 100644 --- a/libraries/native_model/README.md +++ b/libraries/native_model/README.md @@ -134,6 +134,53 @@ struct Cord { // Implement the conversion between versions From for DotV3 and From for DotV2. ``` +## Codecs + +`native_model` comes with several optional built-in serializer features available: + +- [bincode 1.3](https://crates.io/crates/bincode/1.3.3) + - This is the default codec. + - **Warning: This codec may not work with all serde-derived types.** + +- [bincode 2.0.0-rc.3](https://crates.io/crates/bincode/2.0.0-rc.3) + - Enable the `bincode_2_rc` feature and use the `native_model::bincode_2_rc::Bincode` attribute to have `native_db` use this crate for serializing & deserializing. + - **Warning: This codec may not work with all serde-derived types.** + +- [postcard 1.0](https://crates.io/crates/postcard/1.0.8) + - Enable the `postcard_1_0` feature and use the `native_model::postcard_1_0::PostCard` attribute. + - **Warning: This codec may not work with all serde-derived types.** + +- [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0) + - Enable the `rmp_serde_1_3` feature and use the `native_model::rmp_serde_1_3::RmpSerde` attribute. + +###### Codec example: + +As example, to use `rmp-serde`: + +1. In your project's `Cargo.toml` file, enable the `rmp_serde_1_3` feature for the `native_model` dependency. + - Be sure to check `crates.io` for the most recent [`native_model`](https://crates.io/crates/native_model) version number. + +```toml +[dependencies] +serde = { version = "1.0", features = [ "derive" ] } +native_model = { version = "0.4", features = [ "rmp_serde_1_3" ] } +``` + +2. Assign the `rmp_serde_1_3` codec to your `struct` using the `with` attribute: + +```rust +#[derive(Clone, Default, serde::Deserialize, serde::Serialize)] +#[native_model(id = 1, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)] +struct MyStruct { + my_string: String, + // etc. +} +``` + +###### Additional reading + +You may also want to check out [David Koloski](https://github.com/djkoloski)'s [Rust serialization benchmarks](https://github.com/djkoloski/rust_serialization_benchmark) for help selecting the codec (i.e. `bincode_1_3`, `rmp_serde_1_3`, etc.) that's best for your project. + ## Status Early development. Not ready for production. diff --git a/libraries/native_model/src/lib.rs b/libraries/native_model/src/lib.rs index 1bfd8d7c..d4fd5be6 100644 --- a/libraries/native_model/src/lib.rs +++ b/libraries/native_model/src/lib.rs @@ -14,65 +14,6 @@ //! - The crate is in early development, and performance is expected to improve over time. //! //! See examples in the [README.md](https://github.com/vincent-herlemont/native_model) file. -//! -//! # Codecs -//! -//! `native_model` comes with several optional built-in serializer features -//! available: -//! -//! - [bincode 1.3](https://crates.io/crates/bincode/1.3.3) · This is the -//! default codec. **Warning: This codec may not work with all serde-derived -//! types.** -//! -//! - [bincode 2.0.0-rc.3](https://crates.io/crates/bincode/2.0.0-rc.3) · -//! Enable the `bincode_2_rc` feature and use the -//! `native_model::bincode_2_rc::Bincode` attribute to have `native_db` use this -//! crate for serializing & deserializing. **Warning: This codec may not work -//! with all serde-derived types.** -//! -//! - [postcard 1.0](https://crates.io/crates/postcard/1.0.8) · -//! Enable the `postcard_1_0` feature and use the -//! `native_model::postcard_1_0::PostCard` attribute. **Warning: This codec may -//! not work with all serde-derived types.** -//! -//! - [rmp-serde 1.3](https://crates.io/crates/rmp-serde/1.3.0) · -//! Enable the `rmp_serde_1_3` feature and use the -//! `native_model::rmp_serde_1_3::RmpSerde` attribute. -//! -//! ###### Codec example: -//! -//! As example, to use `rmp-serde`: -//! -//! 1. In your project's `Cargo.toml` file, enable the `rmp_serde_1_3` feature -//! for the `native_model` dependency. Be sure to check `crates.io` for the most -//! recent [`native_model`](https://crates.io/crates/native_model) version -//! number. -//! -//! ```toml -//! [dependencies] -//! serde = { version = "1.0", features = [ "derive" ] } -//! native_model = { version = "0.4", features = [ "rmp_serde_1_3" ] } -//! ``` -//! -//! 2. Assign the `rmp_serde_1_3` codec to your `struct` using the `with` -//! attribute: -//! -//! ```rust -//! #[derive(Clone, Default, serde::Deserialize, serde::Serialize)] -//! #[native_model(id = 1, version = 1, with = native_model::rmp_serde_1_3::RmpSerde)] -//! struct MyStruct { -//! my_string: String, -//! // etc. -//! } -//! ``` -//! -//! ###### Additional reading -//! -//! You may also want to check out -//! [David Koloski](https://github.com/djkoloski)'s -//! [Rust serialization benchmarks](https://github.com/djkoloski/rust_serialization_benchmark) -//! for help selecting the codec (i.e. `bincode_1_3`, `rmp_serde_1_3`, etc.) -//! that's best for your project. #[cfg(any( feature = "serde",