Skip to main content

Array

Struct Array 

Source
pub struct Array<M, T> { /* private fields */ }
Expand description

A reference to a fixed-length array of values stored on the trail.

The type parameter T is the type of value stored on the trail, and the type parameter M represents how the value is stored on the trail. An Array<Backtrackable, T> is stored on the trail in backtrackable memory, whereas an Array<NonBacktrackable, T> is stored on the trail in non-backtrackable memory.

Instead of using Array directly, it’s often easier to use the type definitions BacktrackableArray and NonBacktrackableArray.

Implementations§

Source§

impl<M, T> Array<M, T>
where M: StorageMode, T: Bytes,

Source

pub fn new( builder: &mut TrailBuilder, vals: impl IntoIterator<Item = T>, ) -> Self

Creates a new Array with the given values.

The Array is usable after the TrailBuilder used to create it is finished.

§Examples
use contrail::{BacktrackableArray, TrailBuilder};

let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 5..10);
let trail = builder.finish();

// the array is usable now
assert_eq!(array.get(&trail, 2), 7);
Source

pub fn len(&self) -> usize

Returns the length of the array.

§Examples
use contrail::{BacktrackableArray, TrailBuilder};

let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..8);

assert_eq!(array.len(), 8);
Source

pub fn is_empty(&self) -> bool

Checks if the length of the array is equal to 0.

§Examples
use contrail::{BacktrackableArray, TrailBuilder};

let mut builder = TrailBuilder::new();
let empty = BacktrackableArray::new(&mut builder, 0..0);
let not_empty = BacktrackableArray::new(&mut builder, 0..1);

assert_eq!(empty.is_empty(), true);
assert_eq!(not_empty.is_empty(), false);
Source

pub fn iter<'t>(&self, trail: &'t Trail) -> ArrayIter<'t, M, T>

Returns an iterator over the elements of the array.

§Examples
use contrail::{BacktrackableArray, TrailBuilder};

let mut builder = TrailBuilder::new();
let odds = BacktrackableArray::new(&mut builder, (0..10).map(|x| 2 * x + 1));
let trail = builder.finish();

for odd in odds.iter(&trail) {
    assert_eq!(odd % 2, 1);
}
Source

pub fn get(&self, trail: &Trail, i: usize) -> T

Gets the value of the array at the given index.

§Panics

Panics if the index is out of bounds.

§Examples
use contrail::{BacktrackableArray, Trail, TrailBuilder};

let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..10);
let mut trail = builder.finish();

assert_eq!(array.get(&trail, 4), 4);
Source

pub fn set(&self, trail: &mut Trail, i: usize, new_val: T)

Sets the value of the array at the given index.

§Panics

Panics if the index is out of bounds.

§Examples
use contrail::{BacktrackableArray, Trail, TrailBuilder};

let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..10);
let mut trail = builder.finish();

assert_eq!(array.get(&trail, 4), 4);

array.set(&mut trail, 4, -23);
assert_eq!(array.get(&trail, 4), -23);
Source

pub fn update(&self, trail: &mut Trail, i: usize, f: impl FnOnce(T) -> T)

Updates the value of the array at the given index using the given update function.

§Panics

Panics if the index is out of bounds.

§Examples
use contrail::{BacktrackableArray, Trail, TrailBuilder};

let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, 0..10);
let mut trail = builder.finish();

assert_eq!(array.get(&trail, 4), 4);

array.update(&mut trail, 4, |x| x * x);
assert_eq!(array.get(&trail, 4), 16);
Source

pub fn swap(&self, trail: &mut Trail, i: usize, j: usize)

Swaps the two values at the given indices of the array in memory.

§Panics

Panics if either of the indices are out of bounds.

§Examples
use contrail::{BacktrackableArray, TrailBuilder};

let mut builder = TrailBuilder::new();
let array = BacktrackableArray::new(&mut builder, vec!['r', 'u', 't', 's']);
let mut trail = builder.finish();

assert_eq!(array.get(&trail, 2), 't');
assert_eq!(array.get(&trail, 3), 's');

array.swap(&mut trail, 2, 3);

assert_eq!(array.get(&trail, 2), 's');
assert_eq!(array.get(&trail, 3), 't');

Trait Implementations§

Source§

impl<M, T> Clone for Array<M, T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M, T> Debug for Array<M, T>
where M: StorageMode,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M, T> PartialEq for Array<M, T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<M, T> Copy for Array<M, T>

Source§

impl<M, T> Eq for Array<M, T>

Auto Trait Implementations§

§

impl<M, T> Freeze for Array<M, T>

§

impl<M, T> RefUnwindSafe for Array<M, T>

§

impl<M, T> Send for Array<M, T>
where M: Send, T: Send,

§

impl<M, T> Sync for Array<M, T>
where M: Sync, T: Sync,

§

impl<M, T> Unpin for Array<M, T>
where M: Unpin, T: Unpin,

§

impl<M, T> UnsafeUnpin for Array<M, T>

§

impl<M, T> UnwindSafe for Array<M, T>
where M: UnwindSafe, T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.