Hi! Let's assume I have two traits Sink and Source and want to have an enum representing the owned variants of their trait objects Box<dyn T> and one that represents references to them &dyn T. Currently I have this:
#[derive(Clone, Copy)]
pub enum SinkOrSourceRef<'a> {
Sink(&'a dyn Sink),
Source(&'a dyn Source),
}
pub enum SinkOrSource {
Sink(Box<dyn Sink>),
Source(Box<dyn Source>),
}
How would I implement AsRef, Deref or any such trait so that I can create references of type SinkOrSourceRef for SinkOrSource? AsRef and Deref need the conversion methods to return references, but I have this sort of smart pointer. I tried this:
impl<'a> AsRef<SinkOrSourceRef<'a>> for SinkOrSource {
fn as_ref(&self) -> &SinkOrSourceRef<'a> {
match self {
SinkOrSource::Sink(o) => &SinkOrSourceRef::Sink(o.as_ref()),
SinkOrSource::Source(o) => &SinkOrSourceRef::Source(o.as_ref()),
}
}
}
However this does not work since: error[E0515]: cannot return value referencing temporary value
Any hint on how to set this up correctly?