Open
Description
use std::path::{Path,PathBuf};
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) struct PathBasedToolchain(PathBuf);
impl TryFrom<&Path> for PathBasedToolchain {
type Error = anyhow::Error;
fn try_from(value: &Path) -> std::result::Result<Self, Self::Error> {
Ok(PathBasedToolchain(value.into()))
}
}
fn main() -> anyhow::Result<()> {
let path = PathBuf::new();
let t = PathBasedToolchain::try_from(&path);
let u = PathBasedToolchain::try_from(&path as &Path);
Ok(())
}
(playground link)[https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=53696b2684233ffd81680129f6c076d2]
I expected to see this happen: compile with no error (because of https://doc.rust-lang.org/book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods and https://doc.rust-lang.org/stable/std/path/struct.PathBuf.html#impl-Deref-for-PathBuf ).
Instead, this happened:
Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `PathBasedToolchain: From<&PathBuf>` is not satisfied
--> src/lib.rs:17:43
|
17 | let t = PathBasedToolchain::try_from(&path);
| ---------------------------- ^^^^^ the trait `From<&PathBuf>` is not implemented for `PathBasedToolchain`
| |
| required by a bound introduced by this call
|
= help: the trait `TryFrom<&Path>` is implemented for `PathBasedToolchain`
= note: required for `&PathBuf` to implement `Into<PathBasedToolchain>`
= note: required for `PathBasedToolchain` to implement `TryFrom<&PathBuf>`
error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `PathBasedToolchain: From<&PathBuf>` is not satisfied
--> src/lib.rs:17:14
|
17 | let t = PathBasedToolchain::try_from(&path);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<&PathBuf>` is not implemented for `PathBasedToolchain`
|
= help: the trait `TryFrom<&Path>` is implemented for `PathBasedToolchain`
= note: required for `&PathBuf` to implement `Into<PathBasedToolchain>`
= note: required for `PathBasedToolchain` to implement `TryFrom<&PathBuf>`
Meta
rustc --version --verbose
:
Nightly channel
Build using the Nightly version: 1.70.0-nightly
(2023-03-31 5e1d3299a290026b8578)
This doesn't cause a backtrace from the compiler.
Backtrace
<backtrace>