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
sourceimpl<'a> Scalar<'a>
impl<'a> Scalar<'a>
sourcepub fn as_bytes(self) -> &'a [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_bytes(self) -> &'a [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
View the raw data
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
impl<'a> Copy for Scalar<'a>
impl<'a> StructuralPartialEq for Scalar<'a>
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more