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
impl MemorySink
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Construct an empty MemorySink.
Sourcepub fn with_capacity(cap: usize) -> Self
pub fn with_capacity(cap: usize) -> Self
Construct a MemorySink with pre-allocated capacity for
cap records.
Sourcepub fn records(&self) -> &[OwnedRecord]
pub fn records(&self) -> &[OwnedRecord]
Borrow the recorded log.
Sourcepub fn into_records(self) -> Vec<OwnedRecord>
pub fn into_records(self) -> Vec<OwnedRecord>
Consume the sink and return its records.
Trait Implementations§
Source§impl Clone for MemorySink
impl Clone for MemorySink
Source§fn clone(&self) -> MemorySink
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)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for MemorySink
impl Debug for MemorySink
Source§impl Default for MemorySink
impl Default for MemorySink
Source§fn default() -> MemorySink
fn default() -> MemorySink
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for MemorySink
impl RefUnwindSafe for MemorySink
impl Send for MemorySink
impl Sync for MemorySink
impl Unpin for MemorySink
impl UnsafeUnpin for MemorySink
impl UnwindSafe for MemorySink
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more