11use core:: fmt:: Display ;
2+ #[ cfg( feature = "std" ) ]
23use std:: path:: { self , Path , PathBuf } ;
34
45#[ doc( hidden) ]
2122 }
2223}
2324
25+ #[ cfg( feature = "std" ) ]
2426impl < ' a > AsDisplay < ' a > for Path {
2527 type Target = path:: Display < ' a > ;
2628
@@ -30,6 +32,7 @@ impl<'a> AsDisplay<'a> for Path {
3032 }
3133}
3234
35+ #[ cfg( feature = "std" ) ]
3336impl < ' a > AsDisplay < ' a > for PathBuf {
3437 type Target = path:: Display < ' a > ;
3538
@@ -42,5 +45,37 @@ impl<'a> AsDisplay<'a> for PathBuf {
4245#[ doc( hidden) ]
4346pub trait Sealed { }
4447impl < T : Display > Sealed for & T { }
48+ #[ cfg( feature = "std" ) ]
4549impl Sealed for Path { }
50+ #[ cfg( feature = "std" ) ]
4651impl Sealed for PathBuf { }
52+
53+ // Add a synthetic second impl of AsDisplay to prevent the "single applicable
54+ // impl" rule from making too weird inference decision based on the single impl
55+ // for &T, which could lead to code that compiles with thiserror's std feature
56+ // off but breaks under feature unification when std is turned on by an
57+ // unrelated crate.
58+ #[ cfg( not( feature = "std" ) ) ]
59+ mod placeholder {
60+ use super :: { AsDisplay , Sealed } ;
61+ use core:: fmt:: { self , Display } ;
62+
63+ pub struct Placeholder ;
64+
65+ impl < ' a > AsDisplay < ' a > for Placeholder {
66+ type Target = Self ;
67+
68+ #[ inline]
69+ fn as_display ( & ' a self ) -> Self :: Target {
70+ Placeholder
71+ }
72+ }
73+
74+ impl Display for Placeholder {
75+ fn fmt ( & self , _formatter : & mut fmt:: Formatter ) -> fmt:: Result {
76+ unreachable ! ( )
77+ }
78+ }
79+
80+ impl Sealed for Placeholder { }
81+ }
0 commit comments