-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
i2c trait Error type has no bounds #84
Comments
Not that I've tried but why can't you use the associated type used for the error implementation? If to return a custom error, why not map it to your error type using the |
That works fine for a function which only uses a Read or only uses a Write. For a function using both a read or write there's no guarantee they return the same error type.
How can I do this usefully in a generic way when there is no trait implemented by the associated type? I can map any type to an unknown error which will work but there's no way of getting info about what the error is from it, so that information will be lost. |
That's a possibility but a somewhat weird one since the
That is correct. However I'm a bit puzzled how you would use the implementation dependent errors in a generic way. I really only see three options here:
The first two points can be implemented today. |
and because it's a possibility you can't compile a function which returns a single error type from either a Read or a Write. There's no way of constraining the associated types of the two things to be the same. |
@nallar In your first post you mentioned it's not possible to implement a custom conversion using I'm not sure whether the latter can be easily addressed but if you have any idea we can certainly discuss it. |
Yes, I'm getting a bit distracted from the original question. Oops.
You can, but only by discarding the error information. ('not in a useful way')
I don't know how to fix it. I don't have solutions, only problems. :( |
For what it's worth, this should work some day: trait Read {
type Error;
}
trait Write {
type Error;
}
fn do_stuff<T>(_: T) -> Result<(), <T as Read>::Error>
where
T: Read + Write,
<T as Read>::Error == <T as Write>::Error,
{
Ok(())
} Currently fails with:
Would something like the following work? pub enum MyError<R: ErrorTrait, W: ErrorTrait> {
Read(R),
Write(W),
}
pub fn do_stuff<T>(_: T)
-> Result<(), MyError<<T as Read>::Error, <T as Write>::Error>>
where
T: Read + Write,
<T as Read>::Error: ErrorTrait,
<T as Write>::Error: ErrorTrait,
{
Ok(())
} |
Thanks @hannobraun. That last one is a good solution for now. It's a bit boilerplatey but allows it to be fully generic without losing the error info. It can also implement From for both error types allowing use of ?. 🥇 e: the Your version with equality constraints will be the best once they are done. |
@nallar Great to know it works for you!
I just meant to show how to require that the error types have a common API, as a possible alternative to the equality constraint. But I should have been more explicit about that part of the example. |
This should be solved once we land #229 |
In case anybody else stumbles on this, another alternative is: pub fn do_stuff<I2C, E>(interface: &mut I2C) -> Result<(), E>
where
I2C: Read + Write,
E: From<<I2C as Read>::Error> + From<<I2C as Write>::Error>
{
interface.write(...)?;
interface.read(...)?;
Ok(());
} This gives the caller maximum flexibility to define whatever error they want, as long as it has If you want to define a custom error type (for example with pub fn do_stuff<I2C, E, WE, RE>(interface: &mut I2C) -> Result<(), E>
where
I2C: Read<Error = RE> + Write<Error = WE>,
E: From<<I2C as Read>::Error> + From<<I2C as Write>::Error>,
WE: Debug,
RE: Debug
{
interface.write(...)?;
interface.read(...)?;
Ok(());
} |
If we ever land that... |
Closing. This was solved in #296 |
84: Prepare 0.4.0-alpha.3 release r=ryankurte a=eldruin Now that rust-embedded#83 is in, it would be good to publish a new alpha release. Co-authored-by: Diego Barrios Romero <[email protected]>
Is it intentional that there are no bounds on the associated Error types for the i2c traits?
As they are not guaranteed to implement anything, it doesn't seem to be possible to make a custom
Error
type which can implementFrom
for them in a useful way.It would be nice to be able to write code like this:
The text was updated successfully, but these errors were encountered: