|
1 | | -use std::fmt::{self, Display}; |
2 | | -use std::path::{Path, PathBuf}; |
| 1 | +use std::fmt::Display; |
| 2 | +use std::path::{self, Path, PathBuf}; |
3 | 3 |
|
4 | 4 | #[doc(hidden)] |
5 | | -pub trait AsDisplay { |
6 | | - type Target: Display + ?Sized; |
| 5 | +pub trait AsDisplay<'a> { |
| 6 | + type Target: Display; |
7 | 7 |
|
8 | | - fn as_display(&self) -> &Self::Target; |
| 8 | + fn as_display(&'a self) -> Self::Target; |
9 | 9 | } |
10 | 10 |
|
11 | | -impl<T: Display> AsDisplay for &T { |
12 | | - type Target = T; |
| 11 | +impl<'a, T> AsDisplay<'a> for &T |
| 12 | +where |
| 13 | + T: Display + 'a, |
| 14 | +{ |
| 15 | + type Target = &'a T; |
13 | 16 |
|
14 | | - fn as_display(&self) -> &Self::Target { |
15 | | - self |
| 17 | + fn as_display(&'a self) -> Self::Target { |
| 18 | + *self |
16 | 19 | } |
17 | 20 | } |
18 | 21 |
|
19 | | -impl AsDisplay for Path { |
20 | | - type Target = PathDisplay; |
| 22 | +impl<'a> AsDisplay<'a> for Path { |
| 23 | + type Target = path::Display<'a>; |
21 | 24 |
|
22 | 25 | #[inline] |
23 | | - fn as_display(&self) -> &Self::Target { |
24 | | - PathDisplay::new(self) |
| 26 | + fn as_display(&'a self) -> Self::Target { |
| 27 | + self.display() |
25 | 28 | } |
26 | 29 | } |
27 | 30 |
|
28 | | -impl AsDisplay for PathBuf { |
29 | | - type Target = PathDisplay; |
| 31 | +impl<'a> AsDisplay<'a> for PathBuf { |
| 32 | + type Target = path::Display<'a>; |
30 | 33 |
|
31 | 34 | #[inline] |
32 | | - fn as_display(&self) -> &Self::Target { |
33 | | - PathDisplay::new(self.as_path()) |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -#[doc(hidden)] |
38 | | -#[repr(transparent)] |
39 | | -pub struct PathDisplay(Path); |
40 | | - |
41 | | -impl PathDisplay { |
42 | | - #[inline] |
43 | | - fn new(path: &Path) -> &Self { |
44 | | - // SAFETY: PathDisplay is repr(transparent) so casting pointers between |
45 | | - // it and its payload is safe. |
46 | | - unsafe { &*(path as *const Path as *const Self) } |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -impl Display for PathDisplay { |
51 | | - #[inline] |
52 | | - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
53 | | - self.0.display().fmt(formatter) |
| 35 | + fn as_display(&'a self) -> Self::Target { |
| 36 | + self.display() |
54 | 37 | } |
55 | 38 | } |
0 commit comments