ParsedQuantity

Struct ParsedQuantity 

Source
pub struct ParsedQuantity { /* private fields */ }
Expand description

ParsedQuantity represents a parsed Kubernetes quantity.

use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Kubernetes quantity
let k8s_quantity = Quantity("1Ki".to_string());

// Try parsing k8s quantity
let quantity: Result<ParsedQuantity, ParseQuantityError> = k8s_quantity.try_into();

assert_eq!(quantity.unwrap().to_string(), "1Ki");

Implementations§

Source§

impl ParsedQuantity

Source

pub fn to_string_with_precision(&self, precision: u32) -> String

Returns the value of the quantity as a string with the specified number of decimal points for fractional portion. Additionally it performs normalization, i.e., strips any trailing zero’s from a value and converts -0 to 0.

When a number is halfway between two others, it is rounded toward the nearest number that is away from zero. e.g. 6.4 -> 6, 6.5 -> 7, -6.5 -> -7

use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::ParsedQuantity;

let k_quantity: ParsedQuantity = Quantity("1k".to_string()).try_into().unwrap();
let ki_quantity: ParsedQuantity = Quantity("1Ki".to_string()).try_into().unwrap();

let q3 = k_quantity + ki_quantity;

assert_eq!(q3.to_string_with_precision(3), "2.024k");
assert_eq!(q3.to_string_with_precision(2), "2.02k");
assert_eq!(q3.to_string_with_precision(1), "2k");
assert_eq!(q3.to_string_with_precision(0), "2k");
Source

pub fn to_bytes_f64(&self) -> Option<f64>

Returns the value of the quantity as an f64.

use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use kube_quantity::{ParseQuantityError, ParsedQuantity};

// Kubernetes quantity
let k8s_quantity = Quantity("1Ki".to_string());

// Try parsing k8s quantity
let quantity: Result<ParsedQuantity, ParseQuantityError> = k8s_quantity.try_into();

assert_eq!(quantity.unwrap().to_bytes_f64(), Some(1024.0));
Source

pub fn to_bytes_f32(&self) -> Option<f32>

Returns the value of the quantity as an f32.

Source

pub fn to_bytes_i128(&self) -> Option<i128>

Returns the value of the quantity as an i128.

Source

pub fn to_bytes_i64(&self) -> Option<i64>

Returns the value of the quantity as an i64.

Source

pub fn to_bytes_i32(&self) -> Option<i32>

Returns the value of the quantity as an i32.

Source

pub fn to_bytes_i16(&self) -> Option<i16>

Returns the value of the quantity as an i16.

Source

pub fn to_bytes_i8(&self) -> Option<i8>

Returns the value of the quantity as an i8. This will only work if the scale is 0.

Source

pub fn to_bytes_isize(&self) -> Option<isize>

Returns the value of the quantity as an isize.

Source

pub fn to_bytes_u128(&self) -> Option<u128>

Returns the value of the quantity as an u128.

Source

pub fn to_bytes_u64(&self) -> Option<u64>

Returns the value of the quantity as an u64.

Source

pub fn to_bytes_u32(&self) -> Option<u32>

Returns the value of the quantity as an u32.

Source

pub fn to_bytes_u16(&self) -> Option<u16>

Returns the value of the quantity as an u16.

Source

pub fn to_bytes_u8(&self) -> Option<u8>

Returns the value of the quantity as an u8. This will only work if the scale is 0.

Source

pub fn to_bytes_usize(&self) -> Option<usize>

Returns the value of the quantity as an usize.

Trait Implementations§

Source§

impl Add for ParsedQuantity

Source§

type Output = ParsedQuantity

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for ParsedQuantity

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for ParsedQuantity

Source§

fn clone(&self) -> ParsedQuantity

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ParsedQuantity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ParsedQuantity

Source§

fn default() -> ParsedQuantity

Returns the “default value” for a type. Read more
Source§

impl Display for ParsedQuantity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Div<T> for ParsedQuantity
where T: Into<Decimal>,

Source§

type Output = ParsedQuantity

The resulting type after applying the / operator.
Source§

fn div(self, rhs: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> DivAssign<T> for ParsedQuantity
where T: Into<Decimal>,

Source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
Source§

impl From<Decimal> for ParsedQuantity

Source§

fn from(value: Decimal) -> Self

Converts to this type from the input type.
Source§

impl From<ParsedQuantity> for Quantity

Source§

fn from(value: ParsedQuantity) -> Self

Converts to this type from the input type.
Source§

impl<T> Mul<T> for ParsedQuantity
where T: Into<Decimal>,

Source§

type Output = ParsedQuantity

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> MulAssign<T> for ParsedQuantity
where T: Into<Decimal>,

Source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
Source§

impl Neg for ParsedQuantity

Source§

type Output = ParsedQuantity

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for ParsedQuantity

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ParsedQuantity

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ParsedQuantity

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for ParsedQuantity

Source§

type Output = ParsedQuantity

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign for ParsedQuantity

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl TryFrom<&Quantity> for ParsedQuantity

Source§

type Error = ParseQuantityError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &Quantity) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&str> for ParsedQuantity

Source§

type Error = ParseQuantityError

The type returned in the event of a conversion error.
Source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Quantity> for ParsedQuantity

Source§

type Error = ParseQuantityError

The type returned in the event of a conversion error.
Source§

fn try_from(value: Quantity) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<String> for ParsedQuantity

Source§

type Error = ParseQuantityError

The type returned in the event of a conversion error.
Source§

fn try_from(value: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for ParsedQuantity

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.