savvy::sexp::raw

Struct OwnedRawSexp

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

A newly-created SEXP of a raw vector.

Implementations§

Source§

impl OwnedRawSexp

Source

pub fn inner(&self) -> SEXP

Returns the raw SEXP.

Source

pub fn len(&self) -> usize

Returns the length of the SEXP.

Source

pub fn is_empty(&self) -> bool

Returns true if the SEXP is of zero-length.

Source

pub fn get_attrib(&self, attr: &str) -> Result<Option<Sexp>>

Returns the specified attribute.

Source

pub fn get_names(&self) -> Option<Vec<&'static str>>

Returns the names.

Source

pub fn get_class(&self) -> Option<Vec<&'static str>>

Returns the S3 class.

Source

pub fn get_dim(&self) -> Option<&[i32]>

Returns the dimension.

Source

pub fn set_attrib(&mut self, attr: &str, value: Sexp) -> Result<()>

Set the input value to the specified attribute.

Source

pub fn set_class<T, U>(&mut self, classes: T) -> Result<()>
where T: AsRef<[U]>, U: AsRef<str>,

Set the S3 class.

Source

pub fn set_names<T, U>(&mut self, names: T) -> Result<()>
where T: AsRef<[U]>, U: AsRef<str>,

Set the names.

Source

pub fn set_dim<T: TryInto<i32> + Copy>(&mut self, dim: &[T]) -> Result<()>

Set the dimension. dim can be i32, usize, or whatever numeric types that implements TryInto<i32>.

Source§

impl OwnedRawSexp

Source

pub fn as_read_only(&self) -> RawSexp

Returns the read-only version of the wrapper. This is mainly for testing purposes.

Source

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]);
Source

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]);
Source

pub fn iter(&self) -> Iter<'_, u8>

Returns an iterator over the underlying data of the SEXP.

Source

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]);
Source

pub fn to_vec(&self) -> Vec<u8>

Copies the underlying data of the SEXP into a new Vec.

Source

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]);
Source

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]);
Source

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.

Source

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]);
Source

pub fn try_from_slice<S>(x: S) -> Result<Self>
where S: AsRef<[u8]>,

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]);
Source

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]);

Trait Implementations§

Source§

impl Drop for OwnedRawSexp

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<OwnedRawSexp> for Result<Sexp>

Source§

fn from(value: OwnedRawSexp) -> Self

Converts to this type from the input type.
Source§

impl From<OwnedRawSexp> for Sexp

Source§

fn from(value: OwnedRawSexp) -> Self

Converts to this type from the input type.
Source§

impl From<OwnedRawSexp> for TypedSexp

Source§

fn from(value: OwnedRawSexp) -> Self

Converts to this type from the input type.
Source§

impl Index<usize> for OwnedRawSexp

Source§

type Output = u8

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for OwnedRawSexp

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl TryFrom<&[u8]> for OwnedRawSexp

Source§

type Error = Error

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

fn try_from(value: &[u8]) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<Vec<u8>> for OwnedRawSexp

Source§

type Error = Error

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

fn try_from(value: Vec<u8>) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<u8> for OwnedRawSexp

Source§

type Error = Error

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

fn try_from(value: u8) -> Result<Self>

Performs the conversion.

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> 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, 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.