From 63c08c0e93e81d2d8130ecbcf816aa340b57a2b8 Mon Sep 17 00:00:00 2001 From: Amos Wenger Date: Mon, 4 Nov 2024 15:06:00 +0100 Subject: [PATCH] Make compact_str / compact_bytes non-optional Closes #103 --- merde/Cargo.toml | 4 ---- merde_core/Cargo.toml | 8 ++------ merde_core/src/cowbytes.rs | 22 ++------------------ merde_core/src/cowstr.rs | 41 ++++--------------------------------- merde_core/src/lib.rs | 6 ------ zerodeps-example/Cargo.toml | 2 +- 6 files changed, 9 insertions(+), 74 deletions(-) diff --git a/merde/Cargo.toml b/merde/Cargo.toml index ddd1ece..b25cfc1 100644 --- a/merde/Cargo.toml +++ b/merde/Cargo.toml @@ -76,14 +76,10 @@ full = [ "msgpack", "time", "rusqlite", - "compact_str", - "compact_bytes", ] core = ["dep:merde_core"] deserialize = ["core"] # merde_core re-exports -compact_str = ["merde_core/compact_str"] -compact_bytes = ["merde_core/compact_bytes"] serde = ["merde_core/serde"] rusqlite = ["merde_core/rusqlite"] diff --git a/merde_core/Cargo.toml b/merde_core/Cargo.toml index df51b36..7f33cfc 100644 --- a/merde_core/Cargo.toml +++ b/merde_core/Cargo.toml @@ -12,8 +12,8 @@ categories = ["encoding", "parser-implementations"] rust-version = "1.80" [dependencies] -compact_str = { version = "0.8.0", optional = true } -compact_bytes = { version = "0.1.3", optional = true } +compact_str = { version = "0.8.0" } +compact_bytes = { version = "0.1.3" } ordered-float = "4.3.0" rubicon = "3.4.9" rusqlite = { version = "0.32.1", optional = true } @@ -24,13 +24,9 @@ pin-project-lite = "0.2.15" default = [] full = [ # (1 per line) - "compact_str", - "compact_bytes", "serde", "rusqlite", ] -compact_str = ["dep:compact_str"] -compact_bytes = ["dep:compact_bytes"] serde = ["dep:serde", "compact_str/serde"] rusqlite = ["dep:rusqlite"] diff --git a/merde_core/src/cowbytes.rs b/merde_core/src/cowbytes.rs index 7299cba..f120c97 100644 --- a/merde_core/src/cowbytes.rs +++ b/merde_core/src/cowbytes.rs @@ -1,3 +1,4 @@ +use compact_bytes::CompactBytes; use std::{ borrow::Cow, fmt, @@ -5,9 +6,6 @@ use std::{ ops::Deref, }; -#[cfg(feature = "compact_bytes")] -use compact_bytes::CompactBytes; - use crate::IntoStatic; /// A copy-on-write bytes type that uses [`CompactBytes`] for @@ -15,10 +13,7 @@ use crate::IntoStatic; #[derive(Clone)] pub enum CowBytes<'a> { Borrowed(&'a [u8]), - #[cfg(feature = "compact_bytes")] Owned(CompactBytes), - #[cfg(not(feature = "compact_bytes"))] - Owned(Vec), } impl<'a> CowBytes<'a> { @@ -29,10 +24,7 @@ impl<'a> CowBytes<'a> { pub fn into_owned(self) -> Vec { match self { CowBytes::Borrowed(b) => b.to_vec(), - #[cfg(feature = "compact_bytes")] CowBytes::Owned(b) => b.to_vec(), - #[cfg(not(feature = "compact_bytes"))] - CowBytes::Owned(b) => b, } } } @@ -62,14 +54,7 @@ impl<'a> From<&'a [u8]> for CowBytes<'a> { impl<'a> From> for CowBytes<'a> { fn from(v: Vec) -> Self { - #[cfg(feature = "compact_bytes")] - { - CowBytes::Owned(CompactBytes::from(v)) - } - #[cfg(not(feature = "compact_bytes"))] - { - CowBytes::Owned(v) - } + CowBytes::Owned(CompactBytes::from(v)) } } @@ -119,10 +104,7 @@ impl IntoStatic for CowBytes<'_> { fn into_static(self) -> Self::Output { match self { - #[cfg(feature = "compact_bytes")] CowBytes::Borrowed(b) => CowBytes::Owned(CompactBytes::new(b)), - #[cfg(not(feature = "compact_bytes"))] - CowBytes::Borrowed(b) => CowBytes::Owned(b.to_vec()), CowBytes::Owned(b) => CowBytes::Owned(b), } } diff --git a/merde_core/src/cowstr.rs b/merde_core/src/cowstr.rs index 2d3c520..df3fc16 100644 --- a/merde_core/src/cowstr.rs +++ b/merde_core/src/cowstr.rs @@ -5,7 +5,6 @@ use std::{ ops::Deref, }; -#[cfg(feature = "compact_str")] use compact_str::CompactString; use crate::IntoStatic; @@ -18,10 +17,7 @@ use crate::IntoStatic; #[derive(Clone)] pub enum CowStr<'s> { Borrowed(&'s str), - #[cfg(feature = "compact_str")] Owned(CompactString), - #[cfg(not(feature = "compact_str"))] - Owned(String), } impl CowStr<'static> { @@ -29,15 +25,7 @@ impl CowStr<'static> { /// if the `compact_str` feature is disabled, or if the string is longer /// than `MAX_INLINE_SIZE`. pub fn copy_from_str(s: &str) -> Self { - #[cfg(feature = "compact_str")] - { - Self::Owned(CompactString::from(s)) - } - - #[cfg(not(feature = "compact_str"))] - { - Self::Owned(s.into()) - } + Self::Owned(CompactString::from(s)) } } @@ -49,26 +37,12 @@ impl<'s> CowStr<'s> { #[inline] pub fn from_utf8_owned(s: Vec) -> Result { - #[cfg(feature = "compact_str")] - { - Ok(Self::Owned(CompactString::from_utf8(s)?)) - } - #[cfg(not(feature = "compact_str"))] - { - Ok(String::from_utf8(s).map_err(|e| e.utf8_error())?.into()) - } + Ok(Self::Owned(CompactString::from_utf8(s)?)) } #[inline] pub fn from_utf8_lossy(s: &'s [u8]) -> Self { - #[cfg(feature = "compact_str")] - { - Self::Owned(CompactString::from_utf8_lossy(s)) - } - #[cfg(not(feature = "compact_str"))] - { - String::from_utf8_lossy(s).into() - } + Self::Owned(CompactString::from_utf8_lossy(s)) } /// # Safety @@ -76,14 +50,7 @@ impl<'s> CowStr<'s> { /// This function is unsafe because it does not check that the bytes are valid UTF-8. #[inline] pub unsafe fn from_utf8_unchecked(s: &'s [u8]) -> Self { - #[cfg(feature = "compact_str")] - { - Self::Owned(CompactString::from_utf8_unchecked(s)) - } - #[cfg(not(feature = "compact_str"))] - { - Self::Borrowed(std::str::from_utf8_unchecked(s)) - } + Self::Owned(CompactString::from_utf8_unchecked(s)) } } diff --git a/merde_core/src/lib.rs b/merde_core/src/lib.rs index 5f6ccae..4b72fde 100644 --- a/merde_core/src/lib.rs +++ b/merde_core/src/lib.rs @@ -47,10 +47,4 @@ pub use deserialize::FieldSlot; rubicon::compatibility_check! { ("merde_core_pkg_version", env!("CARGO_PKG_VERSION")), - - #[cfg(feature = "compact_str")] - ("compact_str", "enabled") - - #[cfg(feature = "compact_bytes")] - ("compact_bytes", "enabled") } diff --git a/zerodeps-example/Cargo.toml b/zerodeps-example/Cargo.toml index 6e01ff9..7f69a17 100644 --- a/zerodeps-example/Cargo.toml +++ b/zerodeps-example/Cargo.toml @@ -9,4 +9,4 @@ merde = { path = "../merde", default-features = false } [features] default = [] -merde = ["merde/core", "merde/compact_str"] +merde = ["merde/core"]