These are chat archives for rust-lang/rust

26th
Apr 2019
Mikael Call
@MikaelCall
Apr 26 11:51

How can I put a trait bound on the TryFrom::Error?

I'd like to do the following:

T::try_from(metadata_map).map_err(|err| CloudFetcherError::InvalidMetadata(err.to_string()))?;

So I need something like

 T: TryFrom<HashMap<String, String>> + TryFrom<Error: std::string::ToString>,

But something is not working :)

It seems like adding an error parameter works, i.e.

    T: TryFrom<HashMap<String, String>, Error = E> + Debug,
    E: std::string::ToString,

Is that the best way to go?

Denis Lisov
@tanriol
Apr 26 11:58
T::Error: std::string::ToString or, if ambiguous, something like <T as TryFrom<HashMap<String, String>>>::Error: std::string::ToString
Syntax you've been trying has been accepted in RFC 2289, but hasn't been implemented yet.
Mikael Call
@MikaelCall
Apr 26 12:02
Thx, I will go with
where
    T: TryFrom<HashMap<String, String>> + Debug,
    T::Error: ToString,
The RFC you mentioned is where I got it :-)
Denis Lisov
@tanriol
Apr 26 12:03
Also note that T: TryFrom<HashMap<String, String>> + TryFrom<Error: std::string::ToString> is slightly incorrect: it could be satisfied with two different TryFrom impls, which is likely not what you mean :-)
Mikael Call
@MikaelCall
Apr 26 12:04
That is a good and subtle point!
Wouldn't the compiler complain when I do to_string() in that case?
Ingvar Stepanyan
@RReverser
Apr 26 12:06
What @tanriol is trying to say is that you want a single trait bound like TryFrom<HashMap<String, String>, Error: std::string::ToString> to make it work
(with this new proposed syntax)
Otherwise you're potentially matching two different trait impls and yeah, compiler would complain
Mikael Call
@MikaelCall
Apr 26 12:07
Excellent, thx for teaching me.
Marcin Kumorek
@krnik
Apr 26 12:56

Hi, does anyone knows why macro

#[proc_macro_derive(Nody)]
pub fn derive(tokens: TokenStream) -> TokenStream {
    "".parse().unwrap()
}

doubles the definition of derived item?

#[derive(Clone, Deserialize, Serialize, Debug, Nody)]
pub struct Ability;

I mean, it clearly should not. The code results in error in my case that states:
the name 'Ability' is defined multiple times.

When I remove the Nody trait derive it works fine.

Zakarum
@omni-viral
Apr 26 12:58
@krnik Are you sure that derive function is defined like that?
Looks more like it just returns original tokens
Marcin Kumorek
@krnik
Apr 26 13:00
Yeah, the error message says that it returns original tokens. But that's the case, how does it returns the input tokens if there is empty string being parsed to TokenStream?
Zakarum
@omni-viral
Apr 26 13:00

Could it be that you previously wrote it like

#[proc_macro_derive(Nody)]
pub fn derive(tokens: TokenStream) -> TokenStream {
    tokens
}

and cached version being used?

Marcin Kumorek
@krnik
Apr 26 13:01
Damn, cache, that's probably it. Thanks!
And sorry for such silly question.
Ichoran
@Ichoran
Apr 26 18:20
What crate do people use for statistics in Rust? The most popular in crates.io seems to be criterion-stats, which explicitly says that it could change arbitrarily much at any time, which seems rather undesirable. Everything else seems barely touched and not complete enough to be of much use.
Aha, maybe statrs.
Or just average if needs are simple.