Struct RecordBook

Source
pub struct RecordBook { /* private fields */ }

Implementations§

Source§

impl RecordBook

Source

pub fn entries(&self) -> &[Record]

Entries in insertion order, which is also their serialized index order.

Source

pub fn ordered_entries(&self) -> Vec<&Record>

A view ordered lexicographically by ID, then by ascending numeric context.

Allocates at most MAX_RECORDS references. Does not reorder the book or its serialized entries; score/time ties do not affect identity ordering.

use ferravex_play::persistence::RecordBook;
let mut book = RecordBook::default();
book.record("z-course", 1, 10, None)?;
book.record("a-course", 2, 10, None)?;
book.record("a-course", 1, 10, None)?;
let keys: Vec<_> = book.ordered_entries().into_iter()
    .map(|record| (record.id.as_str(), record.context)).collect();
assert_eq!(keys, [("a-course", 1), ("a-course", 2), ("z-course", 1)]);
assert_eq!(book.entries()[0].id, "z-course");
Source

pub fn get(&self, id: &str, context: u64) -> Option<&Record>

Source

pub fn record( &mut self, id: &str, context: u64, score: u64, time_us: Option<u64>, ) -> Result<(), SaveError>

Record one completed attempt. Only supply time for a successful finish. Score/time maxima and context identity never pass through floating point.

Source

pub fn compare( &self, id: &str, context: u64, score: u64, time_us: Option<u64>, ) -> Result<RecordSummary, SaveError>

Preview an attempt against the current bests without mutation or allocation.

Uses exactly the acceptance checks of Self::record: malformed IDs and zero/out-of-range times return SaveError::InvalidData; a new identity in a full book or an overflowing run count returns SaveError::Capacity. A full book can still accept attempts for an existing identity. None supplies no successful time; the score and attempt count still apply.

A preview does not reserve an attempt. Use Self::record_with_summary to emit feedback from the summary of an actually accepted attempt.

use ferravex_play::persistence::RecordBook;
let mut book = RecordBook::default();
book.record("course", 1, 100, Some(2_000_000))?;
let preview = book.compare("course", 1, 90, Some(1_500_000))?;
assert_eq!(preview.score.delta, Some(-10));
assert!(!preview.score.comparison.is_new_best());
assert_eq!(preview.time_us.unwrap().delta, Some(-500_000));
assert!(preview.time_us.unwrap().comparison.is_new_best());
assert_eq!(preview.runs, 2);
assert_eq!(book.get("course", 1).unwrap().runs, 1);
Source

pub fn record_with_summary( &mut self, id: &str, context: u64, score: u64, time_us: Option<u64>, ) -> Result<RecordSummary, SaveError>

Accept one attempt and return its comparisons against the previous bests.

Uses the same validation and independent best updates as Self::record. All checks finish before mutation: errors leave the entire book unchanged. Ties and worse runs are accepted and increment runs without replacing their respective bests. Summary calculation allocates no storage; a new identity retains the existing bounded entry/ID allocation behavior.

use ferravex_play::persistence::RecordBook;
let mut book = RecordBook::default();
let first = book.record_with_summary("course", 1, 0, None)?;
assert!(first.score.comparison.is_new_best());
assert_eq!(first.score.delta, None);
let next = book.record_with_summary("course", 1, u64::MAX, Some(1))?;
assert_eq!(next.score.delta, Some(i128::from(u64::MAX)));
assert_eq!(next.time_us.unwrap().previous, None);
assert_eq!(book.get("course", 1).unwrap().runs, 2);

Trait Implementations§

Source§

impl Clone for RecordBook

Source§

fn clone(&self) -> RecordBook

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RecordBook

Source§

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

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

impl Default for RecordBook

Source§

fn default() -> RecordBook

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

impl PartialEq for RecordBook

Source§

fn eq(&self, other: &RecordBook) -> 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 SaveData for RecordBook

Source§

const SCHEMA: u64 = 5_068_891_838_040_986_180u64

Source§

const VERSION: u32 = 1u32

Source§

const MAX_BYTES: usize = 16_384usize

Source§

fn write(&self, d: &mut Document) -> Result<(), SaveError>

Source§

fn read(d: &Document) -> Result<RecordBook, SaveError>

Source§

fn encode(&self, generation: u64) -> Result<Vec<u8>, SaveError>

Source§

fn decode(data: &[u8]) -> Result<(Self, u64), SaveError>

Source§

impl Eq for RecordBook

Source§

impl StructuralPartialEq for RecordBook

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> 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.