Skip to content

Add more Box conversions #96

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Implement Clone for BoxAsciiStr>
  • Loading branch information
tormol committed Sep 18, 2022
commit 9dca9340e490d6692ddc461e94afb8a96d6d4f64
12 changes: 9 additions & 3 deletions src/ascii_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@
loop {
if valid == b.len() {
// SAFETY: `is_ascii` having returned true for all bytes guarantees all bytes are within ascii range.
return unsafe { Ok(mem::transmute(b)) };

Check failure on line 162 in src/ascii_str.rs

View workflow job for this annotation

GitHub Actions / Lint with Clippy

transmute used without annotations
} else if b[valid].is_ascii() {
valid += 1;
} else {
Expand Down Expand Up @@ -469,7 +469,7 @@
impl<'a> From<&'a [AsciiChar]> for &'a AsciiStr {
#[inline]
fn from(slice: &[AsciiChar]) -> &AsciiStr {
let ptr = slice as *const [AsciiChar] as *const AsciiStr;

Check failure on line 472 in src/ascii_str.rs

View workflow job for this annotation

GitHub Actions / Lint with Clippy

reference as raw pointer
unsafe { &*ptr }
}
}
Expand Down Expand Up @@ -514,6 +514,13 @@
}
}

#[cfg(feature = "alloc")]
impl Clone for Box<AsciiStr> {
fn clone(&self) -> Box<AsciiStr> {
self.to_ascii_string().into()
}
}

impl<'a> From<&'a AsciiStr> for &'a [AsciiChar] {
#[inline]
fn from(astr: &AsciiStr) -> &[AsciiChar] {
Expand Down Expand Up @@ -1493,10 +1500,9 @@
#[test]
#[cfg(feature = "alloc")]
fn to_and_from_byte_box() {
let s = "abc".as_ascii_str().unwrap().to_ascii_string();
let boxed = s.clone().into_boxed_ascii_str();
let s = "abc".as_ascii_str().unwrap().to_ascii_string().into_boxed_ascii_str();
unsafe {
let converted = boxed.into_boxed_bytes();
let converted = s.clone().into_boxed_bytes();
assert_eq!(&converted[..], &b"abc"[..]);
let converted_back = AsciiStr::from_boxed_ascii_bytes_unchecked(converted);
assert_eq!(&converted_back[..], &s[..]);
Expand Down
Loading