[][src]Struct jomini::Scalar

pub struct Scalar<'a> { /* fields omitted */ }

A byte slice that represents a single value.

A scalars does not carry with it an encoding, so an appropriate encoder must be used if text is wished to be extracted from a scalar

use jomini::Scalar;

let v1 = Scalar::new(b"10");
assert_eq!(v1.to_u64(), Ok(10));
assert_eq!(v1.to_i64(), Ok(10));
assert_eq!(v1.to_f64(), Ok(10.0));
assert!(v1.to_bool().is_err());

Implementations

impl<'a> Scalar<'a>[src]

pub fn new(data: &'a [u8]) -> Scalar<'a>[src]

Create a new scalar backed by a byte slice

pub fn view_data(&self) -> &'a [u8][src]

View the raw data

pub fn to_f64(&self) -> Result<f64, ScalarError>[src]

Try converting the scalar to f64

use jomini::Scalar;

let v1 = Scalar::new(b"1.000");
assert_eq!(v1.to_f64(), Ok(1.0));

let v2 = Scalar::new(b"-5.67821");
assert_eq!(v2.to_f64(), Ok(-5.67821));

pub fn to_bool(&self) -> Result<bool, ScalarError>[src]

Try converting the scalar to boolean, only "yes" and "no" can be mapped:

use jomini::Scalar;

let v1 = Scalar::new(b"yes");
assert_eq!(v1.to_bool(), Ok(true));

let v2 = Scalar::new(b"no");
assert_eq!(v2.to_bool(), Ok(false));

pub fn to_i64(&self) -> Result<i64, ScalarError>[src]

Try converting the scalar to i64

use jomini::Scalar;

let v1 = Scalar::new(b"-50");
assert_eq!(v1.to_i64(), Ok(-50));

let v2 = Scalar::new(b"120");
assert_eq!(v2.to_i64(), Ok(120));

pub fn to_u64(&self) -> Result<u64, ScalarError>[src]

Try converting the scalar to u64

use jomini::Scalar;

let v1 = Scalar::new(b"50");
assert_eq!(v1.to_i64(), Ok(50));

let v2 = Scalar::new(b"120");
assert_eq!(v2.to_i64(), Ok(120));

pub fn is_ascii(&self) -> bool[src]

Returns if the scalar contains only ascii values

use jomini::Scalar;

let v1 = Scalar::new(b"a");
assert!(v1.is_ascii());

let v2 = Scalar::new(&[255][..]);
assert!(!v2.is_ascii());

Trait Implementations

impl<'a> Clone for Scalar<'a>[src]

impl<'a> Copy for Scalar<'a>[src]

impl<'a> Debug for Scalar<'a>[src]

impl<'a> Display for Scalar<'a>[src]

impl<'a> PartialEq<Scalar<'a>> for Scalar<'a>[src]

impl<'a> StructuralPartialEq for Scalar<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Scalar<'a>

impl<'a> Send for Scalar<'a>

impl<'a> Sync for Scalar<'a>

impl<'a> Unpin for Scalar<'a>

impl<'a> UnwindSafe for Scalar<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.