I’m trying to implement a function to map a Receiver<Vec<u8>>
to Receiver<T>
, where Receiver
is short for tokio::sync::mpsc::Receiver
. And T
is deserialized from Vec<u8>
, I’m using the bincode
crate to do that. Here’s my implementation:
<code>struct MapRx<T> {
rx: RefCell<Receiver<Vec<u8>>>,
_maker: PhantomData<T>,
}
impl<'de, T: Deserialize<'de>> Future for MapRx<T> {
type Output = Option<T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.rx.borrow_mut().poll_recv(cx) {
Poll::Ready(res) => Poll::Ready(res.map(|bytes| {
bincode::deserialize::<T>(&bytes).unwrap()
})),
Poll::Pending => Poll::Pending
}
}
}
</code>
<code>struct MapRx<T> {
rx: RefCell<Receiver<Vec<u8>>>,
_maker: PhantomData<T>,
}
impl<'de, T: Deserialize<'de>> Future for MapRx<T> {
type Output = Option<T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.rx.borrow_mut().poll_recv(cx) {
Poll::Ready(res) => Poll::Ready(res.map(|bytes| {
bincode::deserialize::<T>(&bytes).unwrap()
})),
Poll::Pending => Poll::Pending
}
}
}
</code>
struct MapRx<T> {
rx: RefCell<Receiver<Vec<u8>>>,
_maker: PhantomData<T>,
}
impl<'de, T: Deserialize<'de>> Future for MapRx<T> {
type Output = Option<T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.rx.borrow_mut().poll_recv(cx) {
Poll::Ready(res) => Poll::Ready(res.map(|bytes| {
bincode::deserialize::<T>(&bytes).unwrap()
})),
Poll::Pending => Poll::Pending
}
}
}
However, the compiler complains that bytes
variable does not live long enough, how do I fix it?