Struct Overlay

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

A two-layer value container used by OverlayMap to manage current and historical values.

Overlay<T> stores up to two values:

  • A foreground value representing the current state.
  • An optional background value representing the previous state.

When used through OverlayMap, each key maps to an Overlay<T> to track updates without requiring clones or reallocations. You can also use Overlay<T> standalone to manage two-layer state transitions for any value type.

Values are moved, never cloned. All operations (push, pull, swap) are zero-cost and memory-efficient.

§Use Cases

  • Managing current and previous state in UI or simulation logic
  • Efficient delta tracking for configs, game state, etc.
  • Avoiding Option<(T, T)> or custom wrappers with cloning overhead

§Examples

use overlay_map::Overlay;

let mut entry = Overlay::new_fg("current");
entry.push("next"); // moves "current" into background

assert_eq!(entry.fg(), Some(&"next"));
assert_eq!(entry.bg(), Some(&"current"));

let pulled = entry.pull();
assert_eq!(pulled, Some("next"));
assert_eq!(entry.fg(), Some(&"current"));

Implementations§

Source§

impl<T> Overlay<T>

Source

pub fn new_empty() -> Self

Creates a new Overlay with no values.

Source

pub fn new_fg(val: T) -> Self

Creates a new Overlay with a foreground value and no background.

Source

pub fn new_both(fg: T, bg: T) -> Self

Creates a new Overlay with both foreground and background values.

Source

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

Returns a reference to the current foreground value, if present.

This returns Some(&T) only if the foreground slot contains a value. If the slot is logically empty, returns None. This is a safe version that checks the presence bits before accessing memory.

§Safety

This function is fully safe and performs a presence check before dereferencing.

§Returns
  • Some(&T) if the foreground slot is initialized
  • None if the foreground slot is uninitialized
Source

pub fn fg_unchecked(&self) -> &T

Returns a reference to the foreground value without checking if it is present.

§Safety

This function assumes the foreground slot is initialized. Calling this when the slot is uninitialized (i.e. after a pull() without a background, or after constructing an empty Overlay) results in undefined behavior.

Use fg if you are not certain the slot is populated.

§Panics

Never panics, but causes UB if the foreground slot is not present.

Source

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

Returns a reference to the background value, if present.

Returns Some(&T) only if the background slot is initialized.

Source

pub fn bg_unchecked(&self) -> &T

Returns a reference to the background value without checking if it is present.

§Safety

This assumes the background slot is initialized. Calling this when it is not will cause undefined behavior.

Prefer bg if you’re unsure whether the background is set.

Source

pub fn is_empty(&self) -> bool

Returns true if both slots are empty.

This is used to determine whether the entry contains any values at all. It does not consider which slot is foreground or background.

Source

pub fn push(&mut self, val: T)

Push a value into the foreground layer, preserving the previous foreground in the background.

If the foreground slot already contains a value, it is moved into the background slot. The new value is then written into the foreground slot. Any previous background value will be dropped to make room—no cloning is performed at any point.

This operation is always safe, even if the entry is empty. If no foreground is currently present, the value will simply be inserted.

§Example
use overlay_map::Overlay;
let mut entry = Overlay::new_fg("a");
entry.push("b");
assert_eq!(entry.fg(), Some(&"b"));
assert_eq!(entry.bg(), Some(&"a"));
Source

pub fn pull(&mut self) -> Option<T>

Safely pull the current foreground value out, promoting the background to foreground if present.

If the foreground value is present, it is removed and returned. The background (if any) is promoted to the foreground. If neither value remains, the entry becomes empty.

Returns None if the foreground was not present.

§Example
use overlay_map::Overlay;
let mut entry = Overlay::new_both("a", "b");
let pulled = entry.pull();
assert_eq!(pulled, Some("a"));
assert_eq!(entry.fg(), Some(&"b")); // background promoted
Source

pub fn pull_unchecked(&mut self) -> T

Pull the current foreground value without checking if it is present.

§Safety

The caller must ensure the foreground slot is present. If it is not, this will result in undefined behavior.

See Self::pull for a safe alternative.

Source

pub fn swap(&mut self, val: T) -> Option<T>

Swap in a new foreground value, returning the old background if present.

If a background value exists, it is evicted and returned. The new value is written into the background slot, which is then promoted to become the new foreground. The current foreground is preserved in-place.

If no background was present, this behaves like a standard push operation, and None is returned.

§Example
use overlay_map::Overlay;
let mut entry = Overlay::new_both("a", "b");
let evicted = entry.swap("c");
assert_eq!(evicted, Some("b"));
assert_eq!(entry.fg(), Some(&"c"));
assert_eq!(entry.bg(), Some(&"a"));

Trait Implementations§

Source§

impl<T: Debug> Debug for Overlay<T>

Source§

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

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

impl<V> Drop for Overlay<V>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Overlay<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Overlay<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> 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, 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.