These are chat archives for rust-lang/rust

4th
Apr 2016
John C F
@critiqjo
Apr 04 2016 11:45
Is there a way to convert io::Write to fmt::Write or vice versa? write! macro handles both, but it seems magic to me...
I want to take in a Write by a method, and have it write to it something... The more generic, the better.
Jarred Nicholls
@jnicholls
Apr 04 2016 11:52
You can create your own trait MyWrite and blanket it for fmt::Write and io::Write
John C F
@critiqjo
Apr 04 2016 12:14
@jnicholls How do you do that? Here's my try: http://is.gd/MgjHmu
Jarred Nicholls
@jnicholls
Apr 04 2016 12:48
dagger
my attempt as well
conflicting impl, didn't think that would be a problem with the trait bounds, but I guess it is
Jarred Nicholls
@jnicholls
Apr 04 2016 12:55
you may have to impl it for specific types
if you want to support some of the fmt::Write types and some of the io::Write types, then you can create a macro to whip out those impl of your custom Write trait
or just do it for the fmt::Write and blanket io::Write (that's the big one)
John C F
@critiqjo
Apr 04 2016 12:56
No, that's not possible, since I'm writing a library
Jarred Nicholls
@jnicholls
Apr 04 2016 12:56
well choose io::Write then and cut your losses
String is the only interesting one from fmt::Write anyways
John C F
@critiqjo
Apr 04 2016 12:57
Yeah... That's definitely an option...
Jarred Nicholls
@jnicholls
Apr 04 2016 12:57
I haven't seen in the wild many types explicitly impl fmt::Write
Erik Hedvall
@Ogeon
Apr 04 2016 12:57
You can actually do this if it's ok that they return different error types. That's enough to distinguish the implementations: http://is.gd/a2f71T
Jarred Nicholls
@jnicholls
Apr 04 2016 12:57
fair
John C F
@critiqjo
Apr 04 2016 12:59
Woooow!! Great! Thanks a bunch, Ogeon...
Erik Hedvall
@Ogeon
Apr 04 2016 12:59
Write<A>, Write<B> and Write<C> are practically different traits, so that's why it works. It wouldn't work with an associated type.
John C F
@critiqjo
Apr 04 2016 12:59
Thank you too, jnicholls...
Erik Hedvall
@Ogeon
Apr 04 2016 12:59
You're welcome :smile:
Jarred Nicholls
@jnicholls
Apr 04 2016 13:00
good call Ogeon
Erik Hedvall
@Ogeon
Apr 04 2016 13:01
The only problem with this is that you still can't mix them in, for example, a collection, but then I would recommend an enum.
John C F
@critiqjo
Apr 04 2016 13:03
The only problem with this is that you still can't mix them in
A single writer is problem enough
:smile: I wouldn't handle a collection of them...
Erik Hedvall
@Ogeon
Apr 04 2016 13:05
I just mentioned it, in case you were eventually looking for a more complete type erasure :smile: