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>
impl<H, T> HeaderVec<H, T>
Sourcepub fn new(head: H) -> Self
pub fn new(head: H) -> Self
Examples found in repository?
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}
pub fn with_capacity(capacity: usize, head: H) -> Self
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Examples found in repository?
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}
pub fn is_empty(&self) -> bool
pub fn capacity(&self) -> usize
Sourcepub fn as_slice(&self) -> &[T]
pub fn as_slice(&self) -> &[T]
Examples found in repository?
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}
pub fn as_mut_slice(&mut self) -> &mut [T]
Sourcepub fn ptr(&self) -> *const ()
pub fn ptr(&self) -> *const ()
This is useful to check if two nodes are the same. Use it with HeaderVec::is
.
Sourcepub fn is(&self, ptr: *const ()) -> bool
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.
Sourcepub unsafe fn weak(&self) -> HeaderVecWeak<H, T>
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.
Sourcepub unsafe fn update(&mut self, weak: HeaderVecWeak<H, T>)
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.
Sourcepub fn push(&mut self, item: T) -> Option<*const ()>
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?
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}
Sourcepub fn retain(&mut self, f: impl FnMut(&T) -> bool)
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.