Skip to content

Commit 66ebef5

Browse files
committed
feat: add serde feature
1 parent ab62c46 commit 66ebef5

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/target
22
/Cargo.lock
3+
.vscode

Cargo.toml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "bandwidth"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
edition = "2021"
55
license = "Apache-2.0"
6-
description="A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct."
6+
description = "A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct."
77
readme = "README.md"
8-
homepage ="https://github.com/stack-rs/bandwidth"
8+
homepage = "https://github.com/stack-rs/bandwidth"
99
repository = "https://github.com/stack-rs/bandwidth"
1010
keywords = ["bandwidth", "data-structures", "network", "no-std", "utility"]
1111
documentation = "https://docs.rs/bandwidth"
@@ -15,3 +15,10 @@ categories = ["data-structures", "network-programming", "no-std"]
1515

1616
[dependencies]
1717
rustversion = "1.0"
18+
serde = { version = "1.0.152", default-features = false, optional = true, features = [
19+
"derive",
20+
] }
21+
22+
[features]
23+
default = ["std"]
24+
std = ["serde?/std"]

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
[![docs.rs](https://img.shields.io/badge/docs.rs-bandwidth-blue?logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/bandwidth)
66
[![LICENSE Apache-2.0](https://img.shields.io/github/license/stack-rs/bandwidth?logo=Apache)](https://github.com/stack-rs/bandwidth/blob/main/LICENSE)
77

8-
A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct.]
8+
A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct.
9+
10+
**MSRV**: 1.60
11+
12+
This library supports `no_std` and `serde`. `std` are enabled by default.
913

1014
## Usage
1115

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
use core::fmt::{self, Write};
2323
use core::iter::Sum;
2424
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
25+
#[cfg(feature = "serde")]
26+
use serde::{Deserialize, Serialize};
2527

2628
const BPS_PER_GBPS: u32 = 1_000_000_000;
2729
const BPS_PER_MBPS: u32 = 1_000_000;
2830
const BPS_PER_KBPS: u32 = 1_000;
2931
const MBPS_PER_GBPS: u64 = 1_000;
3032
const KBPS_PER_GBPS: u64 = 1_000_000;
3133

32-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
34+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
3335
#[repr(transparent)]
34-
#[derive(Default)]
36+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
3537
struct BitPerSec(u32);
3638

3739
/// A `Bandwidth` type to represent a link's bandwidth(to describe how many bits can be sent
@@ -62,6 +64,7 @@ struct BitPerSec(u32);
6264
/// let ten_mbps = Bandwidth::from_mbps(10);
6365
/// ```
6466
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
67+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6568
pub struct Bandwidth {
6669
gbps: u64,
6770
bps: BitPerSec, // Always 0 <= bps < BPS_PER_GBPS

0 commit comments

Comments
 (0)