-
Notifications
You must be signed in to change notification settings - Fork 13.4k
TryFrom / TryInto API Docs and Infallible #97866
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
I don't think this usage is common enough that we need to put an example on the docs. Most of the times you're generic over the error type, or need a concrete error type, but not either of error types. |
I'm not intentionally trying to be generic over multiple error types here, but rather just I'm trying to get a function to take either Supplying As far as I understand, these types are provided to allow designing flexible APIs that "overload" over compatible types. I don't see how that usage is uncommon: It's likely I'm misunderstanding something. Side note: Will stabilizing the |
You can do this instead: pub fn greet<'a, N>(name: N) -> Result<(), ModError>
where
N: TryInto<Name<'a>>,
ModError: From<N::Error>,
{
let name: Name<'a> = name.try_into()?;
println!("Hello, {}!", name.string());
Ok(())
} A minor improvement, perhaps, but at least you don't need the explicit
You'll still need the |
You'll still need it because it's only a reserved impl (unless it'll be a blocker for stabilization). |
Ah, so it is. For anyone interested see #57012 (comment) and #64631 (comment). |
I'm going to close this as not a bug; we can't document every possible use case and I agree with @ChayimFriedman2 that this one isn't common enough to be in the official docs. |
I had a hard time figuring out how design a function signature to pass an object to a method that can take either a
Thing
or aTryInto<Thing, Error=MyError>
.I think there should be an example on how to do this somewhere in the API docs, probably under
TryInto
.Here's what it took me to get this working:
The text was updated successfully, but these errors were encountered: