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

Make compact_str / compact_bytes non-optional #106

Merged
merged 1 commit into from
Nov 4, 2024
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
4 changes: 0 additions & 4 deletions merde/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
8 changes: 2 additions & 6 deletions merde_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]

Expand Down
22 changes: 2 additions & 20 deletions merde_core/src/cowbytes.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
use compact_bytes::CompactBytes;
use std::{
borrow::Cow,
fmt,
hash::{Hash, Hasher},
ops::Deref,
};

#[cfg(feature = "compact_bytes")]
use compact_bytes::CompactBytes;

use crate::IntoStatic;

/// A copy-on-write bytes type that uses [`CompactBytes`] for
/// the "owned" variant.
#[derive(Clone)]
pub enum CowBytes<'a> {
Borrowed(&'a [u8]),
#[cfg(feature = "compact_bytes")]
Owned(CompactBytes),
#[cfg(not(feature = "compact_bytes"))]
Owned(Vec<u8>),
}

impl<'a> CowBytes<'a> {
Expand All @@ -29,10 +24,7 @@ impl<'a> CowBytes<'a> {
pub fn into_owned(self) -> Vec<u8> {
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,
}
}
}
Expand Down Expand Up @@ -62,14 +54,7 @@ impl<'a> From<&'a [u8]> for CowBytes<'a> {

impl<'a> From<Vec<u8>> for CowBytes<'a> {
fn from(v: Vec<u8>) -> Self {
#[cfg(feature = "compact_bytes")]
{
CowBytes::Owned(CompactBytes::from(v))
}
#[cfg(not(feature = "compact_bytes"))]
{
CowBytes::Owned(v)
}
CowBytes::Owned(CompactBytes::from(v))
}
}

Expand Down Expand Up @@ -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),
}
}
Expand Down
41 changes: 4 additions & 37 deletions merde_core/src/cowstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
ops::Deref,
};

#[cfg(feature = "compact_str")]
use compact_str::CompactString;

use crate::IntoStatic;
Expand All @@ -18,26 +17,15 @@ 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> {
/// Create a new `CowStr` by copying from a `&str` — this might allocate
/// 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))
}
}

Expand All @@ -49,41 +37,20 @@ impl<'s> CowStr<'s> {

#[inline]
pub fn from_utf8_owned(s: Vec<u8>) -> Result<Self, std::str::Utf8Error> {
#[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
///
/// 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))
}
}

Expand Down
6 changes: 0 additions & 6 deletions merde_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion zerodeps-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ merde = { path = "../merde", default-features = false }

[features]
default = []
merde = ["merde/core", "merde/compact_str"]
merde = ["merde/core"]