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

feat: add bstr optional support #25

Merged
merged 2 commits into from
Jul 22, 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
31 changes: 23 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,38 @@

Notable changes only.

## [0.5.0] - unreleased
## [unreleased]

## Added
### Added

- add optional `bstr` crate support
- add `truncate`, `pop`, and `clear`
- add `concat` and `join`

### Fixed

- some clippy lints
- fix truncating `set_len` on borrowed

## [0.5.0] - 2024-06-24

### Added

- new conversion to `Cow`s
- a niche to make `Option<HipStr>` the same size as `HipStr`

## Changed
### Changed

- update MSRV to 1.77
- backend complete overall with its own custom RC implementation

## Fixed
### Fixed

- fixed some clippy lints

## [0.4.0] - 2023-12-01

## Added
### Added

- add `HipPath` and `HipOsStr` (#11)
- more methods to keep working with `Hip*` types when possible (#13)
Expand All @@ -29,21 +42,21 @@ Notable changes only.

Most of those addition are breaking because they shadows `str`'s methods.

## Changed
### Changed

- make equality more efficient (#12 then #18)
- better coverage with more tests (#14)

## [0.3.3] - 2023-10-30

## Fixed
### Fixed

- fix clippy lint in `bytes/cmp.rs`
- fix missing `std::error::Error` impl for `string::SliceError`

## [0.3.2] - 2023-08-18

## Added
### Added

- Add trait impls for OsStr/Path compatibility
- Add push_str and push
Expand Down Expand Up @@ -94,6 +107,8 @@ Most of those addition are breaking because they shadows `str`'s methods.

Initial release

[unreleased]: https://github.com/polazarus/hipstr/compare/0.4.0...HEAD
[0.5.0]: https://github.com/polazarus/hipstr/compare/0.4.0...0.5.0
[0.4.0]: https://github.com/polazarus/hipstr/compare/0.3.3...0.4.0
[0.3.3]: https://github.com/polazarus/hipstr/compare/0.3.2...0.3.3
[0.3.2]: https://github.com/polazarus/hipstr/compare/0.3.1...0.3.2
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ default = ["std"]
std = ["serde/std"]
unstable = []
serde = ["dep:serde", "dep:serde_bytes"]
bstr = ["dep:bstr"]

[dev-dependencies]
fastrand = "2.0.0"
Expand All @@ -25,6 +26,7 @@ serde = { version = "1.0.69", default-features = false, features = ["derive", "a
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

[dependencies]
bstr = { version = "1.9.1", optional = true, features = ["alloc"] }
serde = { version = "1.0.69", optional = true, default-features = false, features = [] }
serde_bytes = { version = "0.11.5", optional = true, default-features = false, features = [
"alloc",
Expand Down
28 changes: 24 additions & 4 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,24 @@ mod convert;
#[cfg(feature = "serde")]
pub mod serde;

#[cfg(feature = "bstr")]
mod bstr;

#[cfg(test)]
mod tests;

#[cfg(feature = "bstr")]
type Owned = ::bstr::BString;

#[cfg(not(feature = "bstr"))]
type Owned = Vec<u8>;

#[cfg(feature = "bstr")]
type Slice = ::bstr::BStr;

#[cfg(not(feature = "bstr"))]
type Slice = [u8];

/// Smart bytes, i.e. cheaply clonable and sliceable byte string.
///
/// # Examples
Expand Down Expand Up @@ -490,6 +505,10 @@ where
#[must_use]
pub fn mutate(&mut self) -> RefMut<'_, 'borrow, B> {
let owned = self.0.take_vec();

#[cfg(feature = "bstr")]
let owned = owned.into();

RefMut {
result: self,
owned,
Expand Down Expand Up @@ -1095,11 +1114,11 @@ impl<'borrow, B> Deref for HipByt<'borrow, B>
where
B: Backend,
{
type Target = [u8];
type Target = Slice;

#[inline]
fn deref(&self) -> &Self::Target {
self.as_slice()
self.as_ref()
}
}

Expand Down Expand Up @@ -1323,7 +1342,7 @@ where
B: Backend,
{
result: &'a mut HipByt<'borrow, B>,
owned: Vec<u8>,
owned: Owned,
}

impl<'a, 'borrow, B> Drop for RefMut<'a, 'borrow, B>
Expand All @@ -1340,7 +1359,8 @@ impl<'a, 'borrow, B> Deref for RefMut<'a, 'borrow, B>
where
B: Backend,
{
type Target = Vec<u8>;
type Target = Owned;

fn deref(&self) -> &Self::Target {
&self.owned
}
Expand Down
136 changes: 136 additions & 0 deletions src/bytes/bstr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use alloc::borrow::Cow;
use core::borrow::Borrow;

use bstr::{BStr, BString};

use super::HipByt;
use crate::Backend;

impl<'borrow, B> Borrow<BStr> for HipByt<'borrow, B>
where
B: Backend,
{
#[inline]
fn borrow(&self) -> &BStr {
BStr::new(self.as_slice())
}
}

impl<'borrow, B> AsRef<BStr> for HipByt<'borrow, B>
where
B: Backend,
{
#[inline]
fn as_ref(&self) -> &BStr {
BStr::new(self.as_slice())
}
}

impl<'borrow, B> From<&'borrow BStr> for HipByt<'borrow, B>
where
B: Backend,
{
#[inline]
fn from(value: &'borrow BStr) -> Self {
HipByt::borrowed(value.as_ref())
}
}

impl<'borrow, B> From<BString> for HipByt<'borrow, B>
where
B: Backend,
{
#[inline]
fn from(value: BString) -> Self {
HipByt::from(Vec::from(value))
}
}

impl<'borrow, B> From<Cow<'borrow, BStr>> for HipByt<'borrow, B>
where
B: Backend,
{
#[inline]
fn from(value: Cow<'borrow, BStr>) -> Self {
match value {
Cow::Borrowed(b) => Self::from(b),
Cow::Owned(o) => Self::from(o),
}
}
}

impl<'borrow, B> From<HipByt<'borrow, B>> for BString
where
B: Backend,
{
#[inline]
fn from(value: HipByt<'borrow, B>) -> Self {
value
.into_vec()
.map(Self::from)
.unwrap_or_else(|h| Self::from(h.as_slice()))
}
}

#[cfg(test)]
mod tests {
use bstr::{ByteSlice, ByteVec};

use super::*;
use crate::HipByt;

#[test]
fn test_borrow_bstr() {
let b = HipByt::from(b"Hello, World!");
let s: &BStr = b.borrow();
assert_eq!(s, "Hello, World!");
assert!(s.contains_str("World"));
}

#[test]
fn test_as_ref_bstr() {
let b = HipByt::from(b"Hello, World!");
let s: &BStr = b.as_ref();
assert_eq!(s, "Hello, World!");
assert!(s.contains_str("World"));
}

#[test]
fn test_from_bstr() {
let b = HipByt::from(BStr::new("Hello, World!"));
assert_eq!(&*b, "Hello, World!");
}

#[test]
fn test_from_bstring() {
let b = HipByt::from(BString::from("Hello, World!"));
assert_eq!(&*b, "Hello, World!");
}

#[test]
fn test_from_cow() {
let b = HipByt::from(Cow::Borrowed(BStr::new(b"Hello, World!")));
assert_eq!(&*b, "Hello, World!");
let b: HipByt = HipByt::from(Cow::<BStr>::Owned(BString::from(b"Hello, World!")));
assert_eq!(&*b, "Hello, World!");
}

#[test]
fn test_into_bstring() {
let b: HipByt<'static> = HipByt::from(b"Hello, World!");
let bstring: BString = b.into();
assert_eq!(bstring, "Hello, World!");
}

#[test]
fn test_mutate() {
let mut b = HipByt::from(b"Hello, World!");
{
let mut m = b.mutate();
let r: &mut BString = &mut m;
r.push_char('!');
assert_eq!(r, "Hello, World!!");
}
assert_eq!(b, b"Hello, World!!");
}
}