pub struct Scalar<'a> { /* private fields */ }
Expand description
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§
source§impl<'a> Scalar<'a>
impl<'a> Scalar<'a>
sourcepub fn to_f64(self) -> Result<f64, ScalarError>
pub fn to_f64(self) -> Result<f64, ScalarError>
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));
sourcepub fn to_bool(self) -> Result<bool, ScalarError>
pub fn to_bool(self) -> Result<bool, ScalarError>
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));
sourcepub fn to_i64(self) -> Result<i64, ScalarError>
pub fn to_i64(self) -> Result<i64, ScalarError>
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));
sourcepub fn to_u64(self) -> Result<u64, ScalarError>
pub fn to_u64(self) -> Result<u64, ScalarError>
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));
Trait Implementations§
source§impl<'a> PartialEq for Scalar<'a>
impl<'a> PartialEq for Scalar<'a>
impl<'a> Copy for Scalar<'a>
impl<'a> Eq for Scalar<'a>
impl<'a> StructuralPartialEq for Scalar<'a>
Auto Trait Implementations§
impl<'a> Freeze for Scalar<'a>
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§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more