Skip to content
Open
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "smallstr"
version = "0.3.0"
version = "0.3.1"
authors = ["Murarth <murarth@gmail.com>"]
edition = "2018"

Expand Down
42 changes: 39 additions & 3 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use core::{
borrow::{Borrow, BorrowMut},
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
iter::FromIterator,
ops, ptr, slice,
str::{self, Chars, Utf8Error},
Expand Down Expand Up @@ -100,6 +99,35 @@ impl<A: Array<Item = u8>> SmallString<A> {
}
}

/// Constructs a new `SmallString` from `SmallVec` using UTF-8 bytes.
///
/// If the provided `SmallVec` is not valid UTF-8, an error is returned.
#[inline]
pub fn from_small_vec(data: SmallVec<A>) -> Result<SmallString<A>, FromUtf8Error<A>> {
match str::from_utf8(&data) {
Ok(_) => Ok(SmallString { data }),
Err(error) => {
let buf = data.into_inner().ok().unwrap();

Err(FromUtf8Error { buf, error })
}
}
}

/// Constructs a new `SmallString` on the stack using the provided `SmallVec`
/// without checking that the array contains valid UTF-8.
///
/// # Safety
///
/// This function is unsafe because it does not check that the bytes passed
/// to it are valid UTF-8. If this constraint is violated, it may cause
/// memory unsafety issues, as the Rust standard library functions assume
/// that `&str`s are valid UTF-8.
#[inline]
pub unsafe fn from_small_vec_unchecked(data: SmallVec<A>) -> SmallString<A> {
SmallString { data }
}

/// The maximum number of bytes this string can hold inline.
#[inline]
pub fn inline_size(&self) -> usize {
Expand Down Expand Up @@ -375,6 +403,12 @@ impl<A: Array<Item = u8>> SmallString<A> {
self.data.into_inner().map_err(|data| SmallString { data })
}

/// Convert the `SmallString` into inner `SmallVec`.
#[inline]
pub fn into_small_vec(self) -> SmallVec<A> {
self.data
}

/// Retains only the characters specified by the predicate.
///
/// In other words, removes all characters `c` such that `f(c)` returns `false`.
Expand Down Expand Up @@ -817,12 +851,14 @@ impl<A: Array<Item = u8>> Ord for SmallString<A> {
}
}

impl<A: Array<Item = u8>> Hash for SmallString<A> {
// Removed because of dublicating implementation + clippy error.
// Watch `clippy::impl_hash_borrow_with_str_and_bytes`
/* impl<A: Array<Item = u8>> Hash for SmallString<A> {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self[..].hash(state)
}
}
} */

/// A draining iterator for `SmallString`.
///
Expand Down