Skip to content

Commit

Permalink
feat: add serde feature
Browse files Browse the repository at this point in the history
  • Loading branch information
BobAnkh committed Feb 10, 2023
1 parent ab62c46 commit 66ebef5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
.vscode
13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[package]
name = "bandwidth"
version = "0.2.1"
version = "0.3.0"
edition = "2021"
license = "Apache-2.0"
description="A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct."
description = "A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct."
readme = "README.md"
homepage ="https://github.com/stack-rs/bandwidth"
homepage = "https://github.com/stack-rs/bandwidth"
repository = "https://github.com/stack-rs/bandwidth"
keywords = ["bandwidth", "data-structures", "network", "no-std", "utility"]
documentation = "https://docs.rs/bandwidth"
Expand All @@ -15,3 +15,10 @@ categories = ["data-structures", "network-programming", "no-std"]

[dependencies]
rustversion = "1.0"
serde = { version = "1.0.152", default-features = false, optional = true, features = [
"derive",
] }

[features]
default = ["std"]
std = ["serde?/std"]
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
[![docs.rs](https://img.shields.io/badge/docs.rs-bandwidth-blue?logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/bandwidth)
[![LICENSE Apache-2.0](https://img.shields.io/github/license/stack-rs/bandwidth?logo=Apache)](https://github.com/stack-rs/bandwidth/blob/main/LICENSE)

A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct.]
A library for representing bandwidth speed in a variety of units, mimicking the `core::time::Duration` struct.

**MSRV**: 1.60

This library supports `no_std` and `serde`. `std` are enabled by default.

## Usage

Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
use core::fmt::{self, Write};
use core::iter::Sum;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

const BPS_PER_GBPS: u32 = 1_000_000_000;
const BPS_PER_MBPS: u32 = 1_000_000;
const BPS_PER_KBPS: u32 = 1_000;
const MBPS_PER_GBPS: u64 = 1_000;
const KBPS_PER_GBPS: u64 = 1_000_000;

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
#[derive(Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(transparent))]
struct BitPerSec(u32);

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

0 comments on commit 66ebef5

Please sign in to comment.