Hi there,
Below this is a simplified version of the code showing the problem I run into (you can just copy paste and compile it).
The code is fine until I tried wrapping the incoming stream in a BufferStream inside the NormalStream::new()
function as shown below.
The compiler then complains:
error[E0310]: the parameter type
Tmay not live long enough --> library-runtime/src/streams/example.rs:32:15 | 29 | impl<T> NormalStream<T> { | - help: consider adding an explicit lifetime bound...:
T: 'static... 32 | stream: Box::new(BufferStream::new(stream)), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: ...so that the type
streams::example::BufferStream` will meet its required lifetime bounds
--> library-runtime/src/streams/example.rs:32:15
|
32 | stream: Box::new(BufferStream::new(stream)),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
`
Making that T into a 'static lifetime feels wrong, but I am not sure how to properly fix this. I'd really like to wrap that stream (essentially adding some kind of buffer to it).
Can this be solved in a better way and if so how to do that?
Thank you for your time!
pub trait Stream<T> {
fn next(&self) -> Option<T>;
}
pub trait StreamMark<T>: Stream<T> {
fn mark(&self);
}
pub struct BufferStream<T> {
stream: Box<dyn Stream<T>>,
}
impl<T> BufferStream<T> {
pub fn new(stream: Box<dyn Stream<T>>) -> Self {
return BufferStream { stream };
}
}
impl<T> Stream<T> for BufferStream<T> {
fn next(&self) -> Option<T> {
unimplemented!()
}
}
pub struct NormalStream<T> {
stream: Box<dyn Stream<T>>,
}
impl<T> NormalStream<T> {
pub fn new(stream: Box<dyn Stream<T>>) -> Self {
return NormalStream {
stream: Box::new(BufferStream::new(stream)), // <-- Wrapping here it's unhappy
};
}
}