Skip to content

Commit 12bcd7f

Browse files
sunshowersThomas Bahn
authored and
Thomas Bahn
committed
implement AsAsciiStr and AsMutAsciiStr for references
This allows a method like `foo<T: AsAsciiStr>(s: T)` to accept `&AsciiStr` and `&mut AsciiStr`. The implementations here mirror those in core's `AsRef`.
1 parent 296c055 commit 12bcd7f

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/ascii_str.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,43 @@ pub trait AsMutAsciiStr {
576576
fn as_mut_ascii_str(&mut self) -> Result<&mut AsciiStr, AsAsciiStrError>;
577577
}
578578

579+
// These generic implementations mirror the generic implementations for AsRef<T> in core.
580+
impl<'a, T> AsAsciiStr for &'a T where T: AsAsciiStr + ?Sized {
581+
#[inline]
582+
fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError> {
583+
<T as AsAsciiStr>::as_ascii_str(*self)
584+
}
585+
586+
#[inline]
587+
unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr {
588+
<T as AsAsciiStr>::as_ascii_str_unchecked(*self)
589+
}
590+
}
591+
592+
impl<'a, T> AsAsciiStr for &'a mut T where T: AsAsciiStr + ?Sized {
593+
#[inline]
594+
fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError> {
595+
<T as AsAsciiStr>::as_ascii_str(*self)
596+
}
597+
598+
#[inline]
599+
unsafe fn as_ascii_str_unchecked(&self) -> &AsciiStr {
600+
<T as AsAsciiStr>::as_ascii_str_unchecked(*self)
601+
}
602+
}
603+
604+
impl<'a, T> AsMutAsciiStr for &'a mut T where T: AsMutAsciiStr + ?Sized {
605+
#[inline]
606+
fn as_mut_ascii_str(&mut self) -> Result<&mut AsciiStr, AsAsciiStrError> {
607+
<T as AsMutAsciiStr>::as_mut_ascii_str(*self)
608+
}
609+
610+
#[inline]
611+
unsafe fn as_mut_ascii_str_unchecked(&mut self) -> &mut AsciiStr {
612+
<T as AsMutAsciiStr>::as_mut_ascii_str_unchecked(*self)
613+
}
614+
}
615+
579616
impl AsAsciiStr for AsciiStr {
580617
#[inline]
581618
fn as_ascii_str(&self) -> Result<&AsciiStr, AsAsciiStrError> {
@@ -686,6 +723,26 @@ mod tests {
686723
assert_eq!(generic("A"), Ok(ascii_str));
687724
assert_eq!(generic(&b"A"[..]), Ok(ascii_str));
688725
assert_eq!(generic(ascii_str), Ok(ascii_str));
726+
assert_eq!(generic(&"A"), Ok(ascii_str));
727+
assert_eq!(generic(&ascii_str), Ok(ascii_str));
728+
assert_eq!(generic(&mut "A"), Ok(ascii_str));
729+
}
730+
731+
#[test]
732+
fn generic_as_mut_ascii_str() {
733+
fn generic_mut<C: AsMutAsciiStr + ?Sized>(
734+
c: &mut C,
735+
) -> Result<&mut AsciiStr, AsAsciiStrError> {
736+
c.as_mut_ascii_str()
737+
}
738+
739+
let mut arr_mut = [AsciiChar::B];
740+
let mut ascii_str_mut: &mut AsciiStr = arr_mut.as_mut().into();
741+
// Need a second reference to prevent overlapping mutable borrows
742+
let mut arr_mut_2 = [AsciiChar::B];
743+
let mut ascii_str_mut_2: &mut AsciiStr = arr_mut_2.as_mut().into();
744+
assert_eq!(generic_mut(&mut ascii_str_mut), Ok(&mut *ascii_str_mut_2));
745+
assert_eq!(generic_mut(ascii_str_mut), Ok(&mut *ascii_str_mut_2));
689746
}
690747

691748
#[test]

0 commit comments

Comments
 (0)