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

add const function to get &str from ArrayString #231

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
27 changes: 17 additions & 10 deletions src/array_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl<const CAP: usize> ArrayString<CAP>
///
/// ```
/// use arrayvec::ArrayString;
///
///
/// let mut s = ArrayString::<3>::from("foo").unwrap();
///
/// assert_eq!(s.pop(), Some('o'));
Expand Down Expand Up @@ -336,7 +336,7 @@ impl<const CAP: usize> ArrayString<CAP>
pub fn truncate(&mut self, new_len: usize) {
if new_len <= self.len() {
assert!(self.is_char_boundary(new_len));
unsafe {
unsafe {
// In libstd truncate is called on the underlying vector,
// which in turns drops each element.
// As we know we don't have to worry about Drop,
Expand All @@ -356,7 +356,7 @@ impl<const CAP: usize> ArrayString<CAP>
///
/// ```
/// use arrayvec::ArrayString;
///
///
/// let mut s = ArrayString::<3>::from("foo").unwrap();
///
/// assert_eq!(s.remove(0), 'f');
Expand Down Expand Up @@ -412,24 +412,31 @@ impl<const CAP: usize> ArrayString<CAP>
self
}

fn as_ptr(&self) -> *const u8 {
const fn as_ptr(&self) -> *const u8 {
self.xs.as_ptr() as *const u8
}

fn as_mut_ptr(&mut self) -> *mut u8 {
self.xs.as_mut_ptr() as *mut u8
}

/// Provide a &str in constant contexts where the dereference
/// trait is not available.
#[inline]
pub const fn as_str_const(&self) -> &str {
unsafe {
let sl = slice::from_raw_parts(self.as_ptr(), self.len());
str::from_utf8_unchecked(sl)
}
}
}

impl<const CAP: usize> Deref for ArrayString<CAP>
{
type Target = str;
#[inline]
fn deref(&self) -> &str {
unsafe {
let sl = slice::from_raw_parts(self.as_ptr(), self.len());
str::from_utf8_unchecked(sl)
}
self.as_str_const()
}
}

Expand Down Expand Up @@ -466,7 +473,7 @@ impl<const CAP: usize> PartialEq<ArrayString<CAP>> for str
}
}

impl<const CAP: usize> Eq for ArrayString<CAP>
impl<const CAP: usize> Eq for ArrayString<CAP>
{ }

impl<const CAP: usize> Hash for ArrayString<CAP>
Expand Down Expand Up @@ -587,7 +594,7 @@ impl<const CAP: usize> Serialize for ArrayString<CAP>

#[cfg(feature="serde")]
/// Requires crate feature `"serde"`
impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
impl<'de, const CAP: usize> Deserialize<'de> for ArrayString<CAP>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>
Expand Down
14 changes: 13 additions & 1 deletion tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,19 @@ fn test_string() {
let tmut: &mut str = &mut t;
assert_eq!(tmut, "ab");

// test ArrayString -> &str in const fn
let arr_str = ArrayString::<2>::from("ab").unwrap();
const fn get_second_char(a: ArrayString<2>) -> char {
match a.as_str_const().as_bytes() {
[_char1, char2] => (*char2) as char,
[_char1] => panic!(),
_ => unreachable!(),
}
}
assert_eq!(get_second_char(arr_str), 'b');

// Test Error trait / try

let t = || -> Result<(), Box<dyn Error>> {
let mut t = ArrayString::<2>::new();
t.try_push_str(text)?;
Expand Down Expand Up @@ -790,4 +802,4 @@ fn test_arraystring_zero_filled_has_some_sanity_checks() {
let string = ArrayString::<4>::zero_filled();
assert_eq!(string.as_str(), "\0\0\0\0");
assert_eq!(string.len(), 4);
}
}