Trait scratchpad::Marker [] [src]

pub trait Marker {
    fn allocate<'marker, T>(
        &'marker self,
        value: T
    ) -> Result<Allocation<'marker, T>, Error<(T,)>> { ... }
fn allocate_default<'marker, T: Default>(
        &'marker self
    ) -> Result<Allocation<'marker, T>, Error<()>> { ... }
unsafe fn allocate_uninitialized<'marker, T>(
        &'marker self
    ) -> Result<Allocation<'marker, T>, Error<()>> { ... }
fn allocate_array<'marker, T: Clone>(
        &'marker self,
        len: usize,
        value: T
    ) -> Result<Allocation<'marker, [T]>, Error<(T,)>> { ... }
fn allocate_array_default<'marker, T: Default>(
        &'marker self,
        len: usize
    ) -> Result<Allocation<'marker, [T]>, Error<()>> { ... }
fn allocate_array_with<'marker, T, F: FnMut(usize) -> T>(
        &'marker self,
        len: usize,
        func: F
    ) -> Result<Allocation<'marker, [T]>, Error<()>> { ... }
unsafe fn allocate_array_uninitialized<'marker, T>(
        &'marker self,
        len: usize
    ) -> Result<Allocation<'marker, [T]>, Error<()>> { ... }
fn allocate_slice<'marker, T: ?Sized, U>(
        &'marker self,
        values: U
    ) -> Result<Allocation<'marker, T>, Error<(U,)>>
    where
        T: SliceLike,
        U: OwnedSlice<T>
, { ... }
fn allocate_slice_clone<'marker, T: ?Sized, U>(
        &'marker self,
        slice: &T
    ) -> Result<Allocation<'marker, T>, Error<()>>
    where
        T: SliceLike<Element = U>,
        U: Clone
, { ... }
fn allocate_slice_copy<'marker, T: ?Sized, U>(
        &'marker self,
        slice: &T
    ) -> Result<Allocation<'marker, T>, Error<()>>
    where
        T: SliceLike<Element = U>,
        U: Copy
, { ... }
fn extend<'marker, T: ?Sized, U, V>(
        &'marker self,
        allocation: Allocation<'marker, U>,
        values: V
    ) -> Result<Allocation<'marker, T>, Error<(Allocation<'marker, U>, V)>>
    where
        T: ConcatenateSlice,
        U: ?Sized,
        Allocation<'marker, U>: IntoSliceAllocation<'marker, T>,
        V: OwnedSlice<T>
, { ... }
fn extend_clone<'marker, T, U, IntoIteratorT, IteratorT, RefT>(
        &'marker self,
        allocation: Allocation<'marker, U>,
        values: IntoIteratorT
    ) -> Result<Allocation<'marker, [T]>, Error<(Allocation<'marker, U>,)>>
    where
        T: Clone,
        U: ?Sized,
        Allocation<'marker, U>: IntoSliceAllocation<'marker, [T]>,
        IntoIteratorT: IntoIterator<Item = RefT, IntoIter = IteratorT>,
        IteratorT: ExactSizeIterator<Item = RefT>,
        RefT: Borrow<T>
, { ... }
fn extend_copy<'marker, T, U, IntoIteratorT, IteratorT, RefT>(
        &'marker self,
        allocation: Allocation<'marker, U>,
        values: IntoIteratorT
    ) -> Result<Allocation<'marker, [T]>, Error<(Allocation<'marker, U>,)>>
    where
        T: Copy,
        U: ?Sized,
        Allocation<'marker, U>: IntoSliceAllocation<'marker, [T]>,
        IntoIteratorT: IntoIterator<Item = RefT, IntoIter = IteratorT>,
        IteratorT: ExactSizeIterator<Item = RefT>,
        RefT: Borrow<T>
, { ... }
fn concat_slices_clone<'marker, 'a, T: ?Sized, E>(
        &'marker self,
        slices: &[&T]
    ) -> Result<Allocation<'marker, T>, Error<()>>
    where
        T: ConcatenateSlice + SliceLike<Element = E> + 'a,
        E: Clone
, { ... }
fn concat_slices_copy<'marker, T: ?Sized, E>(
        &'marker self,
        slices: &[&T]
    ) -> Result<Allocation<'marker, T>, Error<()>>
    where
        T: ConcatenateSlice + SliceLike<Element = E>,
        E: Copy
, { ... } }

Scratchpad allocation marker implementation trait.

This provides the shared interface for the MarkerFront and MarkerBack types.

Provided Methods

Allocates space for the given value, moving it into the allocation.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 1], [usize; 1]>::new([0], [0]);
let marker = scratchpad.mark_front().unwrap();

let x = marker.allocate(3.14159).unwrap();
assert_eq!(*x, 3.14159);

Allocates space for a value, initializing it to its default.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 1], [usize; 1]>::new([0], [0]);
let marker = scratchpad.mark_front().unwrap();

let x = marker.allocate_default::<f64>().unwrap();
assert_eq!(*x, 0.0);

Allocates uninitialized space for the given type.

Safety

Since memory for the allocated data is uninitialized, it can potentially be in an invalid state for a given type, leading to undefined program behavior. It is recommended that one of the safe allocate*() methods are used instead if possible.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 1], [usize; 1]>::new([0], [0]);
let marker = scratchpad.mark_front().unwrap();

let mut x = unsafe { marker.allocate_uninitialized().unwrap() };
*x = 3.14159;
assert_eq!(*x, 3.14159);

Allocates space for an array, initializing each element with the given value.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 3], [usize; 1]>::new([0; 3], [0]);
let marker = scratchpad.mark_front().unwrap();

let x = marker.allocate_array(3, 3.14159).unwrap();
assert_eq!(*x, [3.14159, 3.14159, 3.14159]);

Allocates space for an array, initializing each element to its default value.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 3], [usize; 1]>::new([0; 3], [0]);
let marker = scratchpad.mark_front().unwrap();

let x = marker.allocate_array_default::<f64>(3).unwrap();
assert_eq!(*x, [0.0, 0.0, 0.0]);

Allocates space for an array, initializing each element with the result of a function.

The function func takes a single parameter containing the index of the element being initialized.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 3], [usize; 1]>::new([0; 3], [0]);
let marker = scratchpad.mark_front().unwrap();

let x = marker.allocate_array_with(3, |index| index as f64).unwrap();
assert_eq!(*x, [0.0, 1.0, 2.0]);

Allocates uninitialized space for an array of the given type.

Safety

Since memory for the allocated data is uninitialized, it can potentially be in an invalid state for a given type, leading to undefined program behavior. It is recommended that one of the safe allocate*() methods are used instead if possible.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 3], [usize; 1]>::new([0; 3], [0]);
let marker = scratchpad.mark_front().unwrap();

let mut x = unsafe {
    marker.allocate_array_uninitialized(3).unwrap()
};
x[0] = 3.14159;
x[1] = 4.14159;
x[2] = 5.14159;
assert_eq!(*x, [3.14159, 4.14159, 5.14159]);

Allocates a slice, initializing its contents by moving the given values into the allocation.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u64; 3], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let values = [3.14159, 4.14159, 5.14159];
let allocation = marker.allocate_slice(values).unwrap();
let allocation_slice: &[f64] = &*allocation;
assert_eq!(*allocation_slice, [3.14159, 4.14159, 5.14159]);

Allocates a copy of a slice by cloning each individual element.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u8; 32], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let message = "foo";
let allocation = marker.allocate_slice_clone(message).unwrap();
assert_eq!(&*allocation, "foo");

Allocates a copy of a slice by performing a fast copy (equivalent to C's memcpy() function) into the new allocation.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u8; 32], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let message = "foo";
let allocation = marker.allocate_slice_copy(message).unwrap();
assert_eq!(&*allocation, "foo");

Extends an allocation by moving values onto one of its ends, converting the allocation to a slice if necessary.

Whether values are added to the beginning or end depends on the marker type: MarkerFront objects will always append, while MarkerBack objects will always prepend.

The following requirements must be met to allow allocations to be extended:

  • The marker must be the most recently created marker of its type (front or back) from its scratchpad. Front and back markers can be extended independently.
  • The allocation must contain a scalar, array, or slice of the type being pushed.
  • The data being pushed must be a scalar, array, boxed slice, or vector of the same type.
  • The allocation must be the most recent allocation made from this marker, even if other, more recent allocations have been released.

Examples

use scratchpad::{Marker, Scratchpad};

let scratchpad = Scratchpad::<[u32; 5], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let a = marker.allocate([3.14159f32, 2.71828f32]).unwrap();

let ab = marker.extend(a, 0.70711f32).unwrap();
assert_eq!(*ab, [3.14159f32, 2.71828f32, 0.70711f32]);

let abc = marker.extend(ab, vec![0.57722f32, 1.61803f32]).unwrap();
assert_eq!(
    *abc,
    [3.14159f32, 2.71828f32, 0.70711f32, 0.57722f32, 1.61803f32],
);

Extends an allocation by cloning values onto one of its ends, converting the allocation to a slice if necessary.

Whether values are added to the beginning or end depends on the marker type: MarkerFront objects will always append, while MarkerBack objects will always prepend.

The following requirements must be met to allow elements to be appended:

  • The marker must be the most recently created marker of its type (front or back) from its scratchpad. Front and back markers can be extended independently.
  • The allocation must contain a scalar, array, or slice of the type being pushed.
  • The data being pushed must be a scalar, array, boxed slice, or vector of the same type.
  • The allocation must be the most recent allocation made from this marker, even if other, more recent allocations have been released.

Note that the values parameter is consumed as part of the allocation process and cannot be returned on error.

Examples

use scratchpad::{Allocation, IntoSliceAllocation, Marker, Scratchpad};

let scratchpad = Scratchpad::<[u32; 5], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let a: Allocation<[f32]> = marker.allocate([3.14159f32, 2.71828f32])
    .unwrap()
    .into_slice_allocation();

let ab = marker.extend_clone(a, &[0.57722f32, 1.61803f32][..])
    .unwrap();
assert_eq!(*ab, [3.14159f32, 2.71828f32, 0.57722f32, 1.61803f32]);

Extends an allocation by copying values onto one of its ends, converting the allocation to a slice if necessary.

Whether values are added to the beginning or end depends on the marker type: MarkerFront objects will always append, while MarkerBack objects will always prepend.

The following requirements must be met to allow elements to be appended:

  • The marker must be the most recently created marker of its type (front or back) from its scratchpad. Front and back markers can be extended independently.
  • The allocation must contain a scalar, array, or slice of the type being pushed.
  • The data being pushed must be a scalar, array, boxed slice, or vector of the same type.
  • The allocation must be the most recent allocation made from this marker, even if other, more recent allocations have been released.

Note that the values parameter is consumed as part of the allocation process and cannot be returned on error.

Examples

use scratchpad::{Allocation, IntoSliceAllocation, Marker, Scratchpad};

let scratchpad = Scratchpad::<[u32; 5], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let a: Allocation<[f32]> = marker.allocate([3.14159f32, 2.71828f32])
    .unwrap()
    .into_slice_allocation();

let ab = marker.extend_copy(a, &[0.57722f32, 1.61803f32][..])
    .unwrap();
assert_eq!(*ab, [3.14159f32, 2.71828f32, 0.57722f32, 1.61803f32]);

Combines cloned copies of each of the provided slices into a single slice allocated from this marker.

Examples

use scratchpad::Scratchpad;

let scratchpad = Scratchpad::<[u8; 16], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let combined = marker.concat_slices_clone(&["Hello,", " world", "!"])
    .unwrap();
assert_eq!(&*combined, "Hello, world!");

Combines copies of each of the provided slices into a single slice allocated from this marker.

Slices are copied by performing a fast memory copy from each of the source slices (equivalent to C's memcpy() function).

Examples

use scratchpad::{Allocation, Scratchpad};

let scratchpad = Scratchpad::<[u8; 16], [usize; 1]>::static_new();
let marker = scratchpad.mark_front().unwrap();

let combined = marker.concat_slices_copy(&["Hello,", " world", "!"])
    .unwrap();
assert_eq!(&*combined, "Hello, world!");

Implementors