Skip to content

Commit

Permalink
Re-add serde support to NetworkInterface
Browse files Browse the repository at this point in the history
In #255, the ipnetwork crate was added.
IpNetwork did not yet support Serde, so (de)serialization support using such had to be removed.

As of
achanda/ipnetwork@c9e25e1,
IpNetwork includes feature-gated serde support

This snippet did not function prior to my changes, but seems to function
fine after re-implementing serde support for IpNetwork

```rust
pub fn get_network_interfaces() -> anyhow::Result<Value> {
    let raw_interfaces: Vec<NetworkInterface> = interfaces();
    Ok(json!(raw_interfaces))
}
```

Since this is now supported upstream, it seems like a low-risk change.
  • Loading branch information
evanjs authored and mrmonday committed Nov 27, 2020
1 parent dcca97d commit 429c652
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ netmap = ["pnet_datalink/netmap_sys", "pnet_datalink/netmap"]
pcap = ["pnet_datalink/pcap"]
appveyor = []
travis = []
serde = ["pnet_base/serde"]
serde = ["pnet_base/serde", "pnet_datalink/serde"]

[dependencies]
ipnetwork = "0.16.0"
Expand Down
6 changes: 6 additions & 0 deletions pnet_datalink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ pnet_sys = { path = "../pnet_sys", version = "0.26.0" }
pcap = { version = "0.7", optional = true }
netmap_sys = { version = ">=0.0", optional = true, features = ["netmap_with_libs"] }


serde = { version = "~1", optional = true, default-features = false }
[target.'cfg(windows)'.dependencies]
winapi = "0.3.8"

[package.metadata.docs.rs]
# Enable the serde feature when generating docs on docs.rs, so the traits are visible
features = ["serde"]
6 changes: 6 additions & 0 deletions pnet_datalink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ extern crate libc;
extern crate pnet_base;
extern crate pnet_sys;

#[cfg(feature = "serde")]
extern crate serde;
#[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

use std::io;
use std::option::Option;
use std::time::Duration;
Expand Down Expand Up @@ -210,6 +215,7 @@ pub trait DataLinkReceiver: Send {
}

/// Represents a network interface and its associated addresses.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
pub struct NetworkInterface {
/// The name of the interface.
Expand Down

0 comments on commit 429c652

Please sign in to comment.