From 594df5987d41cfa326f297c49ea570a42c28992d Mon Sep 17 00:00:00 2001 From: "Matthieu Le brazidec (r3v2d0g)" Date: Wed, 11 Dec 2024 10:19:50 +0000 Subject: [PATCH 1/2] Add a `bincode2` feature This adds a new `bincode2` feature which when enabled derives `bincode::Encode` and `bincode::Decode` (from `bincode@2.0.0-rc.3`) for `NonEmpty`. The goal of this is to avoid having to use `#[bincode(with_serde)]` when having a field containing `NonEmpty`. --- Cargo.toml | 2 ++ src/lib.rs | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 1864b23..27005d2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,10 +10,12 @@ repository = "https://github.com/cloudhead/nonempty" [dependencies] serde = { features = ["serde_derive"], optional = true, version = "1" } arbitrary = { features = ["derive"], optional = true, version = "1" } +bincode = { optional = true, version = "2.0.0-rc.3" } [features] serialize = ["serde"] arbitrary = ["dep:arbitrary"] +bincode2 = ["dep:bincode"] [dev-dependencies] serde_json = "1" diff --git a/src/lib.rs b/src/lib.rs index 422620c..accf796 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,8 +72,11 @@ //! //! * `serialize`: `serde` support. //! * `arbitrary`: `arbitrary` support. +//! * `bincode2`" `bincode@2.0.0-rc.3` support. #[cfg(feature = "arbitrary")] use arbitrary::Arbitrary; +#[cfg(feature = "bincode2")] +use bincode::{Decode, Encode}; #[cfg(feature = "serialize")] use serde::{ ser::{SerializeSeq, Serializer}, @@ -121,7 +124,12 @@ macro_rules! nonempty { /// Non-empty vector. #[cfg_attr(feature = "serialize", derive(Deserialize))] #[cfg_attr(feature = "arbitrary", derive(Arbitrary))] +#[cfg_attr(feature = "bincode2", derive(Encode, Decode))] #[cfg_attr(feature = "serialize", serde(try_from = "Vec"))] +#[cfg_attr(feature = "bincode2", bincode( + encode_bounds = "T: Encode + 'static", + decode_bounds = "T: Decode + 'static", +))] #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct NonEmpty { pub head: T, From 2ccad59a4109ea5dd2ab0033338067afb3d6e62f Mon Sep 17 00:00:00 2001 From: "Matthieu Le brazidec (r3v2d0g)" Date: Tue, 17 Dec 2024 21:55:06 +0100 Subject: [PATCH 2/2] Fix formatting --- src/lib.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9024f19..4a9d3fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,10 +126,13 @@ macro_rules! nonempty { #[cfg_attr(feature = "arbitrary", derive(Arbitrary))] #[cfg_attr(feature = "bincode2", derive(Encode, Decode))] #[cfg_attr(feature = "serialize", serde(try_from = "Vec"))] -#[cfg_attr(feature = "bincode2", bincode( - encode_bounds = "T: Encode + 'static", - decode_bounds = "T: Decode + 'static", -))] +#[cfg_attr( + feature = "bincode2", + bincode( + encode_bounds = "T: Encode + 'static", + decode_bounds = "T: Decode + 'static", + ) +)] #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct NonEmpty { pub head: T,