Skip to main content

MemorySink

Struct MemorySink 

Source
pub struct MemorySink { /* private fields */ }
Available on crate feature alloc only.
Expand description

In-memory Sink: appends every record into a Vec<OwnedRecord>.

Intended for tests, prototypes, and short-lived buffering. Holds the entire chain in memory, so it is not suitable for long-running production workloads — use a file-backed or streaming sink instead.

§Example

use audit_trail::{
    Action, Actor, Chain, Clock, Digest, Hasher, MemorySink, Outcome, Record, RecordId,
    SinkError, Target, Timestamp, HASH_LEN,
};

// Minimal hasher / clock for the example.
struct XorHasher([u8; HASH_LEN], usize);
impl Hasher for XorHasher {
    fn reset(&mut self) { self.0 = [0u8; HASH_LEN]; self.1 = 0; }
    fn update(&mut self, bytes: &[u8]) {
        for b in bytes { self.0[self.1 % HASH_LEN] ^= *b; self.1 = self.1.wrapping_add(1); }
    }
    fn finalize(&mut self, out: &mut Digest) { *out = Digest::from_bytes(self.0); }
}
struct Tick(core::cell::Cell<u64>);
impl Clock for Tick {
    fn now(&self) -> Timestamp {
        let v = self.0.get(); self.0.set(v + 1); Timestamp::from_nanos(v)
    }
}

let mut chain = Chain::new(
    XorHasher([0u8; HASH_LEN], 0),
    MemorySink::new(),
    Tick(core::cell::Cell::new(1)),
);
chain.append(Actor::new("a"), Action::new("x"), Target::new("t"), Outcome::Success).unwrap();

assert_eq!(chain.sink().len(), 1);

Implementations§

Source§

impl MemorySink

Source

pub const fn new() -> Self

Construct an empty MemorySink.

Source

pub fn with_capacity(cap: usize) -> Self

Construct a MemorySink with pre-allocated capacity for cap records.

Source

pub fn len(&self) -> usize

Number of records currently held.

Source

pub fn is_empty(&self) -> bool

true if no records have been written.

Source

pub fn records(&self) -> &[OwnedRecord]

Borrow the recorded log.

Source

pub fn into_records(self) -> Vec<OwnedRecord>

Consume the sink and return its records.

Source

pub fn clear(&mut self)

Drop all stored records.

Trait Implementations§

Source§

impl Clone for MemorySink

Source§

fn clone(&self) -> MemorySink

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MemorySink

Source§

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

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

impl Default for MemorySink

Source§

fn default() -> MemorySink

Returns the “default value” for a type. Read more
Source§

impl Sink for MemorySink

Source§

fn write(&mut self, record: &Record<'_>) -> Result<(), SinkError>

Persist a single record. Read more

Auto Trait Implementations§

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<T> Same for T

Source§

type Output = T

Should always be Self
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.