pagerduty-rs icon indicating copy to clipboard operation
pagerduty-rs copied to clipboard

Generic parameters and Into<Cow>

Open aldanor opened this issue 9 years ago • 4 comments

Hi, just stumbled upon this from your blog post :) Just a minor note, if you want things to make as nice as possible, you may want to not restrict the string type to be the same when a function accepts multiple parameters:

    pub fn image<S>(src: S, href: Option<S>, alt: Option<S>) -> Context<'a>
        where S: Into<Cow<'a, str>>

should probably be

    pub fn image<S1, S2, S3>(src: S1, href: Option<S2>, alt: Option<S3>) -> Context<'a>
        where S1: Into<Cow<'a, str>>, S2: Into<Cow<'a, str>>, S3: Into<Cow<'a, str>>

(otherwise you're either only accepting all &str, or all String, but can't mix them)

The signature looks fairly ugly though, gotta give it that :)

aldanor avatar Apr 25 '16 22:04 aldanor

Interesting! I had not considered that the simple version will enforce a single type. Thanks for sharing. I'm going to leave this open until I can get around to fixing it. Sure is ugly, though :grin:

jwilm avatar Apr 25 '16 23:04 jwilm

Np! Btw, note that the lifetimes are also constrained to a single 'a here, so the structs/methods would have to use <'a, 'b, 'c, 'd, 'e> etc in order not to be too restrictive? 😄

aldanor avatar Apr 25 '16 23:04 aldanor

Couldn't we get away with introducing additional lifetimes on a per method basis and enforce 'b: 'a where 'a is the lifetime of the struct?

jwilm avatar Apr 25 '16 23:04 jwilm

Hmm I'm not sure that would work but would be interested if you find out :)

aldanor avatar Apr 26 '16 00:04 aldanor