From 905b7928bf853b91723778b116ee4ceee1880326 Mon Sep 17 00:00:00 2001 From: "Matthieu Le brazidec (r3v2d0g)" Date: Wed, 11 Dec 2024 10:19:50 +0000 Subject: [PATCH] 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 | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 27de4f6..6c6ad4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,12 +10,14 @@ repository = "https://github.com/cloudhead/nonempty" [dependencies] serde = { features = ["derive", "alloc"], default-features = false, optional = true, version = "1" } arbitrary = { features = ["derive"], optional = true, version = "1" } +bincode = { optional = true, version = "2.0.0-rc.3" } [features] default = ["std"] std = [] serialize = ["dep:serde"] arbitrary = ["dep:arbitrary"] +bincode2 = ["dep:bincode"] [dev-dependencies] serde_json = "1" diff --git a/src/lib.rs b/src/lib.rs index 761de76..15815b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,8 +80,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}, @@ -139,7 +142,15 @@ 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,