Skip to content
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

rustc(E0515) when trying to use icmp_packet_iter.next() #588

Open
zarkdav opened this issue Oct 11, 2022 · 2 comments
Open

rustc(E0515) when trying to use icmp_packet_iter.next() #588

zarkdav opened this issue Oct 11, 2022 · 2 comments

Comments

@zarkdav
Copy link

zarkdav commented Oct 11, 2022

Apologies if this is a Rust noob question.

I am trying to return the first reply packet from a Layer4(Ipv4(IpNextHeaderProtocols::Icmp)) transport_channel.

The end of my function is:

    let mut rx_iter = icmp_packet_iter(&mut rx);
    rx_iter.next()

And I get the following errors:

error[E0515]: cannot return reference to local variable `rx_iter`
  --> src\main.rs:29:5
   |
29 |     rx_iter.next()
   |     ^^^^^^^^^^^^^^ returns a reference to data owned by the current function

error[E0515]: cannot return value referencing local variable `rx`
  --> src\main.rs:29:5
   |
28 |     let mut rx_iter = icmp_packet_iter(&mut rx);
   |                                        ------- `rx` is borrowed here
29 |     rx_iter.next()
   |     ^^^^^^^^^^^^^^ returns a value referencing data owned by the current function

The official advice is to "consider returning an owned value instead". How should I do it?

@zarkdav
Copy link
Author

zarkdav commented Oct 11, 2022

I tried an std::mem::replace trick without success.

@zarkdav
Copy link
Author

zarkdav commented Oct 12, 2022

I think I found a solution by replacing rx_iter.next() with:

    let reply_packet = rx_iter.next();
    match reply_packet {
        Ok((p, a)) => Ok((IcmpPacket::owned(p.packet().to_vec()).unwrap(), a)),
        Err(e) => Err(e)
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant