Skip to content

Added Into{Vec<AsciiChar>, Rc<AsciiStr>, Arc<AsciiStr>} #80

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

Merged
merged 2 commits into from
Jun 9, 2022
Merged
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: 27 additions & 0 deletions src/ascii_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::ffi::{CStr, CString};

use ascii_char::AsciiChar;
use ascii_str::{AsAsciiStr, AsAsciiStrError, AsciiStr};
use std::rc::Rc;
use std::sync::Arc;

/// A growable string stored as an ASCII encoded buffer.
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -513,6 +515,13 @@ impl Into<Vec<u8>> for AsciiString {
}
}

#[allow(clippy::from_over_into)]
impl Into<Vec<AsciiChar>> for AsciiString {
fn into(self) -> Vec<AsciiChar> {
self.vec
}
}

impl<'a> From<&'a AsciiStr> for AsciiString {
#[inline]
fn from(s: &'a AsciiStr) -> Self {
Expand Down Expand Up @@ -553,6 +562,24 @@ impl From<AsciiString> for Box<AsciiStr> {
}
}

#[allow(clippy::from_over_into)]
impl Into<Rc<AsciiStr>> for AsciiString {
fn into(self) -> Rc<AsciiStr> {
let var: Rc<[AsciiChar]> = self.vec.into();
// SAFETY: AsciiStr is repr(transparent) and thus has the same layout as [AsciiChar]
unsafe { Rc::from_raw(Rc::into_raw(var) as *const AsciiStr) }
}
}

#[allow(clippy::from_over_into)]
impl Into<Arc<AsciiStr>> for AsciiString {
fn into(self) -> Arc<AsciiStr> {
let var: Arc<[AsciiChar]> = self.vec.into();
// SAFETY: AsciiStr is repr(transparent) and thus has the same layout as [AsciiChar]
unsafe { Arc::from_raw(Arc::into_raw(var) as *const AsciiStr) }
}
}

impl<'a> From<Cow<'a, AsciiStr>> for AsciiString {
fn from(cow: Cow<'a, AsciiStr>) -> AsciiString {
cow.into_owned()
Expand Down