From 41e6cae66b49a0e4cac757c63efab52a9249a54b Mon Sep 17 00:00:00 2001 From: DefinitelyNotRobot <42606625+DefinitelyNotRobot@users.noreply.github.com> Date: Wed, 29 Sep 2021 09:36:09 +0800 Subject: [PATCH] Add impl From<&RelativePath> for Box (#26) Add impl From for Box Add impl From<&RelativePath> for Arc Add impl From for Arc Add impl From<&RelativePath> for Rc Add impl From for Rc Co-authored-by: DefinitelyNotRobot --- src/lib.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index e1195cd..7e78140 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -189,7 +189,7 @@ // cb2a656cdfb6400ac0200c661267f91fabf237e2 src/libstd/path.rs #![deny(missing_docs)] -#![deny(broken_intra_doc_links)] +#![deny(rustdoc::broken_intra_doc_links)] use std::borrow::{Borrow, Cow}; use std::cmp; @@ -200,7 +200,9 @@ use std::iter::FromIterator; use std::mem; use std::ops::{self, Deref}; use std::path; +use std::rc::Rc; use std::str; +use std::sync::Arc; #[cfg(feature = "serde")] extern crate serde; @@ -1509,6 +1511,60 @@ impl RelativePath { } } +impl From<&RelativePath> for Box { + #[inline] + fn from(path: &RelativePath) -> Box { + let boxed: Box = path.inner.into(); + let rw = Box::into_raw(boxed) as *mut RelativePath; + unsafe { Box::from_raw(rw) } + } +} + +impl From for Box { + #[inline] + fn from(path: RelativePathBuf) -> Box { + let boxed: Box = path.inner.into(); + let rw = Box::into_raw(boxed) as *mut RelativePath; + unsafe { Box::from_raw(rw) } + } +} + +impl From<&RelativePath> for Arc { + #[inline] + fn from(path: &RelativePath) -> Arc { + let arc: Arc = path.inner.into(); + let rw = Arc::into_raw(arc) as *const RelativePath; + unsafe { Arc::from_raw(rw) } + } +} + +impl From for Arc { + #[inline] + fn from(path: RelativePathBuf) -> Arc { + let arc: Arc = path.inner.into(); + let rw = Arc::into_raw(arc) as *const RelativePath; + unsafe { Arc::from_raw(rw) } + } +} + +impl From<&RelativePath> for Rc { + #[inline] + fn from(path: &RelativePath) -> Rc { + let rc: Rc = path.inner.into(); + let rw = Rc::into_raw(rc) as *const RelativePath; + unsafe { Rc::from_raw(rw) } + } +} + +impl From for Rc { + #[inline] + fn from(path: RelativePathBuf) -> Rc { + let rc: Rc = path.inner.into(); + let rw = Rc::into_raw(rc) as *const RelativePath; + unsafe { Rc::from_raw(rw) } + } +} + impl ToOwned for RelativePath { type Owned = RelativePathBuf; @@ -1747,6 +1803,8 @@ impl_cmp_str!(&'a RelativePath, String); #[cfg(test)] mod tests { use super::*; + use std::rc::Rc; + use std::sync::Arc; use std::path::Path; macro_rules! t( @@ -2371,6 +2429,15 @@ mod tests { ); assert_eq!(rp("foo/bar").to_owned(), RelativePathBuf::from("foo/bar"),); + + assert_eq!(&*Box::::from(rp("foo/bar")), rp("foo/bar")); + assert_eq!(&*Box::::from(RelativePathBuf::from("foo/bar")), rp("foo/bar")); + + assert_eq!(&*Arc::::from(rp("foo/bar")), rp("foo/bar")); + assert_eq!(&*Arc::::from(RelativePathBuf::from("foo/bar")), rp("foo/bar")); + + assert_eq!(&*Rc::::from(rp("foo/bar")), rp("foo/bar")); + assert_eq!(&*Rc::::from(RelativePathBuf::from("foo/bar")), rp("foo/bar")); } #[test]