refactor(aura-core): split Scalar into a tag-free Cell + ScalarKind
Motivation
----------
`Scalar` was a tagged enum (I64/F64/Bool/Ts), so every scalar value
physically carried its own kind tag. But the kind is already known from
the schema/port/column the value flows through (C7: the type is a
property of the column, not of the value — the hot path is already
columnar `Column<T>`, and `AnyColumn::get` *reconstructs* the tag from
the column on the way out). The per-value tag was therefore redundant
with the kind the surrounding context already holds.
That redundancy had three costs:
* It baked an implicit `match` (a branch) into every function that read
a Scalar payload — even where the caller statically knew the type.
The tag could never be exploited away.
* Size: a tagged enum is tag + payload = 16 bytes (f64/i64 alignment),
twice the 8 bytes the value needs. A `Column<Scalar>` would be double
the memory and half the cache utilisation.
* It is the shared root of several downstream papercuts we keep hitting
— the lossy f64 manifest field, the `unreachable!` panic on a
non-numeric param, the serde-tag question — all symptoms of "the type
is baked into the value".
Change
------
Introduce `Cell`: a type-erased 64-bit word (`struct Cell(u64)`) that is
not readable without external type context. It is constructed per base
type (`from_i64/from_f64/from_bool/from_ts`) and read only by naming the
type at the call site (`i64()/f64()/bool()/ts()`) — each a branch-free
bit-cast. The hot path resolves the kind once at the boundary (from the
schema) and then reads natively, with no per-value branch. `Cell` knows
nothing of `Scalar` or `ScalarKind`; the dependency is strictly one-way,
and it lives in its own `cell.rs` (more is planned on top of it).
`Scalar` becomes `struct { kind: ScalarKind, cell: Cell }` — the
self-describing form for the dynamic boundaries (builder binding,
serialization, rendering), built on top of `Cell`. Its `as_*` accessors
now `debug_assert` the kind and return the native value (free in
release); calling the wrong accessor is a caller bug, not a checked
`Option`. The variant constructors `Scalar::I64(..)` become associated
fns `Scalar::i64(..)`.
`PartialEq` is hand-written (not derived) to preserve the former enum's
value semantics: kinds must match, then native payloads compare, so f64
keeps IEEE-754 behaviour (`NaN != NaN`, `+0.0 == -0.0`) and a kind
mismatch is never equal even when the raw words coincide. A fixture
(`scalar_eq_is_value_not_bitwise`) pins exactly the cases where bit- and
value-equality diverge, so it can't silently regress. `Cell`'s own
`Eq`/`Hash` stay bitwise — correct for a raw word.
The change is behaviour-preserving: Scalar's observable behaviour is
identical to the pre-Cell enum (the value-equality fixture proves it);
only the internal representation changed. The ~440 call sites across the
workspace are a mechanical constructor rename plus ~12 destructuring
sites (match-arms / `let`-patterns) rewritten to `kind()` + `as_*`.
Verified: cargo build --workspace --all-targets, cargo clippy --workspace
--all-targets -- -D warnings, cargo test --workspace — all green.
This commit is contained in:
+22
-22
@@ -60,34 +60,34 @@ impl AnyColumn {
|
||||
/// kind; the column is left untouched (C7 guard). The hot path bypasses this
|
||||
/// by taking the concrete `Column<T>` once via `as_*_mut`.
|
||||
pub fn push(&mut self, v: Scalar) -> Result<(), KindMismatch> {
|
||||
match (self, v) {
|
||||
(AnyColumn::I64(c), Scalar::I64(x)) => {
|
||||
c.push(x);
|
||||
match (self, v.kind()) {
|
||||
(AnyColumn::I64(c), ScalarKind::I64) => {
|
||||
c.push(v.as_i64());
|
||||
Ok(())
|
||||
}
|
||||
(AnyColumn::F64(c), Scalar::F64(x)) => {
|
||||
c.push(x);
|
||||
(AnyColumn::F64(c), ScalarKind::F64) => {
|
||||
c.push(v.as_f64());
|
||||
Ok(())
|
||||
}
|
||||
(AnyColumn::Bool(c), Scalar::Bool(x)) => {
|
||||
c.push(x);
|
||||
(AnyColumn::Bool(c), ScalarKind::Bool) => {
|
||||
c.push(v.as_bool());
|
||||
Ok(())
|
||||
}
|
||||
(AnyColumn::Ts(c), Scalar::Ts(x)) => {
|
||||
c.push(x);
|
||||
(AnyColumn::Ts(c), ScalarKind::Timestamp) => {
|
||||
c.push(v.as_ts());
|
||||
Ok(())
|
||||
}
|
||||
(this, v) => Err(KindMismatch { expected: this.kind(), got: v.kind() }),
|
||||
(this, got) => Err(KindMismatch { expected: this.kind(), got }),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read one type-erased value (0 = newest). `None` if cold / out of range.
|
||||
pub fn get(&self, k: usize) -> Option<Scalar> {
|
||||
match self {
|
||||
AnyColumn::I64(c) => c.get(k).map(Scalar::I64),
|
||||
AnyColumn::F64(c) => c.get(k).map(Scalar::F64),
|
||||
AnyColumn::Bool(c) => c.get(k).map(Scalar::Bool),
|
||||
AnyColumn::Ts(c) => c.get(k).map(Scalar::Ts),
|
||||
AnyColumn::I64(c) => c.get(k).map(Scalar::i64),
|
||||
AnyColumn::F64(c) => c.get(k).map(Scalar::f64),
|
||||
AnyColumn::Bool(c) => c.get(k).map(Scalar::bool),
|
||||
AnyColumn::Ts(c) => c.get(k).map(Scalar::ts),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,20 +160,20 @@ mod tests {
|
||||
#[test]
|
||||
fn same_kind_push_round_trips() {
|
||||
let mut e = AnyColumn::with_capacity(ScalarKind::F64, 4);
|
||||
e.push(Scalar::F64(1.5)).unwrap();
|
||||
e.push(Scalar::F64(2.5)).unwrap();
|
||||
e.push(Scalar::f64(1.5)).unwrap();
|
||||
e.push(Scalar::f64(2.5)).unwrap();
|
||||
assert_eq!(e.kind(), ScalarKind::F64);
|
||||
assert_eq!(e.len(), 2);
|
||||
assert_eq!(e.run_count(), 2);
|
||||
assert_eq!(e.get(0), Some(Scalar::F64(2.5))); // newest
|
||||
assert_eq!(e.get(1), Some(Scalar::F64(1.5)));
|
||||
assert_eq!(e.get(0), Some(Scalar::f64(2.5))); // newest
|
||||
assert_eq!(e.get(1), Some(Scalar::f64(1.5)));
|
||||
assert_eq!(e.get(2), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wrong_kind_push_is_rejected() {
|
||||
let mut edge = AnyColumn::with_capacity(ScalarKind::I64, 8);
|
||||
let err = edge.push(Scalar::F64(1.5)).unwrap_err(); // f64 into an i64 edge
|
||||
let err = edge.push(Scalar::f64(1.5)).unwrap_err(); // f64 into an i64 edge
|
||||
assert_eq!(err, KindMismatch { expected: ScalarKind::I64, got: ScalarKind::F64 });
|
||||
assert_eq!(edge.len(), 0); // nothing was stored
|
||||
assert_eq!(edge.run_count(), 0); // and the counter did not move
|
||||
@@ -188,7 +188,7 @@ mod tests {
|
||||
assert!(e.as_ts_mut().is_none());
|
||||
// hot-path push through the concrete column, monomorphic:
|
||||
e.as_f64_mut().unwrap().push(3.0);
|
||||
assert_eq!(e.get(0), Some(Scalar::F64(3.0)));
|
||||
assert_eq!(e.get(0), Some(Scalar::f64(3.0)));
|
||||
assert_eq!(e.run_count(), 1);
|
||||
}
|
||||
|
||||
@@ -208,7 +208,7 @@ mod tests {
|
||||
#[test]
|
||||
fn timestamp_edge_round_trips() {
|
||||
let mut e = AnyColumn::with_capacity(ScalarKind::Timestamp, 2);
|
||||
e.push(Scalar::Ts(Timestamp(1_700_000_000_000_000_000))).unwrap();
|
||||
assert_eq!(e.get(0), Some(Scalar::Ts(Timestamp(1_700_000_000_000_000_000))));
|
||||
e.push(Scalar::ts(Timestamp(1_700_000_000_000_000_000))).unwrap();
|
||||
assert_eq!(e.get(0), Some(Scalar::ts(Timestamp(1_700_000_000_000_000_000))));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
//! `Cell` — a type-erased 64-bit word holding one scalar value with its kind
|
||||
//! stripped away (C7). The type lives in the schema / column / port, never in
|
||||
//! the value, so a cell is read back only by naming its type via a typed
|
||||
//! accessor. `Scalar` is built on top of this; `Cell` itself knows nothing of
|
||||
//! `Scalar` or `ScalarKind`.
|
||||
|
||||
use crate::scalar::Timestamp;
|
||||
|
||||
/// A type-erased 64-bit cell: the raw storage of one scalar value with its kind
|
||||
/// stripped away. The bits are meaningless on their own — the type lives in the
|
||||
/// schema/column/port, not in the value (C7), so the word is recovered **only**
|
||||
/// by naming the type at the call site via a typed accessor ([`Cell::i64`],
|
||||
/// [`Cell::f64`], [`Cell::bool`], [`Cell::ts`]). Each reinterprets the word with
|
||||
/// no tag to check and therefore no branch — the caller's choice of accessor
|
||||
/// *is* the type context the hot path already holds. The inner word is private;
|
||||
/// there is no kind-free way to read it.
|
||||
///
|
||||
/// All four base types fit one 64-bit word: `i64`/`Timestamp`/`bool` reuse the
|
||||
/// same integer word, `f64` via its IEEE-754 bit pattern. `Eq`/`Hash` are
|
||||
/// bit-exact (so `+0.0`/`-0.0` differ and a `NaN` bit pattern equals itself) —
|
||||
/// the semantics of a raw word, not of a number.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct Cell(u64);
|
||||
|
||||
impl Cell {
|
||||
/// Store an `i64` (identity bit-cast).
|
||||
pub fn from_i64(v: i64) -> Self {
|
||||
Cell(v as u64)
|
||||
}
|
||||
/// Store an `f64` as its IEEE-754 bit pattern.
|
||||
pub fn from_f64(v: f64) -> Self {
|
||||
Cell(v.to_bits())
|
||||
}
|
||||
/// Store a `bool` as `0`/`1`.
|
||||
pub fn from_bool(v: bool) -> Self {
|
||||
Cell(v as u64)
|
||||
}
|
||||
/// Store a [`Timestamp`] (its `i64` epoch-ns, bit-cast).
|
||||
pub fn from_ts(v: Timestamp) -> Self {
|
||||
Cell(v.0 as u64)
|
||||
}
|
||||
|
||||
/// Read the word as `i64` (identity bit-cast). The caller asserts the type
|
||||
/// by choosing this accessor; there is no tag to check, hence no branch.
|
||||
pub fn i64(self) -> i64 {
|
||||
self.0 as i64
|
||||
}
|
||||
/// Read the word as `f64` from its IEEE-754 bit pattern. Branch-free.
|
||||
pub fn f64(self) -> f64 {
|
||||
f64::from_bits(self.0)
|
||||
}
|
||||
/// Read the word as `bool` (any non-zero word is `true`). Branch-free.
|
||||
pub fn bool(self) -> bool {
|
||||
self.0 != 0
|
||||
}
|
||||
/// Read the word as a [`Timestamp`] (identity bit-cast). Branch-free.
|
||||
pub fn ts(self) -> Timestamp {
|
||||
Timestamp(self.0 as i64)
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,7 @@ mod tests {
|
||||
fn ctx_hands_financial_indexed_windows() {
|
||||
let mut inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, 4)];
|
||||
for v in [10.0_f64, 20.0, 30.0] {
|
||||
inputs[0].push(Scalar::F64(v)).unwrap();
|
||||
inputs[0].push(Scalar::f64(v)).unwrap();
|
||||
}
|
||||
let ctx = Ctx::new(&inputs, Timestamp(0));
|
||||
let w = ctx.f64_in(0);
|
||||
@@ -91,8 +91,8 @@ mod tests {
|
||||
AnyColumn::with_capacity(ScalarKind::F64, 2),
|
||||
AnyColumn::with_capacity(ScalarKind::I64, 2),
|
||||
];
|
||||
inputs[0].push(Scalar::F64(1.5)).unwrap();
|
||||
inputs[1].push(Scalar::I64(42)).unwrap();
|
||||
inputs[0].push(Scalar::f64(1.5)).unwrap();
|
||||
inputs[1].push(Scalar::i64(42)).unwrap();
|
||||
let ctx = Ctx::new(&inputs, Timestamp(0));
|
||||
assert_eq!(ctx.f64_in(0)[0], 1.5);
|
||||
assert_eq!(ctx.i64_in(1)[0], 42);
|
||||
@@ -102,7 +102,7 @@ mod tests {
|
||||
#[should_panic(expected = "engine bug")]
|
||||
fn ctx_panics_on_kind_mismatch() {
|
||||
let mut inputs = vec![AnyColumn::with_capacity(ScalarKind::I64, 2)];
|
||||
inputs[0].push(Scalar::I64(7)).unwrap();
|
||||
inputs[0].push(Scalar::i64(7)).unwrap();
|
||||
let ctx = Ctx::new(&inputs, Timestamp(0));
|
||||
let _ = ctx.f64_in(0); // wrong kind → panic
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
//! (C18/C22).
|
||||
|
||||
mod any;
|
||||
mod cell;
|
||||
mod column;
|
||||
mod ctx;
|
||||
mod error;
|
||||
@@ -36,6 +37,7 @@ mod node;
|
||||
mod scalar;
|
||||
|
||||
pub use any::AnyColumn;
|
||||
pub use cell::Cell;
|
||||
pub use column::{Column, Window};
|
||||
pub use ctx::Ctx;
|
||||
pub use error::KindMismatch;
|
||||
|
||||
@@ -435,45 +435,45 @@ mod tests {
|
||||
#[test]
|
||||
fn bind_removes_slot_and_reconstructs_positionally() {
|
||||
// chained bind of b then a (reverse slot order); c stays open
|
||||
let bound = probe3().bind("b", Scalar::I64(8)).bind("a", Scalar::I64(7));
|
||||
let bound = probe3().bind("b", Scalar::i64(8)).bind("a", Scalar::i64(7));
|
||||
assert_eq!(
|
||||
bound.params().iter().map(|p| p.name.as_str()).collect::<Vec<_>>(),
|
||||
["c"], // only c remains open
|
||||
);
|
||||
let built = bound.build(&[Scalar::F64(9.0)]); // inject c
|
||||
let built = bound.build(&[Scalar::f64(9.0)]); // inject c
|
||||
assert_eq!(
|
||||
built.label(),
|
||||
format!("{:?}", vec![Scalar::I64(7), Scalar::I64(8), Scalar::F64(9.0)]),
|
||||
format!("{:?}", vec![Scalar::i64(7), Scalar::i64(8), Scalar::f64(9.0)]),
|
||||
);
|
||||
|
||||
// partial: bind only b; a and c stay open and keep their positions
|
||||
let built2 = probe3()
|
||||
.bind("b", Scalar::I64(8))
|
||||
.build(&[Scalar::I64(7), Scalar::F64(9.0)]); // a, c injected in order
|
||||
.bind("b", Scalar::i64(8))
|
||||
.build(&[Scalar::i64(7), Scalar::f64(9.0)]); // a, c injected in order
|
||||
assert_eq!(
|
||||
built2.label(),
|
||||
format!("{:?}", vec![Scalar::I64(7), Scalar::I64(8), Scalar::F64(9.0)]),
|
||||
format!("{:?}", vec![Scalar::i64(7), Scalar::i64(8), Scalar::f64(9.0)]),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bind_records_bound_param_at_original_position() {
|
||||
// middle-of-three: binding `b` records its ORIGINAL slot position (1).
|
||||
let mid = probe3().bind("b", Scalar::I64(8));
|
||||
let mid = probe3().bind("b", Scalar::i64(8));
|
||||
assert_eq!(
|
||||
mid.bound_params(),
|
||||
[BoundParam { pos: 1, name: "b".into(), kind: ScalarKind::I64, value: Scalar::I64(8) }]
|
||||
[BoundParam { pos: 1, name: "b".into(), kind: ScalarKind::I64, value: Scalar::i64(8) }]
|
||||
.as_slice(),
|
||||
);
|
||||
|
||||
// chained reverse-order bind (b then a): each entry carries its ORIGINAL
|
||||
// position regardless of the shrinking array — positions {1, 0} in call order.
|
||||
let pair = probe3().bind("b", Scalar::I64(8)).bind("a", Scalar::I64(7));
|
||||
let pair = probe3().bind("b", Scalar::i64(8)).bind("a", Scalar::i64(7));
|
||||
assert_eq!(
|
||||
pair.bound_params(),
|
||||
[
|
||||
BoundParam { pos: 1, name: "b".into(), kind: ScalarKind::I64, value: Scalar::I64(8) },
|
||||
BoundParam { pos: 0, name: "a".into(), kind: ScalarKind::I64, value: Scalar::I64(7) },
|
||||
BoundParam { pos: 1, name: "b".into(), kind: ScalarKind::I64, value: Scalar::i64(8) },
|
||||
BoundParam { pos: 0, name: "a".into(), kind: ScalarKind::I64, value: Scalar::i64(7) },
|
||||
]
|
||||
.as_slice(),
|
||||
);
|
||||
@@ -494,7 +494,7 @@ mod tests {
|
||||
},
|
||||
|_| Box::new(Bare),
|
||||
);
|
||||
let _ = b.bind("width", Scalar::I64(2)); // "width" is not a declared param
|
||||
let _ = b.bind("width", Scalar::i64(2)); // "width" is not a declared param
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -512,7 +512,7 @@ mod tests {
|
||||
},
|
||||
|_| Box::new(Bare),
|
||||
);
|
||||
let _ = b.bind("dup", Scalar::I64(1)); // two slots named "dup"
|
||||
let _ = b.bind("dup", Scalar::i64(1)); // two slots named "dup"
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -527,7 +527,7 @@ mod tests {
|
||||
},
|
||||
|_| Box::new(Bare),
|
||||
);
|
||||
let _ = b.bind("length", Scalar::F64(2.0)); // F64 value for an I64 slot
|
||||
let _ = b.bind("length", Scalar::f64(2.0)); // F64 value for an I64 slot
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,12 +542,12 @@ mod zip_params_tests {
|
||||
ParamSpec { name: "fast".into(), kind: ScalarKind::I64 },
|
||||
ParamSpec { name: "scale".into(), kind: ScalarKind::F64 },
|
||||
];
|
||||
let point = vec![Scalar::I64(2), Scalar::F64(0.5)];
|
||||
let point = vec![Scalar::i64(2), Scalar::f64(0.5)];
|
||||
assert_eq!(
|
||||
zip_params(&space, &point),
|
||||
vec![
|
||||
("fast".to_string(), Scalar::I64(2)),
|
||||
("scale".to_string(), Scalar::F64(0.5)),
|
||||
("fast".to_string(), Scalar::i64(2)),
|
||||
("scale".to_string(), Scalar::f64(0.5)),
|
||||
],
|
||||
);
|
||||
}
|
||||
@@ -558,7 +558,7 @@ mod zip_params_tests {
|
||||
ParamSpec { name: "a".into(), kind: ScalarKind::I64 },
|
||||
ParamSpec { name: "b".into(), kind: ScalarKind::I64 },
|
||||
];
|
||||
let point = vec![Scalar::I64(7), Scalar::I64(9)];
|
||||
let point = vec![Scalar::i64(7), Scalar::i64(9)];
|
||||
let hand: Vec<(String, Scalar)> =
|
||||
space.iter().zip(&point).map(|(ps, v)| (ps.name.clone(), *v)).collect();
|
||||
assert_eq!(zip_params(&space, &point), hand);
|
||||
|
||||
+127
-44
@@ -1,6 +1,10 @@
|
||||
//! The closed four-type scalar set streamed on aura's hot path (C7):
|
||||
//! `i64`, `f64`, `bool`, and `Timestamp` (epoch-ns UTC). The carrier `Scalar`
|
||||
//! is a `Copy` POD enum — no type-erased payloads, no heap, no reference counting.
|
||||
//! The closed four-type scalar set streamed on aura's hot path (C7): `i64`,
|
||||
//! `f64`, `bool`, and `Timestamp` (epoch-ns UTC). The raw value is a type-erased
|
||||
//! [`Cell`] — one 64-bit word, no tag; a [`Scalar`] pairs a `Cell` with its
|
||||
//! [`ScalarKind`] for the dynamic boundaries. Both are `Copy` PODs — no heap, no
|
||||
//! reference counting.
|
||||
|
||||
use crate::cell::Cell;
|
||||
|
||||
/// Canonical engine time: epoch-nanoseconds UTC. Newtype over `i64` (C7).
|
||||
/// `Default` (epoch 0) lets `Column<Timestamp>` pre-size its ring.
|
||||
@@ -16,53 +20,110 @@ pub enum ScalarKind {
|
||||
Timestamp,
|
||||
}
|
||||
|
||||
/// The four scalar base types, type-erased into one `Copy` carrier (C7).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub enum Scalar {
|
||||
I64(i64),
|
||||
F64(f64),
|
||||
Bool(bool),
|
||||
Ts(Timestamp),
|
||||
/// One scalar value: a type-erased [`Cell`] plus its [`ScalarKind`] tag — the
|
||||
/// self-describing form for the dynamic boundaries (builder binding,
|
||||
/// serialization, rendering). The hot path uses the bare [`Cell`]; a `Scalar`
|
||||
/// is the fatter form that remembers its own type.
|
||||
///
|
||||
/// Reading is the caller's responsibility. Each `as_*` accessor `debug_assert`s
|
||||
/// that the stored kind matches, then hands back the **native** value — in
|
||||
/// release the assert is gone, so an accessor *is* the bare cell read
|
||||
/// (branch-free). Calling the wrong `as_*` for the stored kind is a caller bug
|
||||
/// (debug-asserted), not a recoverable error.
|
||||
///
|
||||
/// `PartialEq` is **value** equality (hand-written, not derived — the derived
|
||||
/// one would inherit the `Cell`'s bitwise compare). Two scalars are equal iff
|
||||
/// their kinds match and their native payloads compare equal, so `f64` keeps
|
||||
/// IEEE-754 semantics (`NaN != NaN`, `+0.0 == -0.0`) exactly as the former enum
|
||||
/// did, and a kind mismatch is never equal even when the words coincide
|
||||
/// (`i64(0) != f64(0.0)`).
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Scalar {
|
||||
kind: ScalarKind,
|
||||
cell: Cell,
|
||||
}
|
||||
|
||||
impl Scalar {
|
||||
/// The kind tag of this scalar.
|
||||
/// Construct an `i64` scalar.
|
||||
pub fn i64(v: i64) -> Self {
|
||||
Scalar { kind: ScalarKind::I64, cell: Cell::from_i64(v) }
|
||||
}
|
||||
/// Construct an `f64` scalar.
|
||||
pub fn f64(v: f64) -> Self {
|
||||
Scalar { kind: ScalarKind::F64, cell: Cell::from_f64(v) }
|
||||
}
|
||||
/// Construct a `bool` scalar.
|
||||
pub fn bool(v: bool) -> Self {
|
||||
Scalar { kind: ScalarKind::Bool, cell: Cell::from_bool(v) }
|
||||
}
|
||||
/// Construct a [`Timestamp`] scalar.
|
||||
pub fn ts(v: Timestamp) -> Self {
|
||||
Scalar { kind: ScalarKind::Timestamp, cell: Cell::from_ts(v) }
|
||||
}
|
||||
|
||||
/// The kind tag of this scalar (a field read, no branch).
|
||||
pub fn kind(self) -> ScalarKind {
|
||||
match self {
|
||||
Scalar::I64(_) => ScalarKind::I64,
|
||||
Scalar::F64(_) => ScalarKind::F64,
|
||||
Scalar::Bool(_) => ScalarKind::Bool,
|
||||
Scalar::Ts(_) => ScalarKind::Timestamp,
|
||||
self.kind
|
||||
}
|
||||
|
||||
/// Read as `i64`. The caller asserts the kind by choosing this accessor; a
|
||||
/// mismatch is a caller bug (debug-asserted, free in release).
|
||||
pub fn as_i64(self) -> i64 {
|
||||
debug_assert_eq!(self.kind, ScalarKind::I64);
|
||||
self.cell.i64()
|
||||
}
|
||||
/// Read as `f64`. Caller asserts the kind (debug-asserted, free in release).
|
||||
pub fn as_f64(self) -> f64 {
|
||||
debug_assert_eq!(self.kind, ScalarKind::F64);
|
||||
self.cell.f64()
|
||||
}
|
||||
/// Read as `bool`. Caller asserts the kind (debug-asserted, free in release).
|
||||
pub fn as_bool(self) -> bool {
|
||||
debug_assert_eq!(self.kind, ScalarKind::Bool);
|
||||
self.cell.bool()
|
||||
}
|
||||
/// Read as [`Timestamp`]. Caller asserts the kind (debug-asserted, free in release).
|
||||
pub fn as_ts(self) -> Timestamp {
|
||||
debug_assert_eq!(self.kind, ScalarKind::Timestamp);
|
||||
self.cell.ts()
|
||||
}
|
||||
}
|
||||
|
||||
/// Value equality, not the `Cell`'s bitwise one: kinds must match, then the
|
||||
/// native payloads compare — so `f64` keeps IEEE-754 semantics (`NaN != NaN`,
|
||||
/// `+0.0 == -0.0`), matching the pre-`Cell` enum's behaviour.
|
||||
impl PartialEq for Scalar {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
if self.kind != other.kind {
|
||||
return false;
|
||||
}
|
||||
match self.kind {
|
||||
ScalarKind::I64 => self.as_i64() == other.as_i64(),
|
||||
ScalarKind::F64 => self.as_f64() == other.as_f64(),
|
||||
ScalarKind::Bool => self.as_bool() == other.as_bool(),
|
||||
ScalarKind::Timestamp => self.as_ts() == other.as_ts(),
|
||||
}
|
||||
}
|
||||
/// The `i64` payload, or `None` if this scalar is not an `I64`.
|
||||
pub fn as_i64(self) -> Option<i64> {
|
||||
if let Scalar::I64(v) = self { Some(v) } else { None }
|
||||
}
|
||||
/// The `f64` payload, or `None` if this scalar is not an `F64`.
|
||||
pub fn as_f64(self) -> Option<f64> {
|
||||
if let Scalar::F64(v) = self { Some(v) } else { None }
|
||||
}
|
||||
}
|
||||
|
||||
impl From<i64> for Scalar {
|
||||
fn from(v: i64) -> Self {
|
||||
Scalar::I64(v)
|
||||
Scalar::i64(v)
|
||||
}
|
||||
}
|
||||
impl From<f64> for Scalar {
|
||||
fn from(v: f64) -> Self {
|
||||
Scalar::F64(v)
|
||||
Scalar::f64(v)
|
||||
}
|
||||
}
|
||||
impl From<bool> for Scalar {
|
||||
fn from(v: bool) -> Self {
|
||||
Scalar::Bool(v)
|
||||
Scalar::bool(v)
|
||||
}
|
||||
}
|
||||
impl From<Timestamp> for Scalar {
|
||||
fn from(v: Timestamp) -> Self {
|
||||
Scalar::Ts(v)
|
||||
Scalar::ts(v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,18 +133,18 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn kind_matches_variant() {
|
||||
assert_eq!(Scalar::I64(1).kind(), ScalarKind::I64);
|
||||
assert_eq!(Scalar::F64(1.0).kind(), ScalarKind::F64);
|
||||
assert_eq!(Scalar::Bool(true).kind(), ScalarKind::Bool);
|
||||
assert_eq!(Scalar::Ts(Timestamp(1)).kind(), ScalarKind::Timestamp);
|
||||
assert_eq!(Scalar::i64(1).kind(), ScalarKind::I64);
|
||||
assert_eq!(Scalar::f64(1.0).kind(), ScalarKind::F64);
|
||||
assert_eq!(Scalar::bool(true).kind(), ScalarKind::Bool);
|
||||
assert_eq!(Scalar::ts(Timestamp(1)).kind(), ScalarKind::Timestamp);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn from_widenings() {
|
||||
assert_eq!(Scalar::from(7i64), Scalar::I64(7));
|
||||
assert_eq!(Scalar::from(2.5f64), Scalar::F64(2.5));
|
||||
assert_eq!(Scalar::from(true), Scalar::Bool(true));
|
||||
assert_eq!(Scalar::from(Timestamp(9)), Scalar::Ts(Timestamp(9)));
|
||||
assert_eq!(Scalar::from(7i64), Scalar::i64(7));
|
||||
assert_eq!(Scalar::from(2.5f64), Scalar::f64(2.5));
|
||||
assert_eq!(Scalar::from(true), Scalar::bool(true));
|
||||
assert_eq!(Scalar::from(Timestamp(9)), Scalar::ts(Timestamp(9)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -97,8 +158,8 @@ mod tests {
|
||||
fn lower(v: impl Into<Scalar>) -> Scalar {
|
||||
v.into()
|
||||
}
|
||||
assert_eq!(lower(2), Scalar::I64(2));
|
||||
assert_eq!(lower(0.5), Scalar::F64(0.5));
|
||||
assert_eq!(lower(2), Scalar::i64(2));
|
||||
assert_eq!(lower(0.5), Scalar::f64(0.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -108,16 +169,38 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_value_accessors_are_kind_exact() {
|
||||
assert_eq!(Scalar::I64(3).as_i64(), Some(3));
|
||||
assert_eq!(Scalar::I64(3).as_f64(), None);
|
||||
assert_eq!(Scalar::F64(0.5).as_f64(), Some(0.5));
|
||||
assert_eq!(Scalar::F64(0.5).as_i64(), None);
|
||||
fn scalar_value_accessors_return_native_payload() {
|
||||
assert_eq!(Scalar::i64(3).as_i64(), 3);
|
||||
assert_eq!(Scalar::f64(0.5).as_f64(), 0.5);
|
||||
assert!(Scalar::bool(true).as_bool());
|
||||
assert_eq!(Scalar::ts(Timestamp(7)).as_ts(), Timestamp(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_eq_is_value_not_bitwise() {
|
||||
// f64 follows IEEE-754 exactly as the former enum did: a NaN is never
|
||||
// equal to itself, and +0.0 equals -0.0 — value equality, NOT the
|
||||
// bitwise equality the underlying `Cell` carries (those two cases are
|
||||
// precisely where bit- and value-equality diverge).
|
||||
assert_ne!(Scalar::f64(f64::NAN), Scalar::f64(f64::NAN));
|
||||
assert_eq!(Scalar::f64(0.0), Scalar::f64(-0.0));
|
||||
assert_eq!(Scalar::f64(2.5), Scalar::f64(2.5));
|
||||
// i64 / bool / timestamp compare by native value.
|
||||
assert_eq!(Scalar::i64(7), Scalar::i64(7));
|
||||
assert_ne!(Scalar::i64(7), Scalar::i64(8));
|
||||
assert_eq!(Scalar::bool(true), Scalar::bool(true));
|
||||
assert_ne!(Scalar::bool(true), Scalar::bool(false));
|
||||
assert_eq!(Scalar::ts(Timestamp(9)), Scalar::ts(Timestamp(9)));
|
||||
// A kind mismatch is never equal — even when the words coincide:
|
||||
// i64(0) and f64(0.0) share the bit pattern 0; bool(true) and i64(1)
|
||||
// share 1. Value equality still separates them by kind.
|
||||
assert_ne!(Scalar::i64(0), Scalar::f64(0.0));
|
||||
assert_ne!(Scalar::i64(1), Scalar::bool(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scalar_is_copy() {
|
||||
let s = Scalar::F64(1.0);
|
||||
let s = Scalar::f64(1.0);
|
||||
let a = s;
|
||||
let b = s; // Copy: `s` still usable after move-by-value
|
||||
assert_eq!(a, b);
|
||||
|
||||
Reference in New Issue
Block a user