pub struct Bandwidth { /* private fields */ }
Expand description
A Bandwidth
type to represent a link’s bandwidth(to describe how many bits can be sent
on the link per second), typically used for network.
Each Bandwidth
is composed of a whole number of gbps(gigabits per second) and a fractional part
represented in bps(bits per second). The fractional part should always in the range [0, 1_000_000_000)
.
Bandwidth
s implement many common traits, including Add
, Sub
, and other
[ops
] traits. It implements Default
by returning a zero-speed Bandwidth
.
- gbps = gigabits per second
- mbps = megabits per second
- kbps = kilobits per second
- bps = bits per second
§Examples
use bandwidth::Bandwidth;
let five_gbps = Bandwidth::new(5, 0);
let five_gbps_and_five_bps = five_gbps + Bandwidth::new(0, 5);
assert_eq!(five_gbps_and_five_bps.as_gbps(), 5);
assert_eq!(five_gbps_and_five_bps.subgbps_bps(), 5);
let ten_mbps = Bandwidth::from_mbps(10);
Implementations§
Source§impl Bandwidth
impl Bandwidth
Sourcepub const MAX: Bandwidth
pub const MAX: Bandwidth
The maximum bandwidth speed.
§Examples
use bandwidth::Bandwidth;
assert_eq!(Bandwidth::MAX, Bandwidth::new(u64::MAX, 1_000_000_000 - 1));
Sourcepub const ZERO: Bandwidth
pub const ZERO: Bandwidth
A zero bandwidth speed.
§Examples
use bandwidth::Bandwidth;
let bw = Bandwidth::ZERO;
assert!(bw.is_zero());
assert_eq!(bw.as_bps(), 0);
pub const fn new(gbps: u64, bps: u32) -> Bandwidth
Sourcepub const fn from_gbps(gbps: u64) -> Bandwidth
pub const fn from_gbps(gbps: u64) -> Bandwidth
Creates a new Bandwidth
from the specified number of gigabits per second
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::from_gbps(5);
assert_eq!(5, Bandwidth.as_gbps());
assert_eq!(0, Bandwidth.subgbps_bps());
Sourcepub const fn from_mbps(mbps: u64) -> Bandwidth
pub const fn from_mbps(mbps: u64) -> Bandwidth
Creates a new Bandwidth
from the specified number of megabits per second
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::from_mbps(2569);
assert_eq!(2, Bandwidth.as_gbps());
assert_eq!(569_000_000, Bandwidth.subgbps_bps());
Sourcepub const fn from_kbps(kbps: u64) -> Bandwidth
pub const fn from_kbps(kbps: u64) -> Bandwidth
Creates a new Bandwidth
from the specified number of kilobits per second
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::from_kbps(1_000_002);
assert_eq!(1, Bandwidth.as_gbps());
assert_eq!(2000, Bandwidth.subgbps_bps());
Sourcepub const fn from_bps(bps: u64) -> Bandwidth
pub const fn from_bps(bps: u64) -> Bandwidth
Creates a new Bandwidth
from the specified number of bits per second
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::from_bps(1_000_000_123);
assert_eq!(1, Bandwidth.as_gbps());
assert_eq!(123, Bandwidth.subgbps_bps());
Sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true if this Bandwidth
has zero speed.
§Examples
use bandwidth::Bandwidth;
assert!(Bandwidth::ZERO.is_zero());
assert!(Bandwidth::new(0, 0).is_zero());
assert!(Bandwidth::from_bps(0).is_zero());
assert!(Bandwidth::from_gbps(0).is_zero());
assert!(!Bandwidth::new(1, 1).is_zero());
assert!(!Bandwidth::from_bps(1).is_zero());
assert!(!Bandwidth::from_gbps(1).is_zero());
Sourcepub const fn as_gbps(&self) -> u64
pub const fn as_gbps(&self) -> u64
Returns the number of whole gbps contained by this Bandwidth
.
The returned value does not include the fractional(bps) part of the
Bandwidth, which can be obtained using subgbps_bps
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::new(5, 730023852);
assert_eq!(Bandwidth.as_gbps(), 5);
To determine the total number of gbps represented by the Bandwidth
including the fractional part, use as_gbps_f64
or as_gbps_f32
Sourcepub const fn subgbps_mbps(&self) -> u32
pub const fn subgbps_mbps(&self) -> u32
Returns the fractional part of this Bandwidth
, in whole mbps.
This method does not return the speed of the Bandwidth when represented by mbps. The returned number always represents a fractional portion of a gbps (i.e., it is less than one thousand).
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::from_mbps(5432);
assert_eq!(Bandwidth.as_gbps(), 5);
assert_eq!(Bandwidth.subgbps_mbps(), 432);
Sourcepub const fn subgbps_kbps(&self) -> u32
pub const fn subgbps_kbps(&self) -> u32
Returns the fractional part of this Bandwidth
, in whole kbps.
This method does not return the speed of the Bandwidth when represented by kbps. The returned number always represents a fractional portion of a gbps (i.e., it is less than one million).
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::from_kbps(1_234_567);
assert_eq!(Bandwidth.as_gbps(), 1);
assert_eq!(Bandwidth.subgbps_kbps(), 234_567);
Sourcepub const fn subgbps_bps(&self) -> u32
pub const fn subgbps_bps(&self) -> u32
Returns the fractional part of this Bandwidth
, in bps.
This method does not return the speed of the Bandwidth when represented by bps. The returned number always represents a fractional portion of a gbps (i.e., it is less than one billion).
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::from_mbps(5010);
assert_eq!(Bandwidth.as_gbps(), 5);
assert_eq!(Bandwidth.subgbps_bps(), 10_000_000);
Sourcepub const fn as_mbps(&self) -> u128
pub const fn as_mbps(&self) -> u128
Returns the total number of whole mbps contained by this Bandwidth
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::new(5, 730023852);
assert_eq!(Bandwidth.as_mbps(), 5730);
Sourcepub const fn as_kbps(&self) -> u128
pub const fn as_kbps(&self) -> u128
Returns the total number of whole kbps contained by this Bandwidth
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::new(5, 730023852);
assert_eq!(Bandwidth.as_kbps(), 5730023);
Sourcepub const fn as_bps(&self) -> u128
pub const fn as_bps(&self) -> u128
Returns the total number of bps contained by this Bandwidth
.
§Examples
use bandwidth::Bandwidth;
let Bandwidth = Bandwidth::new(5, 730023852);
assert_eq!(Bandwidth.as_bps(), 5730023852);
Sourcepub const fn checked_add(self, rhs: Bandwidth) -> Option<Bandwidth>
pub const fn checked_add(self, rhs: Bandwidth) -> Option<Bandwidth>
Checked Bandwidth
addition. Computes self + other
, returning None
if overflow occurred.
§Examples
Basic usage:
use bandwidth::Bandwidth;
assert_eq!(Bandwidth::new(0, 0).checked_add(Bandwidth::new(0, 1)), Some(Bandwidth::new(0, 1)));
assert_eq!(Bandwidth::new(1, 0).checked_add(Bandwidth::new(u64::MAX, 0)), None);
Sourcepub const fn saturating_add(self, rhs: Bandwidth) -> Bandwidth
pub const fn saturating_add(self, rhs: Bandwidth) -> Bandwidth
Saturating Bandwidth
addition. Computes self + other
, returning Bandwidth::MAX
if overflow occurred.
§Examples
use bandwidth::Bandwidth;
assert_eq!(Bandwidth::new(0, 0).saturating_add(Bandwidth::new(0, 1)), Bandwidth::new(0, 1));
assert_eq!(Bandwidth::new(1, 0).saturating_add(Bandwidth::new(u64::MAX, 0)), Bandwidth::MAX);
Sourcepub const fn checked_sub(self, rhs: Bandwidth) -> Option<Bandwidth>
pub const fn checked_sub(self, rhs: Bandwidth) -> Option<Bandwidth>
Checked Bandwidth
subtraction. Computes self - other
, returning None
if the result would be negative or if overflow occurred.
§Examples
Basic usage:
use bandwidth::Bandwidth;
assert_eq!(Bandwidth::new(0, 1).checked_sub(Bandwidth::new(0, 0)), Some(Bandwidth::new(0, 1)));
assert_eq!(Bandwidth::new(0, 0).checked_sub(Bandwidth::new(0, 1)), None);
Sourcepub const fn saturating_sub(self, rhs: Bandwidth) -> Bandwidth
pub const fn saturating_sub(self, rhs: Bandwidth) -> Bandwidth
Saturating Bandwidth
subtraction. Computes self - other
, returning Bandwidth::ZERO
if the result would be negative or if overflow occurred.
§Examples
use bandwidth::Bandwidth;
assert_eq!(Bandwidth::new(0, 1).saturating_sub(Bandwidth::new(0, 0)), Bandwidth::new(0, 1));
assert_eq!(Bandwidth::new(0, 0).saturating_sub(Bandwidth::new(0, 1)), Bandwidth::ZERO);
Sourcepub const fn checked_mul(self, rhs: u32) -> Option<Bandwidth>
pub const fn checked_mul(self, rhs: u32) -> Option<Bandwidth>
Sourcepub const fn saturating_mul(self, rhs: u32) -> Bandwidth
pub const fn saturating_mul(self, rhs: u32) -> Bandwidth
Saturating Bandwidth
multiplication. Computes self * other
, returning
Bandwidth::MAX
if overflow occurred.
§Examples
use bandwidth::Bandwidth;
assert_eq!(Bandwidth::new(0, 500_000_001).saturating_mul(2), Bandwidth::new(1, 2));
assert_eq!(Bandwidth::new(u64::MAX - 1, 0).saturating_mul(2), Bandwidth::MAX);
Sourcepub const fn checked_div(self, rhs: u32) -> Option<Bandwidth>
pub const fn checked_div(self, rhs: u32) -> Option<Bandwidth>
Checked Bandwidth
division. Computes self / other
, returning None
if other == 0
.
§Examples
Basic usage:
use bandwidth::Bandwidth;
assert_eq!(Bandwidth::new(2, 0).checked_div(2), Some(Bandwidth::new(1, 0)));
assert_eq!(Bandwidth::new(1, 0).checked_div(2), Some(Bandwidth::new(0, 500_000_000)));
assert_eq!(Bandwidth::new(2, 0).checked_div(0), None);
Sourcepub fn as_gbps_f64(&self) -> f64
pub fn as_gbps_f64(&self) -> f64
Returns the number of gbps contained by this Bandwidth
as f64
.
The returned value does include the fractional (bps) part of the Bandwidth.
§Examples
use bandwidth::Bandwidth;
let bw = Bandwidth::new(2, 700_000_000);
assert_eq!(bw.as_gbps_f64(), 2.7);
Sourcepub fn as_gbps_f32(&self) -> f32
pub fn as_gbps_f32(&self) -> f32
Returns the number of gbps contained by this Bandwidth
as f32
.
The returned value does include the fractional (bps) part of the Bandwidth.
§Examples
use bandwidth::Bandwidth;
let bw = Bandwidth::new(2, 700_000_000);
assert_eq!(bw.as_gbps_f32(), 2.7);
Sourcepub fn from_gbps_f64(gbps: f64) -> Bandwidth
pub fn from_gbps_f64(gbps: f64) -> Bandwidth
Creates a new Bandwidth
from the specified number of gbps represented
as f64
.
§Panics
This constructor will panic if gbps
is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let res = Bandwidth::from_gbps_f64(0.0);
assert_eq!(res, Bandwidth::new(0, 0));
let res = Bandwidth::from_gbps_f64(1e-20);
assert_eq!(res, Bandwidth::new(0, 0));
let res = Bandwidth::from_gbps_f64(4.2e-7);
assert_eq!(res, Bandwidth::new(0, 420));
let res = Bandwidth::from_gbps_f64(2.7);
assert_eq!(res, Bandwidth::new(2, 700_000_000));
let res = Bandwidth::from_gbps_f64(3e10);
assert_eq!(res, Bandwidth::new(30_000_000_000, 0));
// subnormal float
let res = Bandwidth::from_gbps_f64(f64::from_bits(1));
assert_eq!(res, Bandwidth::new(0, 0));
// conversion uses rounding
let res = Bandwidth::from_gbps_f64(0.999e-9);
assert_eq!(res, Bandwidth::new(0, 1));
Sourcepub fn from_gbps_f32(gbps: f32) -> Bandwidth
pub fn from_gbps_f32(gbps: f32) -> Bandwidth
Creates a new Bandwidth
from the specified number of gbps represented
as f32
.
§Panics
This constructor will panic if gbps
is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let res = Bandwidth::from_gbps_f32(0.0);
assert_eq!(res, Bandwidth::new(0, 0));
let res = Bandwidth::from_gbps_f32(1e-20);
assert_eq!(res, Bandwidth::new(0, 0));
let res = Bandwidth::from_gbps_f32(4.2e-7);
assert_eq!(res, Bandwidth::new(0, 420));
let res = Bandwidth::from_gbps_f32(2.7);
assert_eq!(res, Bandwidth::new(2, 700_000_048));
let res = Bandwidth::from_gbps_f32(3e10);
assert_eq!(res, Bandwidth::new(30_000_001_024, 0));
// subnormal float
let res = Bandwidth::from_gbps_f32(f32::from_bits(1));
assert_eq!(res, Bandwidth::new(0, 0));
// conversion uses rounding
let res = Bandwidth::from_gbps_f32(0.999e-9);
assert_eq!(res, Bandwidth::new(0, 1));
Sourcepub fn mul_f64(self, rhs: f64) -> Bandwidth
pub fn mul_f64(self, rhs: f64) -> Bandwidth
Multiplies Bandwidth
by f64
.
§Panics
This method will panic if result is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let bw = Bandwidth::new(2, 700_000_000);
assert_eq!(bw.mul_f64(3.14), Bandwidth::new(8, 478_000_000));
assert_eq!(bw.mul_f64(3.14e5), Bandwidth::new(847_800, 0));
Sourcepub fn mul_f32(self, rhs: f32) -> Bandwidth
pub fn mul_f32(self, rhs: f32) -> Bandwidth
Multiplies Bandwidth
by f32
.
§Panics
This method will panic if result is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let bw = Bandwidth::new(2, 700_000_000);
assert_eq!(bw.mul_f32(3.14), Bandwidth::new(8, 478_000_641));
assert_eq!(bw.mul_f32(3.14e5), Bandwidth::new(847800, 0));
Sourcepub fn div_f64(self, rhs: f64) -> Bandwidth
pub fn div_f64(self, rhs: f64) -> Bandwidth
Divide Bandwidth
by f64
.
§Panics
This method will panic if result is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let bw = Bandwidth::new(2, 700_000_000);
assert_eq!(bw.div_f64(3.14), Bandwidth::new(0, 859_872_611));
assert_eq!(bw.div_f64(3.14e5), Bandwidth::new(0, 8_599));
Sourcepub fn div_f32(self, rhs: f32) -> Bandwidth
pub fn div_f32(self, rhs: f32) -> Bandwidth
Divide Bandwidth
by f32
.
§Panics
This method will panic if result is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let bw = Bandwidth::new(2, 700_000_000);
// note that due to rounding errors result is slightly
// different from 0.859_872_611
assert_eq!(bw.div_f32(3.14), Bandwidth::new(0, 859_872_580));
assert_eq!(bw.div_f32(3.14e5), Bandwidth::new(0, 8_599));
Sourcepub fn div_bandwidth_f64(self, rhs: Bandwidth) -> f64
pub fn div_bandwidth_f64(self, rhs: Bandwidth) -> f64
Divide Bandwidth
by Bandwidth
and return f64
.
§Examples
use bandwidth::Bandwidth;
let bw1 = Bandwidth::new(2, 700_000_000);
let bw2 = Bandwidth::new(5, 400_000_000);
assert_eq!(bw1.div_bandwidth_f64(bw2), 0.5);
Sourcepub fn div_bandwidth_f32(self, rhs: Bandwidth) -> f32
pub fn div_bandwidth_f32(self, rhs: Bandwidth) -> f32
Divide Bandwidth
by Bandwidth
and return f32
.
§Examples
use bandwidth::Bandwidth;
let bw1 = Bandwidth::new(2, 700_000_000);
let bw2 = Bandwidth::new(5, 400_000_000);
assert_eq!(bw1.div_bandwidth_f32(bw2), 0.5);
Source§impl Bandwidth
impl Bandwidth
Sourcepub fn try_from_gbps_f32(gbps: f32) -> Result<Bandwidth, TryFromFloatGbpsError>
pub fn try_from_gbps_f32(gbps: f32) -> Result<Bandwidth, TryFromFloatGbpsError>
The checked version of from_gbps_f32
.
This constructor will return an Err
if gbps
is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let res = Bandwidth::try_from_gbps_f32(0.0);
assert_eq!(res, Ok(Bandwidth::new(0, 0)));
let res = Bandwidth::try_from_gbps_f32(1e-20);
assert_eq!(res, Ok(Bandwidth::new(0, 0)));
let res = Bandwidth::try_from_gbps_f32(4.2e-7);
assert_eq!(res, Ok(Bandwidth::new(0, 420)));
let res = Bandwidth::try_from_gbps_f32(2.7);
assert_eq!(res, Ok(Bandwidth::new(2, 700_000_048)));
let res = Bandwidth::try_from_gbps_f32(3e10);
assert_eq!(res, Ok(Bandwidth::new(30_000_001_024, 0)));
// subnormal float:
let res = Bandwidth::try_from_gbps_f32(f32::from_bits(1));
assert_eq!(res, Ok(Bandwidth::new(0, 0)));
let res = Bandwidth::try_from_gbps_f32(-5.0);
assert!(res.is_err());
let res = Bandwidth::try_from_gbps_f32(f32::NAN);
assert!(res.is_err());
let res = Bandwidth::try_from_gbps_f32(2e19);
assert!(res.is_err());
// the conversion uses rounding with tie resolution to even
let res = Bandwidth::try_from_gbps_f32(0.999e-9);
assert_eq!(res, Ok(Bandwidth::new(0, 1)));
// this float represents exactly 976562.5e-9
let val = f32::from_bits(0x3A80_0000);
let res = Bandwidth::try_from_gbps_f32(val);
assert_eq!(res, Ok(Bandwidth::new(0, 976_562)));
// this float represents exactly 2929687.5e-9
let val = f32::from_bits(0x3B40_0000);
let res = Bandwidth::try_from_gbps_f32(val);
assert_eq!(res, Ok(Bandwidth::new(0, 2_929_688)));
// this float represents exactly 1.000_976_562_5
let val = f32::from_bits(0x3F802000);
let res = Bandwidth::try_from_gbps_f32(val);
assert_eq!(res, Ok(Bandwidth::new(1, 976_562)));
// this float represents exactly 1.002_929_687_5
let val = f32::from_bits(0x3F806000);
let res = Bandwidth::try_from_gbps_f32(val);
assert_eq!(res, Ok(Bandwidth::new(1, 2_929_688)));
Sourcepub fn try_from_gbps_f64(gbps: f64) -> Result<Bandwidth, TryFromFloatGbpsError>
pub fn try_from_gbps_f64(gbps: f64) -> Result<Bandwidth, TryFromFloatGbpsError>
The checked version of from_gbps_f64
.
This constructor will return an Err
if secs
is negative, overflows Bandwidth
or not finite.
§Examples
use bandwidth::Bandwidth;
let res = Bandwidth::try_from_gbps_f64(0.0);
assert_eq!(res, Ok(Bandwidth::new(0, 0)));
let res = Bandwidth::try_from_gbps_f64(1e-20);
assert_eq!(res, Ok(Bandwidth::new(0, 0)));
let res = Bandwidth::try_from_gbps_f64(4.2e-7);
assert_eq!(res, Ok(Bandwidth::new(0, 420)));
let res = Bandwidth::try_from_gbps_f64(2.7);
assert_eq!(res, Ok(Bandwidth::new(2, 700_000_000)));
let res = Bandwidth::try_from_gbps_f64(3e10);
assert_eq!(res, Ok(Bandwidth::new(30_000_000_000, 0)));
// subnormal float
let res = Bandwidth::try_from_gbps_f64(f64::from_bits(1));
assert_eq!(res, Ok(Bandwidth::new(0, 0)));
let res = Bandwidth::try_from_gbps_f64(-5.0);
assert!(res.is_err());
let res = Bandwidth::try_from_gbps_f64(f64::NAN);
assert!(res.is_err());
let res = Bandwidth::try_from_gbps_f64(2e19);
assert!(res.is_err());
// the conversion uses rounding with tie resolution to even
let res = Bandwidth::try_from_gbps_f64(0.999e-9);
assert_eq!(res, Ok(Bandwidth::new(0, 1)));
let res = Bandwidth::try_from_gbps_f64(0.999_999_999_499);
assert_eq!(res, Ok(Bandwidth::new(0, 999_999_999)));
let res = Bandwidth::try_from_gbps_f64(0.999_999_999_501);
assert_eq!(res, Ok(Bandwidth::new(1, 0)));
let res = Bandwidth::try_from_gbps_f64(42.999_999_999_499);
assert_eq!(res, Ok(Bandwidth::new(42, 999_999_999)));
let res = Bandwidth::try_from_gbps_f64(42.999_999_999_501);
assert_eq!(res, Ok(Bandwidth::new(43, 0)));
// this float represents exactly 976562.5e-9
let val = f64::from_bits(0x3F50_0000_0000_0000);
let res = Bandwidth::try_from_gbps_f64(val);
assert_eq!(res, Ok(Bandwidth::new(0, 976_562)));
// this float represents exactly 2929687.5e-9
let val = f64::from_bits(0x3F68_0000_0000_0000);
let res = Bandwidth::try_from_gbps_f64(val);
assert_eq!(res, Ok(Bandwidth::new(0, 2_929_688)));
// this float represents exactly 1.000_976_562_5
let val = f64::from_bits(0x3FF0_0400_0000_0000);
let res = Bandwidth::try_from_gbps_f64(val);
assert_eq!(res, Ok(Bandwidth::new(1, 976_562)));
// this float represents exactly 1.002_929_687_5
let val = f64::from_bits(0x3_FF00_C000_0000_000);
let res = Bandwidth::try_from_gbps_f64(val);
assert_eq!(res, Ok(Bandwidth::new(1, 2_929_688)));
Trait Implementations§
Source§impl AddAssign for Bandwidth
impl AddAssign for Bandwidth
Source§fn add_assign(&mut self, rhs: Bandwidth)
fn add_assign(&mut self, rhs: Bandwidth)
+=
operation. Read moreSource§impl DivAssign<u32> for Bandwidth
impl DivAssign<u32> for Bandwidth
Source§fn div_assign(&mut self, rhs: u32)
fn div_assign(&mut self, rhs: u32)
/=
operation. Read moreSource§impl MulAssign<u32> for Bandwidth
impl MulAssign<u32> for Bandwidth
Source§fn mul_assign(&mut self, rhs: u32)
fn mul_assign(&mut self, rhs: u32)
*=
operation. Read moreSource§impl Ord for Bandwidth
impl Ord for Bandwidth
Source§impl PartialOrd for Bandwidth
impl PartialOrd for Bandwidth
Source§impl SubAssign for Bandwidth
impl SubAssign for Bandwidth
Source§fn sub_assign(&mut self, rhs: Bandwidth)
fn sub_assign(&mut self, rhs: Bandwidth)
-=
operation. Read more