Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from dtolnay/path
Support interpolating paths as if they had a Display impl
- Loading branch information
Showing
7 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use std::fmt::Display; | ||
use std::path::{self, Path, PathBuf}; | ||
|
||
pub trait DisplayAsDisplay { | ||
fn as_display(&self) -> Self; | ||
} | ||
|
||
impl<T: Display> DisplayAsDisplay for &T { | ||
fn as_display(&self) -> Self { | ||
self | ||
} | ||
} | ||
|
||
pub trait PathAsDisplay { | ||
fn as_display(&self) -> path::Display<'_>; | ||
} | ||
|
||
impl PathAsDisplay for Path { | ||
fn as_display(&self) -> path::Display<'_> { | ||
self.display() | ||
} | ||
} | ||
|
||
impl PathAsDisplay for PathBuf { | ||
fn as_display(&self) -> path::Display<'_> { | ||
self.display() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use ref_cast::RefCast; | ||
use std::fmt::Display; | ||
use std::path::{Path, PathBuf}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
#[error("failed to read '{file}'")] | ||
struct StructPathBuf { | ||
file: PathBuf, | ||
} | ||
|
||
#[derive(Error, Debug, RefCast)] | ||
#[repr(C)] | ||
#[error("failed to read '{file}'")] | ||
struct StructPath { | ||
file: Path, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
enum EnumPathBuf { | ||
#[error("failed to read '{0}'")] | ||
Read(PathBuf), | ||
} | ||
|
||
fn assert<T: Display>(expected: &str, value: T) { | ||
assert_eq!(expected, value.to_string()); | ||
} | ||
|
||
#[test] | ||
fn test_display() { | ||
let path = Path::new("/thiserror"); | ||
let file = path.to_owned(); | ||
assert("failed to read '/thiserror'", StructPathBuf { file }); | ||
let file = path.to_owned(); | ||
assert("failed to read '/thiserror'", EnumPathBuf::Read(file)); | ||
assert("failed to read '/thiserror'", StructPath::ref_cast(path)); | ||
} |