Struct HeaderVec

Source
pub struct HeaderVec<H, T> { /* private fields */ }
Expand description

A vector with a header of your choosing behind a thin pointer

§Example

use core::mem::size_of_val;
use header_vec::HeaderVec;

#[derive(Debug)]
struct OurHeaderType {
    a: usize,
}

let h = OurHeaderType{ a: 2 };
let mut hv = HeaderVec::<OurHeaderType, char>::new(h);
hv.push('x');
hv.push('z');

HeaderVec itself consists solely of a pointer, it’s only 8 bytes big. All of the data, like our header OurHeaderType { a: 2 }, the length of the vector: 2, and the contents of the vector ['x', 'z'] resides on the other side of the pointer.

Implementations§

Source§

impl<H, T> HeaderVec<H, T>

Source

pub fn new(head: H) -> Self

Examples found in repository?
examples/doc_example.rs (line 12)
10fn main() {
11    let h = OurHeaderType { a: 2 };
12    let mut hv = HeaderVec::<OurHeaderType, char>::new(h);
13    hv.push('x');
14    hv.push('z');
15
16    println!(
17        "[`HeaderVec`] itself consists solely of a pointer, it's only {} bytes big.",
18        size_of_val(&hv)
19    );
20    println!(
21        "All of the data, like our header `{:?}`, the length of the vector: `{}`,",
22        &*hv,
23        hv.len()
24    );
25    println!(
26        "and the contents of the vector `{:?}` resides on the other side of the pointer.",
27        hv.as_slice()
28    );
29}
Source

pub fn with_capacity(capacity: usize, head: H) -> Self

Source

pub fn len(&self) -> usize

Examples found in repository?
examples/doc_example.rs (line 23)
10fn main() {
11    let h = OurHeaderType { a: 2 };
12    let mut hv = HeaderVec::<OurHeaderType, char>::new(h);
13    hv.push('x');
14    hv.push('z');
15
16    println!(
17        "[`HeaderVec`] itself consists solely of a pointer, it's only {} bytes big.",
18        size_of_val(&hv)
19    );
20    println!(
21        "All of the data, like our header `{:?}`, the length of the vector: `{}`,",
22        &*hv,
23        hv.len()
24    );
25    println!(
26        "and the contents of the vector `{:?}` resides on the other side of the pointer.",
27        hv.as_slice()
28    );
29}
Source

pub fn is_empty(&self) -> bool

Source

pub fn capacity(&self) -> usize

Source

pub fn as_slice(&self) -> &[T]

Examples found in repository?
examples/doc_example.rs (line 27)
10fn main() {
11    let h = OurHeaderType { a: 2 };
12    let mut hv = HeaderVec::<OurHeaderType, char>::new(h);
13    hv.push('x');
14    hv.push('z');
15
16    println!(
17        "[`HeaderVec`] itself consists solely of a pointer, it's only {} bytes big.",
18        size_of_val(&hv)
19    );
20    println!(
21        "All of the data, like our header `{:?}`, the length of the vector: `{}`,",
22        &*hv,
23        hv.len()
24    );
25    println!(
26        "and the contents of the vector `{:?}` resides on the other side of the pointer.",
27        hv.as_slice()
28    );
29}
Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Source

pub fn ptr(&self) -> *const ()

This is useful to check if two nodes are the same. Use it with HeaderVec::is.

Source

pub fn is(&self, ptr: *const ()) -> bool

This is used to check if this is the HeaderVec that corresponds to the given pointer. This is useful for updating weak references after HeaderVec::push returns the pointer.

Source

pub unsafe fn weak(&self) -> HeaderVecWeak<H, T>

Create a (dangerous) weak reference to the HeaderVec. This is useful to be able to create, for instance, graph data structures. Edges can utilize HeaderVecWeak so that they can traverse the graph immutably without needing to go to memory twice to look up first the pointer to the underlying dynamic edge store (like a Vec). The caveat is that the user is responsible for updating all HeaderVecWeak if the HeaderVec needs to reallocate when HeaderVec::push is called. HeaderVec::push returns true when it reallocates, and this indicates that the HeaderVecWeak need to be updated. Therefore, this works best for implemented undirected graphs where it is easy to find neighbor nodes. Directed graphs with an alternative method to traverse directed edges backwards should also work with this technique.

§Safety

A HeaderVecWeak can only be used while its corresponding HeaderVec is still alive. HeaderVecWeak also MUST be updated manually by the user when HeaderVec::push returns true, since the pointer has now changed. As there is no reference counting mechanism, or method by which all the weak references could be updated, it is up to the user to do this. That is why this is unsafe. Make sure you update your HeaderVecWeak appropriately.

Source

pub unsafe fn update(&mut self, weak: HeaderVecWeak<H, T>)

If a HeaderVec is updated through a weak reference and reallocates, you must use this method to update the internal pointer to the HeaderVec (along with any other weak references).

§Safety

See the safety section in HeaderVec::weak for an explanation of why this is necessary.

Source

pub fn push(&mut self, item: T) -> Option<*const ()>

Adds an item to the end of the list.

Returns true if the memory was moved to a new location. In this case, you are responsible for updating the weak nodes.

Examples found in repository?
examples/doc_example.rs (line 13)
10fn main() {
11    let h = OurHeaderType { a: 2 };
12    let mut hv = HeaderVec::<OurHeaderType, char>::new(h);
13    hv.push('x');
14    hv.push('z');
15
16    println!(
17        "[`HeaderVec`] itself consists solely of a pointer, it's only {} bytes big.",
18        size_of_val(&hv)
19    );
20    println!(
21        "All of the data, like our header `{:?}`, the length of the vector: `{}`,",
22        &*hv,
23        hv.len()
24    );
25    println!(
26        "and the contents of the vector `{:?}` resides on the other side of the pointer.",
27        hv.as_slice()
28    );
29}
Source

pub fn retain(&mut self, f: impl FnMut(&T) -> bool)

Retains only the elements specified by the predicate.

In other words, remove all elements e such that f(&e) returns false. This method operates in place, visiting each element exactly once in the original order, and preserves the order of the retained elements.

Trait Implementations§

Source§

impl<H, T> Clone for HeaderVec<H, T>
where H: Clone, T: Clone,

Source§

fn clone(&self) -> Self

Returns a copy 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<H, T> Debug for HeaderVec<H, T>
where H: Debug, T: Debug,

Source§

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

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

impl<H, T> Deref for HeaderVec<H, T>

Source§

type Target = H

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<H, T> DerefMut for HeaderVec<H, T>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<H, T> Drop for HeaderVec<H, T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<H, T, I> Index<I> for HeaderVec<H, T>
where I: SliceIndex<[T]>,

Source§

type Output = <I as SliceIndex<[T]>>::Output

The returned type after indexing.
Source§

fn index(&self, index: I) -> &I::Output

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

impl<H, T, I> IndexMut<I> for HeaderVec<H, T>
where I: SliceIndex<[T]>,

Source§

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

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

impl<H, T> PartialEq for HeaderVec<H, T>
where H: PartialEq, T: PartialEq,

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.

Auto Trait Implementations§

§

impl<H, T> Freeze for HeaderVec<H, T>

§

impl<H, T> RefUnwindSafe for HeaderVec<H, T>

§

impl<H, T> !Send for HeaderVec<H, T>

§

impl<H, T> !Sync for HeaderVec<H, T>

§

impl<H, T> Unpin for HeaderVec<H, T>
where H: Unpin,

§

impl<H, T> UnwindSafe for HeaderVec<H, T>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.