pub struct OwnedRawSexp { /* private fields */ }
Expand description
A newly-created SEXP of a raw vector.
Implementations§
Source§impl OwnedRawSexp
impl OwnedRawSexp
Source§impl OwnedRawSexp
impl OwnedRawSexp
Sourcepub fn as_read_only(&self) -> RawSexp
pub fn as_read_only(&self) -> RawSexp
Returns the read-only version of the wrapper. This is mainly for testing purposes.
Sourcepub fn as_slice(&self) -> &[u8] ⓘ
pub fn as_slice(&self) -> &[u8] ⓘ
Extracts a slice containing the underlying data of the SEXP.
§Examples
use savvy::OwnedRawSexp;
let raw_sexp = OwnedRawSexp::try_from_slice([1_u8, 2, 3])?;
assert_eq!(raw_sexp.as_slice(), &[1, 2, 3]);
Sourcepub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
Extracts a mutable slice containing the underlying data of the SEXP.
§Examples
use savvy::OwnedRawSexp;
let mut raw_sexp = OwnedRawSexp::new(3)?;
let s = raw_sexp.as_mut_slice();
s[2] = 10;
assert_eq!(raw_sexp.as_slice(), &[0, 0, 10]);
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, u8>
pub fn iter_mut(&mut self) -> IterMut<'_, u8>
Returns a mutable iterator over the underlying data of the SEXP.
§Examples
use savvy::OwnedRawSexp;
let mut raw_sexp = OwnedRawSexp::try_from_slice([1_u8, 2, 3])?;
raw_sexp.iter_mut().for_each(|x| *x = *x * 2);
assert_eq!(raw_sexp.as_slice(), &[2, 4, 6]);
Sourcepub fn set_elt(&mut self, i: usize, v: u8) -> Result<()>
pub fn set_elt(&mut self, i: usize, v: u8) -> Result<()>
Set the value of the i
-th element. i
starts from 0
.
§Examples
use savvy::OwnedRawSexp;
let mut raw_sexp = OwnedRawSexp::new(3)?;
raw_sexp.set_elt(2, 10)?;
assert_eq!(raw_sexp.as_slice(), &[0, 0, 10]);
Sourcepub fn new(len: usize) -> Result<Self>
pub fn new(len: usize) -> Result<Self>
Constructs a new, initialized raw vector.
let x = savvy::OwnedRawSexp::new(3)?;
assert_eq!(x.as_slice(), &[0, 0, 0]);
Sourcepub unsafe fn new_without_init(len: usize) -> Result<Self>
pub unsafe fn new_without_init(len: usize) -> Result<Self>
Constructs a new, uninitialized raw vector.
This is an expert-only version of new()
, which can be found useful
when you want to skip initialization and you are confident that the
vector will be filled with values later.
For example, you can use this in TryFrom
implementation.
use savvy::OwnedRawSexp;
struct Pair {
x: u8,
y: u8
}
impl TryFrom<Pair> for OwnedRawSexp {
type Error = savvy::Error;
fn try_from(value: Pair) -> savvy::Result<Self> {
let mut out = unsafe { OwnedRawSexp::new_without_init(2)? };
out[0] = value.x;
out[1] = value.y;
Ok(out)
}
}
let pair = Pair { x: 1, y: 2 };
let raw_sexp = <OwnedRawSexp>::try_from(pair)?;
assert_eq!(raw_sexp.as_slice(), &[1, 2]);
§Safety
As the memory is uninitialized, all elements must be filled values before return.
Sourcepub fn try_from_iter<I>(iter: I) -> Result<Self>where
I: IntoIterator<Item = u8>,
pub fn try_from_iter<I>(iter: I) -> Result<Self>where
I: IntoIterator<Item = u8>,
Constructs a new complex vector from an iterator.
Note that, if you already have a slice or vec, try_from_slice()
is what you want. try_from_slice
is more performant than
try_from_iter
because it copies the underlying memory directly.
§Examples
use savvy::OwnedRawSexp;
let iter = (0..10).filter(|x| x % 2 == 0);
let raw_sexp = OwnedRawSexp::try_from_iter(iter)?;
assert_eq!(raw_sexp.as_slice(), &[0, 2, 4, 6, 8]);
Sourcepub fn try_from_slice<S>(x: S) -> Result<Self>
pub fn try_from_slice<S>(x: S) -> Result<Self>
Constructs a new raw vector from a slice or vec.
§Examples
use savvy::OwnedRawSexp;
let raw_sexp = OwnedRawSexp::try_from_slice([1_u8, 2, 3])?;
assert_eq!(raw_sexp.as_slice(), &[1, 2, 3]);
Sourcepub fn try_from_scalar(value: u8) -> Result<Self>
pub fn try_from_scalar(value: u8) -> Result<Self>
Constructs a new raw vector from a scalar value.
§Examples
use savvy::OwnedRawSexp;
let raw_sexp = OwnedRawSexp::try_from_scalar(1)?;
assert_eq!(raw_sexp.as_slice(), &[1]);