Skip to content

Commit ab36f52

Browse files
A248tormol
authored andcommitted
Add conversions between AsciiString and Box<AsciiStr> (#85)
1 parent 1a91033 commit ab36f52

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/ascii_str.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ impl AsciiStr {
317317
pub fn last(&self) -> Option<AsciiChar> {
318318
self.slice.last().cloned()
319319
}
320+
321+
/// Converts a [`Box<AsciiStr>`] into a [`AsciiString`] without copying or allocating.
322+
#[cfg(feature = "alloc")]
323+
#[inline]
324+
pub fn into_ascii_string(self: Box<Self>) -> AsciiString {
325+
let slice = Box::<[AsciiChar]>::from(self);
326+
AsciiString::from(slice.into_vec())
327+
}
320328
}
321329

322330
macro_rules! impl_partial_eq {

src/ascii_string.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,16 @@ impl AsciiString {
385385
pub fn clear(&mut self) {
386386
self.vec.clear()
387387
}
388+
389+
/// Converts this [`AsciiString`] into a [`Box`]`<`[`AsciiStr`]`>`.
390+
///
391+
/// This will drop any excess capacity
392+
#[cfg(feature = "alloc")]
393+
#[inline]
394+
pub fn into_boxed_ascii_str(self) -> Box<AsciiStr> {
395+
let slice = self.vec.into_boxed_slice();
396+
Box::from(slice)
397+
}
388398
}
389399

390400
impl Deref for AsciiString {
@@ -496,6 +506,22 @@ impl Into<String> for AsciiString {
496506
}
497507
}
498508

509+
#[cfg(feature = "alloc")]
510+
impl From<Box<AsciiStr>> for AsciiString {
511+
#[inline]
512+
fn from(boxed: Box<AsciiStr>) -> Self {
513+
boxed.into_ascii_string()
514+
}
515+
}
516+
517+
#[cfg(feature = "alloc")]
518+
impl From<AsciiString> for Box<AsciiStr> {
519+
#[inline]
520+
fn from(string: AsciiString) -> Self {
521+
string.into_boxed_ascii_str()
522+
}
523+
}
524+
499525
impl<'a> From<Cow<'a, AsciiStr>> for AsciiString {
500526
fn from(cow: Cow<'a, AsciiStr>) -> AsciiString {
501527
cow.into_owned()
@@ -902,7 +928,7 @@ mod tests {
902928
use alloc::vec::Vec;
903929
#[cfg(feature = "std")]
904930
use std::ffi::CString;
905-
use AsciiChar;
931+
use ::{AsciiChar, AsciiStr};
906932

907933
#[test]
908934
fn into_string() {
@@ -971,4 +997,13 @@ mod tests {
971997
let sparkle_heart = str::from_utf8(&sparkle_heart_bytes).unwrap();
972998
assert!(fmt::write(&mut s2, format_args!("{}", sparkle_heart)).is_err());
973999
}
1000+
1001+
#[cfg(feature = "alloc")]
1002+
#[test]
1003+
fn to_and_from_box() {
1004+
let string = "abc".into_ascii_string().unwrap();
1005+
let converted: Box<AsciiStr> = Box::from(string.clone());
1006+
let converted: AsciiString = converted.into();
1007+
assert_eq!(string, converted);
1008+
}
9741009
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy