fix!: make the Time generic reachable
`History<T: Time, ..>` has always been generic over the time axis,
`Untimed` has always been exported, and `Drift<T>` is generic specifically
so that "seasonal or calendar-aware drift is expressible without going
through i64". None of it was reachable from a downstream crate.
Every construction route pinned `T = i64`: `History::builder()`,
`History::builder_with_key()`, and the only `Default` impl on
`HistoryBuilder`. Its fields are private and it had no `new`. So all three
escape routes failed to compile, and a consumer with domain timestamps
had to convert to i64 — which is the exact thing the parameter exists to
avoid. One of `History`'s four type parameters was paid for at every
signature and could never be varied.
`Default` is now generic over `T` and `K`, `HistoryBuilder::new()` exists,
and `time_type::<T2>()` / `key_type::<K2>()` join `drift` and `observer`
as type-changing setters:
History::builder().time_type::<Untimed>().build()
History::builder().key_type::<String>().build()
HistoryBuilder::<Season, _, _, String>::new().build()
`key_type` replaces `builder_with_key`, which could not be turbofished —
`K` sat on the impl rather than the function, so callers had to spell
`History::<i64, _, _, String>::builder_with_key()`. 18 call sites across
15 files migrated.
tests/time_axis.rs is the part that matters. NOTHING in the repository
constructed a non-i64 history, which is precisely why this survived, so
the fix is only half done without a test that exercises the generic. It
defines a `Season(u16)` time type and a `SeasonalDrift` that accumulates
between seasons but not within one — the calendar-aware case the trait's
docs cite — and checks the whole path: fit, converge, and read a learning
curve whose times come back as `Season`, not as integers.
Two of the six tests are controls rather than assertions about output.
`Untimed` must ignore drift entirely, since elapsed is always zero, so
gamma 0.0 and gamma 5.0 must agree bit for bit. And a custom `Drift` must
actually widen a gap across seasons, or the test above would pass whether
or not the drift was consulted at all.
The README's ticked "Generalise a time axis" box is now true.
BREAKING CHANGE: `History::builder_with_key()` is removed. Use
`History::builder().key_type::<K>()`.
Closes #68
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011hcFjNDmHXZF8URGLku5zZ
This commit is contained in:
+94
-18
@@ -182,6 +182,73 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> HistoryBuilder<
|
||||
self
|
||||
}
|
||||
|
||||
/// Change the time axis, keeping every other setting.
|
||||
///
|
||||
/// The same shape as [`HistoryBuilder::drift`] and
|
||||
/// [`HistoryBuilder::observer`], which already move between type
|
||||
/// parameters. Call it before setting a drift that is specific to one time
|
||||
/// type, since the stored drift and observer must also be valid for `T2`.
|
||||
///
|
||||
/// ```
|
||||
/// # use trueskill_tt::{History, Untimed};
|
||||
/// let mut h = History::builder().time_type::<Untimed>().build();
|
||||
/// h.record_winner(&"alice", &"bob", Untimed)?;
|
||||
/// h.converge()?;
|
||||
/// assert!(h.current_skill(&"alice").unwrap().mu() > 0.0);
|
||||
/// # Ok::<(), trueskill_tt::InferenceError>(())
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn time_type<T2>(self) -> HistoryBuilder<T2, D, O, K>
|
||||
where
|
||||
T2: Time,
|
||||
D: Drift<T2>,
|
||||
O: Observer<T2>,
|
||||
{
|
||||
HistoryBuilder {
|
||||
mu: self.mu,
|
||||
sigma: self.sigma,
|
||||
beta: self.beta,
|
||||
drift: self.drift,
|
||||
p_draw: self.p_draw,
|
||||
score_sigma: self.score_sigma,
|
||||
convergence: self.convergence,
|
||||
observer: self.observer,
|
||||
unknown_keys: self.unknown_keys,
|
||||
_time: PhantomData,
|
||||
_key: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Change the key type, keeping every other setting.
|
||||
///
|
||||
/// Replaces the former `History::builder_with_key`, which could not be
|
||||
/// turbofished — `K` sat on the `impl` rather than the function, so
|
||||
/// `History::builder_with_key::<String>()` was a compile error and callers
|
||||
/// had to spell the whole `History::builder().key_type::<String>()`.
|
||||
///
|
||||
/// ```
|
||||
/// # use trueskill_tt::History;
|
||||
/// let mut h = History::builder().key_type::<String>().build();
|
||||
/// h.record_winner(&"alice".to_string(), &"bob".to_string(), 1)?;
|
||||
/// # Ok::<(), trueskill_tt::InferenceError>(())
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn key_type<K2: Eq + Hash + Clone>(self) -> HistoryBuilder<T, D, O, K2> {
|
||||
HistoryBuilder {
|
||||
mu: self.mu,
|
||||
sigma: self.sigma,
|
||||
beta: self.beta,
|
||||
drift: self.drift,
|
||||
p_draw: self.p_draw,
|
||||
score_sigma: self.score_sigma,
|
||||
convergence: self.convergence,
|
||||
observer: self.observer,
|
||||
unknown_keys: self.unknown_keys,
|
||||
_time: PhantomData,
|
||||
_key: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn observer<O2: Observer<T>>(self, observer: O2) -> HistoryBuilder<T, D, O2, K> {
|
||||
HistoryBuilder {
|
||||
mu: self.mu,
|
||||
@@ -218,7 +285,14 @@ impl<T: Time, D: Drift<T>, O: Observer<T>, K: Eq + Hash + Clone> HistoryBuilder<
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for HistoryBuilder<i64, ConstantDrift, NullObserver, &'static str> {
|
||||
/// Generic over the time axis and the key type, so a builder exists for every
|
||||
/// `T: Time` rather than only for `i64`.
|
||||
///
|
||||
/// It used to be implemented for the `i64`/`&'static str` instantiation alone.
|
||||
/// That, plus private fields and no `new`, meant a downstream crate could not
|
||||
/// construct a `History` on any other time axis at all — `Untimed` and every
|
||||
/// custom `Drift<T>` were public but unreachable.
|
||||
impl<T: Time, K: Eq + Hash + Clone> Default for HistoryBuilder<T, ConstantDrift, NullObserver, K> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
mu: MU,
|
||||
@@ -350,23 +424,25 @@ impl History<i64, ConstantDrift, NullObserver, &'static str> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<K: Eq + Hash + Clone> History<i64, ConstantDrift, NullObserver, K> {
|
||||
/// Like `builder()` but uses a custom key type `K` instead of the default `&'static str`.
|
||||
impl<T: Time, K: Eq + Hash + Clone> HistoryBuilder<T, ConstantDrift, NullObserver, K> {
|
||||
/// A builder on any time axis and key type, with the default drift and no
|
||||
/// observer.
|
||||
///
|
||||
/// [`History::builder`] is the common case and pins `T = i64`,
|
||||
/// `K = &'static str`. Reach for this — or for the type-changing
|
||||
/// [`HistoryBuilder::time_type`] / [`HistoryBuilder::key_type`] — when
|
||||
/// either needs to be something else.
|
||||
///
|
||||
/// ```
|
||||
/// # use trueskill_tt::{History, HistoryBuilder, Untimed};
|
||||
/// let mut h = HistoryBuilder::<Untimed, _, _, String>::new().build();
|
||||
/// h.record_winner(&"alice".to_string(), &"bob".to_string(), Untimed)?;
|
||||
/// h.converge()?;
|
||||
/// # Ok::<(), trueskill_tt::InferenceError>(())
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn builder_with_key() -> HistoryBuilder<i64, ConstantDrift, NullObserver, K> {
|
||||
HistoryBuilder {
|
||||
mu: MU,
|
||||
sigma: SIGMA,
|
||||
beta: BETA,
|
||||
drift: ConstantDrift::new(GAMMA),
|
||||
p_draw: P_DRAW,
|
||||
score_sigma: 1.0,
|
||||
convergence: ConvergenceOptions::default(),
|
||||
observer: NullObserver,
|
||||
unknown_keys: crate::UnknownKeys::default(),
|
||||
_time: PhantomData,
|
||||
_key: PhantomData,
|
||||
}
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2545,7 +2621,7 @@ mod tests {
|
||||
fn per_slice_footprint_is_independent_of_index_magnitude() {
|
||||
fn total_skill_slots(high_indices: bool) -> usize {
|
||||
let mut h: History<i64, ConstantDrift, NullObserver, String> =
|
||||
History::builder_with_key().build();
|
||||
History::builder().key_type::<String>().build();
|
||||
|
||||
for i in 0..2_000 {
|
||||
h.intern(&format!("k{i:05}"));
|
||||
|
||||
Reference in New Issue
Block a user