Skip to main content

NearList

Struct NearList 

Source
pub struct NearList<T> { /* private fields */ }
Expand description

A linked list of T values stored in a Region.

§Layout

#[repr(C)] — 8 bytes, align_of::<i32>() alignment:

OffsetFieldTypeDescription
0headi32Self-relative offset to first segment
4lenu32TOTAL element count across all segments

When len == 0, head is 0 (no valid segment). When non-empty, head is a self-relative offset from the head field itself to the first Segment<T>.

NearList<T> is not Copy or Clone — moving it would invalidate the self-relative offset.

After trim, all elements live in a single contiguous segment for cache-friendly iteration.

§Segment chain

A non-empty list consists of one or more Segment<T> nodes linked by self-relative next pointers (next == 0 terminates the chain). Each segment stores a contiguous array of T values immediately after its header.

Mutation operations (splice_list, push_front, extend_list) may produce multi-segment lists. trim compacts all segments into one, restoring O(1) indexing and cache-friendly iteration.

§Soundness

Provenance: Segment walking uses with_exposed_provenance to follow self-relative next pointers, recovering the allocation’s provenance exposed by AlignedBuf::grow. Same pattern as Near<T>.

Invariant: len == 0head == 0. Enforced by all list-patching methods which write both fields atomically.

Implementations§

Source§

impl<T: Flat> NearList<T>

Source

pub const fn len(&self) -> usize

Returns the number of elements.

§Examples
use nearest::{Flat, NearList, Region, empty, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let region = Region::new(Root::make(list([1u32, 2, 3])));
assert_eq!(region.items.len(), 3);

let empty_region = Region::new(Root::make(empty()));
assert_eq!(empty_region.items.len(), 0);
Source

pub const fn is_empty(&self) -> bool

Returns true if the list has no elements.

§Examples
use nearest::{Flat, NearList, Region, empty, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let region = Region::new(Root::make(empty()));
assert!(region.items.is_empty());

let region = Region::new(Root::make(list([1u32])));
assert!(!region.items.is_empty());
Source

pub fn segment_count(&self) -> usize

Returns the number of segments in the chain.

After trim, this is always 0 (empty) or 1.

§Examples
use nearest::{Flat, NearList, Region, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let mut region = Region::new(Root::make(list([1u32, 2, 3])));
assert_eq!(region.items.segment_count(), 1);

// push_front adds a new segment.
region.session(|s| {
  let items = s.nav(s.root(), |r| &r.items);
  s.push_front(items, 0u32);
});
assert_eq!(region.items.segment_count(), 2);

// trim consolidates into one segment.
region.trim();
assert_eq!(region.items.segment_count(), 1);
Source

pub fn last(&self) -> Option<&T>

Returns a reference to the last element, or None if empty.

O(n): requires full traversal of the segment chain to find the last segment, then indexes its final element.

§Examples
use nearest::{Flat, NearList, Region, empty, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let region = Region::new(Root::make(list([10u32, 20, 30])));
assert_eq!(region.items.last(), Some(&30));

let region = Region::new(Root::make(empty()));
assert_eq!(region.items.last(), None);
Source

pub fn contains(&self, item: &T) -> bool
where T: PartialEq,

Returns true if the list contains an element equal to the given value.

§Examples
use nearest::{Flat, NearList, Region, empty, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let region = Region::new(Root::make(list([1u32, 2, 3])));
assert!(region.items.contains(&2));
assert!(!region.items.contains(&4));

let region = Region::new(Root::make(empty()));
assert!(!region.items.contains(&1));
Source

pub fn first(&self) -> Option<&T>

Returns a reference to the first element, or None if empty.

§Examples
use nearest::{Flat, NearList, Region, empty, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let region = Region::new(Root::make(list([10u32, 20])));
assert_eq!(region.items.first(), Some(&10));

let region = Region::new(Root::make(empty()));
assert_eq!(region.items.first(), None);
Source

pub fn iter(&self) -> NearListIter<'_, T>

Returns an iterator over the elements.

Construction is O(1). Within a segment, iteration is contiguous (ptr.add(1)). At segment boundaries, follows next pointers to the next segment. After trim, iteration is pure array traversal.

§Examples
use nearest::{Flat, NearList, Region, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let region = Region::new(Root::make(list([10u32, 20, 30])));
let sum: u32 = region.items.iter().sum();
assert_eq!(sum, 60);

// Also works with for-in (via IntoIterator for &NearList<T>).
let mut count = 0;
for item in &region.items {
  count += 1;
  assert!(*item >= 10);
}
assert_eq!(count, 3);
Source§

impl<T: Flat> NearList<T>

Source

pub fn get(&self, index: usize) -> Option<&T>

Returns a reference to the element at index, or None if out of bounds.

O(1) when all elements are in a single segment (always true after trim). Falls back to O(n) segment walk otherwise.

§Examples
use nearest::{Flat, NearList, Region, list};

#[derive(Flat)]
struct Root { items: NearList<u32> }

let region = Region::new(Root::make(list([10u32, 20, 30])));
assert_eq!(region.items.get(0), Some(&10));
assert_eq!(region.items.get(2), Some(&30));
assert_eq!(region.items.get(3), None);

Trait Implementations§

Source§

impl<T: Flat + Debug> Debug for NearList<T>

Source§

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

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

impl<T: Flat + Display> Display for NearList<T>

Source§

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

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

impl<T: Flat> Flat for NearList<T>

Source§

unsafe fn deep_copy(&self, p: &mut impl Patch, at: Pos)

Deep-copy self into the buffer at position at, patching all self-relative pointers so they remain valid in the new location. Read more
Source§

fn validate(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Validate that buf[addr..] contains a valid representation of Self. Read more
Source§

fn validate_option(addr: usize, buf: &[u8]) -> Result<(), ValidateError>

Validate an Option<Self> at addr in buf. Read more
Source§

impl<T: Flat> Index<usize> for NearList<T>

Source§

fn index(&self, index: usize) -> &T

Access the element at index.

O(1) when all elements are in a single segment (always true after trim). Falls back to O(n) segment walk otherwise.

§Panics

Panics if index >= self.len().

Source§

type Output = T

The returned type after indexing.
Source§

impl<'a, T: Flat> IntoIterator for &'a NearList<T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = NearListIter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Flat + PartialEq> PartialEq for NearList<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<T: Flat + Eq> Eq for NearList<T>

Auto Trait Implementations§

§

impl<T> Freeze for NearList<T>

§

impl<T> RefUnwindSafe for NearList<T>
where T: RefUnwindSafe,

§

impl<T> Send for NearList<T>
where T: Send,

§

impl<T> Sync for NearList<T>
where T: Sync,

§

impl<T> Unpin for NearList<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for NearList<T>

§

impl<T> UnwindSafe for NearList<T>
where 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> Emit<T> for T
where T: Flat,

Source§

unsafe fn write_at(self, p: &mut impl Patch, at: Pos)

Write this builder’s data at position at. Read more
Source§

fn emit(self, p: &mut impl Patch) -> Pos
where T: Flat,

Reserve space for T, write this builder’s data, and return the position.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.