rust - Implementing the From trait for a ParseIntError -
when using try! macro, uses trait transform error desired error.
i transform errors own type. goes great e.g. io::error, can't work error type core.
use std::io; pub struct parsererror { pub message: string, } impl from<io::error> parsererror { fn from(e: io::error) -> parsererror { parsererror{message: format!("generic io error: {}", e.description())} } }
this works doing try! on io. parsing:
fn parse_string(s: &str) -> result<u64, parsererror> { let = try!(s.parse::<u64>()); return ok(i); }
my error says:
error: trait core::convert::from<parser::parsererror>
not implemented type `core::num::parseinterror
i tried implement from:
impl from<core::num::parseinterror> parsererror { fn from(_: core::num::parseinterror) -> parsererror { parsererror{message: "invalid data type".to_string()} } }
but can't core imported. how this?
the modules core
reexported std
. can fix error replacing core
std
in code:
impl from<std::num::parseinterror> parsererror { fn from(_: std::num::parseinterror) -> parsererror { parsererror{message: "invalid data type".to_string()} } }
Comments
Post a Comment