Try to get rid of owned types
Inspired by https://www.youtube.com/watch?v=A4cKi7PTJSs. This turns a lot of cloning Strings and PathBufs into Rcs.
This commit is contained in:
parent
e3b425eb1c
commit
d091265d27
20 changed files with 343 additions and 232 deletions
|
|
@ -1,13 +1,24 @@
|
|||
use std::path::{Path as ActualPath, PathBuf};
|
||||
use std::path::{self, PathBuf};
|
||||
use std::rc::Rc;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Path(PathBuf);
|
||||
pub struct Path(Rc<path::Path>);
|
||||
|
||||
// FIXME: This is a specialization since with Path: Into<PathBuf>
|
||||
impl Path {
|
||||
pub(crate) fn clone_rc(&self) -> Rc<path::Path> {
|
||||
Rc::clone(&self.0)
|
||||
}
|
||||
|
||||
pub fn join(&self, path: impl AsRef<path::Path>) -> Self {
|
||||
Self::from(self.0.join(path))
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: This is a specialization since with Path: AsRef<path::Path>
|
||||
// it would overwrite impl<T> From <T> for T
|
||||
//impl<T: Into<PathBuf>> From<T> for Path {
|
||||
//impl<T: AsRef<path::Path>> From<T> for Path {
|
||||
// fn from(v: T) -> Self {
|
||||
// Path(v.into())
|
||||
// Self(v.as_ref().into())
|
||||
// }
|
||||
//}
|
||||
|
||||
|
|
@ -15,7 +26,8 @@ macro_rules! path_from {
|
|||
( $t:ty ) => {
|
||||
impl From<$t> for Path {
|
||||
fn from(v: $t) -> Self {
|
||||
Self(v.into())
|
||||
let path: &path::Path = v.as_ref();
|
||||
Self(path.into())
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -25,23 +37,35 @@ path_from!(String);
|
|||
path_from!(&str);
|
||||
path_from!(PathBuf);
|
||||
|
||||
impl From<Path> for PathBuf {
|
||||
impl From<Rc<path::Path>> for Path {
|
||||
fn from(v: Rc<path::Path>) -> Self {
|
||||
Self(v)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<path::Path> for Path {
|
||||
fn as_ref(&self) -> &path::Path {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Path> for Rc<path::Path> {
|
||||
fn from(v: Path) -> Self {
|
||||
v.0
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<ActualPath> for Path {
|
||||
fn as_ref(&self) -> &ActualPath {
|
||||
&self.0
|
||||
impl From<&Path> for Rc<path::Path> {
|
||||
fn from(v: &Path) -> Self {
|
||||
v.0.clone()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct UserName(pub String);
|
||||
pub struct UserName(pub Rc<str>);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ServiceName(pub String);
|
||||
pub struct ServiceName(pub Rc<str>);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct DatabaseName(pub String);
|
||||
pub struct DatabaseName(pub Rc<str>);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue