New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Sink::SinkItem a generic parameter #623
Comments
Thanks for the report! It's interesting how this is pretty different from I wonder, though, if we could perhaps make this some sort of combinator? In any case this'll be a long-term issue as we can't change |
This issue is currently blocking me. I'm writing a typesafe interface for a multiplexing rpc protocol. Basically I want to open up multiple "virtual" sinks multiplexed over one "real" sink: // This is just a sketch, not actual code. In particular, I don't need to consume
// the `T` to get a value accepted by the underlying sink. So I can actually
// return a `T` if the underlying sink doesn't accept the item.
pub struct MultiplexedSink<T>(UnderlyingSinkOfVecU8s);
impl<T: Into<Vec<u8>>> Sink<T> for MultiplexedSink<T> {
fn start_send(&mut self, item: T) -> StartSend<T, Self::SinkError> {
self.0.start_send(item.into())
}
} But currently, the best I can do is the following, which loses all the type-safety. pub struct MultiplexedSink(UnderlyingSinkOfVecU8s);
impl Sink for MultiplexedSink {
fn start_send(&mut self, item: Vec<u8>) -> StartSend<Vec<u8>, Self::SinkError> {
self.0.start_send(item)
}
} The actual code uses serde traits and is fully independent of the actual serialization format. So while I could do better than just taking raw The alternative would be to just define my own As for "some sort of combinator", I'd be glad for any suggestions on how to do that, I currently don't see a way of implementing this. So I really hope this change will land in this crate at some point, and I'm happy to contribute the necessary code myself. But even then, it looks like this would still be blocked until all other changes for 0.2.0 are done, right? |
@alexcrichton are there any plans for making this change, or will this be dropped? |
@AljoschaMeyer We've been talking about a number of different possibilities for refactoring the |
Ping @cramertj, @alexcrichton — should a concrete decision be made on this for futures-preview 0.3? |
@shepmaster I made a prototype a while ago, but had to decide what to do about unifying the error type in the |
Sadly, I have nothing useful to offer. I just followed links here from a similar problem and figured I could (a) poke you to see if there were any problems with the proposed implementation and (b) help make sure it didn't get lost among the bigger pile of work 😉 |
I was thinking about the need for this change, since we've finished up a similar change in tower, but I don't follow what you mean with this comment. What problem arose that needed "unifying the error type"? |
@seanmonstar I wanted to accept a type that was a |
Oh I see what you mean! Hm, could there be cases where the error type were actually different? If a |
@seanmonstar yeah, I could imagine trying to do something like that. My gut feeling is that being able to return the item is an uncommon case, and not terribly important to support, but I don't have strong feelings either way. |
See tokio-rs/tokio-core#273.
This would allow
Sink
s to accept values of multiple different types. I'm sure there's a reason it's an associated type right now, but I can't quite figure out why that is.Example:
The same goes for the tokio-io Encoder trait:
This would solve this tokio-io issue, because users could write an impl like:
Conceptually,
Item
is an input to aSync
orEncoder
, rather than an output like it is inStream
,Future
, andIterator
. As an input, it makes sense for it to be variant. For other examples of this design, see theFn<...>
traits, which useArgs
as a generic parameter, andOutput
as an associated type.The text was updated successfully, but these errors were encountered: