rust-pcap / pcap

Rust language pcap library
Apache License 2.0
610 stars 138 forks source link

Add doc example for using generics to abstract over capture types #197

Closed not-my-profile closed 2 years ago

not-my-profile commented 2 years ago

(the PR has been changed)

Stargateur commented 2 years ago

but why ? It's look totally wrong, why are you using Capture<dyn Activated> ?

not-my-profile commented 2 years ago

I want to abstract over Capture<Active> and Capture<Offline> (the code snippet from the commit message is just a minimal example to illustrate the error). I want to create a capture either by tapping an interface or by reading a file and then later read from the capture in a new tokio task.

Stargateur commented 2 years ago

You should use generic, this is a little complex but you can see here that next() is implemented for every Activated. This mean you can do:

fn foo<IsActivated: Activated>(capture: Capture<IsActivated>) {
  capture.next();
}

This is way simpler than use dynamic dispatch.

not-my-profile commented 2 years ago

Ah yeah of course ... thanks!

I updated this PR to just add an example about this to the docs.

Stargateur commented 2 years ago

LGTM

Wojtek242 commented 2 years ago

@not-my-profile can you please rebase on top of the latest master so that the tests can rerun? Since you're adding a code example I believe your example will be run during tests. I assume the most suitable fix would be to just add ignore like for the previous example, but I'll leave that up to you.

Wojtek242 commented 2 years ago

Thanks @not-my-profile