Skip to content

Commit eb0ea87

Browse files
committed
Convert Into<> impls to From<> impls
A follop-up on #80 (commit 7b12e69).
1 parent 7f4e47c commit eb0ea87

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

src/ascii_string.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -498,16 +498,14 @@ impl From<AsciiChar> for AsciiString {
498498
}
499499
}
500500

501-
// FIXME? Turn this into a `From` impl
502-
#[allow(clippy::from_over_into)]
503-
impl Into<Vec<u8>> for AsciiString {
504-
fn into(mut self) -> Vec<u8> {
501+
impl From<AsciiString> for Vec<u8> {
502+
fn from(mut s: AsciiString) -> Vec<u8> {
505503
// SAFETY: All ascii bytes are valid `u8`, as we are `repr(u8)`.
506504
// Note: We forget `self` to avoid `self.vec` from being deallocated.
507-
let ptr = self.vec.as_mut_ptr().cast::<u8>();
508-
let length = self.vec.len();
509-
let capacity = self.vec.capacity();
510-
mem::forget(self);
505+
let ptr = s.vec.as_mut_ptr().cast::<u8>();
506+
let length = s.vec.len();
507+
let capacity = s.vec.capacity();
508+
mem::forget(s);
511509

512510
// SAFETY: We guarantee all invariants due to getting `ptr`, `length`
513511
// and `capacity` from a `Vec`. We also guarantee `ptr` is valid
@@ -516,10 +514,9 @@ impl Into<Vec<u8>> for AsciiString {
516514
}
517515
}
518516

519-
#[allow(clippy::from_over_into)]
520-
impl Into<Vec<AsciiChar>> for AsciiString {
521-
fn into(self) -> Vec<AsciiChar> {
522-
self.vec
517+
impl From<AsciiString> for Vec<AsciiChar> {
518+
fn from(s: AsciiString) -> Vec<AsciiChar> {
519+
s.vec
523520
}
524521
}
525522

@@ -537,13 +534,11 @@ impl<'a> From<&'a [AsciiChar]> for AsciiString {
537534
}
538535
}
539536

540-
// FIXME? Turn this into a `From` impl
541-
#[allow(clippy::from_over_into)]
542-
impl Into<String> for AsciiString {
537+
impl From<AsciiString> for String {
543538
#[inline]
544-
fn into(self) -> String {
539+
fn from(s: AsciiString) -> String {
545540
// SAFETY: All ascii bytes are `utf8`.
546-
unsafe { String::from_utf8_unchecked(self.into()) }
541+
unsafe { String::from_utf8_unchecked(s.into()) }
547542
}
548543
}
549544

@@ -561,19 +556,17 @@ impl From<AsciiString> for Box<AsciiStr> {
561556
}
562557
}
563558

564-
#[allow(clippy::from_over_into)]
565-
impl Into<Rc<AsciiStr>> for AsciiString {
566-
fn into(self) -> Rc<AsciiStr> {
567-
let var: Rc<[AsciiChar]> = self.vec.into();
559+
impl From<AsciiString> for Rc<AsciiStr> {
560+
fn from(s: AsciiString) -> Rc<AsciiStr> {
561+
let var: Rc<[AsciiChar]> = s.vec.into();
568562
// SAFETY: AsciiStr is repr(transparent) and thus has the same layout as [AsciiChar]
569563
unsafe { Rc::from_raw(Rc::into_raw(var) as *const AsciiStr) }
570564
}
571565
}
572566

573-
#[allow(clippy::from_over_into)]
574-
impl Into<Arc<AsciiStr>> for AsciiString {
575-
fn into(self) -> Arc<AsciiStr> {
576-
let var: Arc<[AsciiChar]> = self.vec.into();
567+
impl From<AsciiString> for Arc<AsciiStr> {
568+
fn from(s: AsciiString) -> Arc<AsciiStr> {
569+
let var: Arc<[AsciiChar]> = s.vec.into();
577570
// SAFETY: AsciiStr is repr(transparent) and thus has the same layout as [AsciiChar]
578571
unsafe { Arc::from_raw(Arc::into_raw(var) as *const AsciiStr) }
579572
}

0 commit comments

Comments
 (0)