osiris-data 0.2.1

A data management package.
Documentation
use crate::data::atomic::Word;
use crate::data::composite::Array;

/// A collection of function to transmute [Word]s from/into various usual types.
pub struct Words;

impl Words {
    /// Creates a new word from its signed 64 bits value.
    pub const fn from_i64(value: i64) -> Word { Word::from_be_bytes(value.to_be_bytes()) }

    /// Creates a new word from its signed 64 bits value.
    pub const fn i64(word: Word) -> i64 { i64::from_be_bytes(word.to_be_bytes()) }
    /// Creates a new word from its signed 64 bits value.
    pub fn from_f64(value: f64) -> Word { Word::from_be_bytes(value.to_be_bytes()) }

    /// Creates a new word from its signed 64 bits value.
    pub fn f64(word: Word) -> f64 { f64::from_be_bytes(word.to_be_bytes()) }

    /// Split a [Word] into two `u32`s.
    pub fn raw_u32s(word: Word) -> (u32, u32) {
        let [l, r] = word.split();
        (l.to_u32(), r.to_u32())
    }

    /// Split a [Word] into four `u16`s.
    pub fn raw_u16s(word: Word) -> (u16, u16, u16, u16) {
        let [l, r] = word.split();
        let l = l .to_u32().to_be_bytes();
        let r = r .to_u32().to_be_bytes();
        (
            u16::from_be_bytes([l[0], l[1]]),
            u16::from_be_bytes([l[2], l[3]]),
            u16::from_be_bytes([r[0], r[1]]),
            u16::from_be_bytes([r[2], r[3]]),
        )
    }
}

/// A collection of function to transmute [Array]s from/into various usual types.
pub struct Arrays;

impl Arrays {
    /// Creates a new [Array] from a slice of `u64`s.
    pub fn from_raw(arr: &[u64]) -> Array {
        Array::from(&arr.iter().map(|u| Word::new(*u)).collect::<Vec<Word>>())
    }

    /// Returns a vector of `u64` from an [Array].
    pub fn raw(array: Array) -> Vec<u64> {
        array.into_iter().map(|word| word.to_u64()).collect()
    }

    /// Creates a new [Array] from a slice of `u64`s.
    pub fn from_sizes(arr: &[usize]) -> Array {
        Array::from(&arr.iter().map(|u| Word::new(*u as u64)).collect::<Vec<Word>>())
    }

    /// Returns a vector of `usize` from an [Array].
    pub fn sizes(array: Array) -> Vec<usize> {
        array.into_iter().map(|word| word.to_u64() as usize).collect()
    }
}