diff --git a/Cargo.toml b/Cargo.toml index f6a55b3..295d7c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "smallstr" -version = "0.3.0" +version = "0.3.1" authors = ["Murarth "] edition = "2018" diff --git a/src/string.rs b/src/string.rs index 3c13c69..dcc0e09 100644 --- a/src/string.rs +++ b/src/string.rs @@ -2,7 +2,6 @@ use core::{ borrow::{Borrow, BorrowMut}, cmp::Ordering, fmt, - hash::{Hash, Hasher}, iter::FromIterator, ops, ptr, slice, str::{self, Chars, Utf8Error}, @@ -100,6 +99,35 @@ impl> SmallString { } } + /// 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) -> Result, FromUtf8Error> { + 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) -> SmallString { + SmallString { data } + } + /// The maximum number of bytes this string can hold inline. #[inline] pub fn inline_size(&self) -> usize { @@ -375,6 +403,12 @@ impl> SmallString { self.data.into_inner().map_err(|data| SmallString { data }) } + /// Convert the `SmallString` into inner `SmallVec`. + #[inline] + pub fn into_small_vec(self) -> SmallVec { + self.data + } + /// Retains only the characters specified by the predicate. /// /// In other words, removes all characters `c` such that `f(c)` returns `false`. @@ -817,12 +851,14 @@ impl> Ord for SmallString { } } -impl> Hash for SmallString { +// Removed because of dublicating implementation + clippy error. +// Watch `clippy::impl_hash_borrow_with_str_and_bytes` +/* impl> Hash for SmallString { #[inline] fn hash(&self, state: &mut H) { self[..].hash(state) } -} +} */ /// A draining iterator for `SmallString`. ///