1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! In-process L2 order book state.
//!
//! This crate keeps limit order books in memory and applies exchange feeds to them:
//! full snapshots, incremental deltas, and the sequence checks needed to notice when
//! a feed has dropped a message.
//!
//! # Design
//!
//! - **Fixed-capacity sides.** Each side of a book is a sorted [`Level`] array with the
//! best level at the end, so applying an update is a short scan plus a small memmove
//! with no allocation.
//! - **Integer prices.** Prices and quantities are [`Scale9`] fixed-point values rather
//! than `f64` or bare `i64`, so arithmetic is exact and an unscaled number cannot be
//! passed where a scaled one belongs.
//! - **Sequence tracking.** [`BookStore::apply_delta`] rejects an update that skips a
//! sequence number instead of silently corrupting the book.
//!
//! # Example
//!
//! ```
//! use depthbook::{f64_to_scale9, BookStore, Level};
//!
//! let store = BookStore::new();
//!
//! store.apply_snapshot(
//! "binance",
//! "BTC-USDT",
//! &[Level::new(f64_to_scale9(50_000.0), f64_to_scale9(1.0))],
//! &[Level::new(f64_to_scale9(50_001.0), f64_to_scale9(2.0))],
//! 1,
//! 1_700_000_000_000_000_000,
//! )?;
//!
//! let (bid, ask) = store.bbo("binance", "BTC-USDT").unwrap();
//! assert_eq!(ask.price - bid.price, f64_to_scale9(1.0));
//!
//! // A delta that skips sequence 2 is refused rather than applied.
//! let gap = store.apply_delta("binance", "BTC-USDT", &[], &[], 7, 1_700_000_000_000_000_001);
//! assert!(gap.is_err());
//! assert_eq!(store.stats().sequence_gaps, 1);
//! # Ok::<(), depthbook::Error>(())
//! ```
//!
//! # Scope
//!
//! This is a data structure, not a feed handler. It does no I/O and speaks no exchange
//! protocol; feeding it is the caller's job. State lives in the process that owns the
//! [`BookStore`] and is not shared between processes or persisted.
pub use ;
pub use ;
pub use InternedString;
pub use ;
pub use ;