Hi, I am having trouble using Deref in this more complicated case. Hoping someone can help, or let me know if it's impossible. The full context is at: https://github.com/krisprice/ipnet/blob/master/src/ipnet.rs
I have an enum IpNet
that contains two structs Ipv4Net
and Ipv6Net
as follows.
pub enum IpNet {
V4(Ipv4Net),
V6(Ipv6Net),
}
pub struct Ipv4Net {
addr: Ipv4Addr,
prefix_len: u8,
}
pub struct Ipv6Net {
addr: Ipv6Addr,
prefix_len: u8,
}
For the Ipv4Net
and Ipv6Net
types I am using Deref to call methods on the contained Ipv4Addr
and Ipv6Addr
types respectively, as follows. This works fine, as expected.
impl Deref for Ipv4Net {
type Target = Ipv4Addr;
fn deref(&self) -> &Self::Target {
&self.addr
}
}
impl Deref for Ipv6Net {
type Target = Ipv6Addr;
fn deref(&self) -> &Self::Target {
&self.addr
}
}
But I have no idea how to implement this for the IpNet
enum, or if it's even possible, my very naive attempt at trying to make this work below seems clumsy and doesn't work.
impl Deref for IpNet {
type Target = IpAddr;
fn deref(&self) -> &Self::Target {
match *self {
IpNet::V4(ref n) => &IpAddr::from(n.addr.clone()),
IpNet::V6(ref n) => &IpAddr::from(n.addr.clone()),
}
}
}
This results in the error below. (Just showing one of the errors, but it's repeated for each match arm.)
--> src/ipnet.rs:197:34
|
197 | IpNet::V4(ref n) => &IpAddr::from(n.addr.clone()),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^-
| | |
| | temporary value only lives until here
| does not live long enough
|
note: borrowed value must be valid for the anonymous lifetime #1 defined on the method body at 195:5...
--> src/ipnet.rs:195:5
|
195 | / fn deref(&self) -> &Self::Target {
196 | | match *self {
197 | | IpNet::V4(ref n) => &IpAddr::from(n.addr.clone()),
198 | | IpNet::V6(ref n) => &IpAddr::from(n.addr.clone()),
199 | | }
200 | | }
| |_____^