Created
July 19, 2020 08:00
-
-
Save dacut/72bf96718bf776946000d4bea03f9f1d to your computer and use it in GitHub Desktop.
Problem with From<Error> issue...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::convert::From; | |
use std::fmt; | |
#[derive(Debug)] | |
pub struct InvalidError {} | |
impl fmt::Display for InvalidError { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
write!(f, "InvalidError") | |
} | |
} | |
pub trait IsReady { | |
type Error; | |
fn is_ready(&self) -> bool; | |
} | |
pub struct MyWrapper<T> { | |
wrapped: T, | |
} | |
impl<T> MyWrapper<T> { | |
pub fn new(wrapped: T) -> Self { | |
MyWrapper { wrapped } | |
} | |
} | |
impl<T> MyWrapper<T> | |
where | |
T: IsReady, | |
T::Error: From<InvalidError>, | |
{ | |
fn get_result(&self) -> Result<(), T::Error> { | |
if self.wrapped.is_ready() { | |
Ok(()) | |
} else { | |
Err(InvalidError{}) | |
// expected associated type `<T as IsReady>::Error` | |
// found struct `InvalidError` | |
// consider constraining the associated type `<T as IsReady>::Error` to `InvalidError` | |
// or calling a method that returns `<T as IsReady>::Error` | |
} | |
} | |
} | |
pub struct Wrapped {} | |
impl IsReady for Wrapped { | |
type Error = InvalidError; | |
fn is_ready(&self) -> bool { | |
false | |
} | |
} | |
pub fn main() { | |
let wrapped = Wrapped{}; | |
let wrapper = MyWrapper::new(wrapped); | |
println!("wrapper.get_result() = {:?}", wrapper.get_result()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment