libitofin 0.7.0

A ground-up Rust port of QuantLib: quantitative-finance primitives for pricing, risk, and numerical methods.
Documentation
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! Observer/observable pattern.
//!
//! Port of the single-threaded branch of `ql/patterns/observable.{hpp,cpp}`
//! (design decision D1). The thread-safety machinery (`ObservableSettings`,
//! deferred updates, recursive mutexes, the `Proxy` indirection) is skipped
//! per the Rc-first port; only the observer registry and notification are kept.
//!
//! Observers are held by [`WeakMut`] back-references so the observer ⇄ observable
//! graph cannot form an `Rc` cycle. [`Observable::notify_observers`] snapshots the
//! live observers and drops every borrow before calling `update`, so an observer
//! may freely register, unregister or mutate the graph while being notified.
//!
//! A notification that cannot be delivered immediately - the observer is
//! already mid-`update` when a re-entrant notification reaches it, through any
//! observable - is queued on a thread-local pending list and re-delivered by
//! the outermost notification on the thread once the borrow releases, so no
//! notification is dropped; see [`Observable::notify_observers`].

use std::cell::{Cell, RefCell};

use crate::shared::{Shared, SharedMut, WeakMut, shared, shared_mut};

thread_local! {
    /// Nesting depth of [`Observable::notify_observers`] across all
    /// observables on this thread; the invocation returning to depth zero
    /// drains [`PENDING`].
    static DEPTH: Cell<usize> = const { Cell::new(0) };
    /// Re-entrancy latch so a drained observer re-notifying does not start a
    /// nested drain; the running drain loop picks its deferrals up instead.
    static DRAINING: Cell<bool> = const { Cell::new(false) };
    /// Observers a notification round could not borrow, shared by all
    /// observables so a miss is re-delivered no matter which observable's
    /// round it happened in.
    static PENDING: RefCell<Vec<WeakMut<dyn Observer>>> = const { RefCell::new(Vec::new()) };
}

struct ObserverEntry {
    observer: WeakMut<dyn Observer>,
    defer_reentrant: bool,
}

impl ObserverEntry {
    fn new(observer: &SharedMut<dyn Observer>) -> Self {
        let defer_reentrant = observer
            .try_borrow()
            .map_or(true, |observer| observer.defer_reentrant_update());
        ObserverEntry {
            observer: SharedMut::downgrade(observer),
            defer_reentrant,
        }
    }
}

/// RAII depth counter for [`DEPTH`], panic-safe like `LazyObject`'s guard.
struct DepthGuard;

impl DepthGuard {
    fn enter() -> Self {
        DEPTH.with(|depth| depth.set(depth.get() + 1));
        DepthGuard
    }
}

impl Drop for DepthGuard {
    fn drop(&mut self) {
        DEPTH.with(|depth| depth.set(depth.get() - 1));
    }
}

/// RAII latch for [`DRAINING`]; `try_enter` yields `None` when already set.
struct DrainGuard;

impl DrainGuard {
    fn try_enter() -> Option<Self> {
        if DRAINING.with(Cell::get) {
            return None;
        }
        DRAINING.with(|flag| flag.set(true));
        Some(DrainGuard)
    }
}

impl Drop for DrainGuard {
    fn drop(&mut self) {
        DRAINING.with(|flag| flag.set(false));
    }
}

/// Delivers one notification with the same re-entrancy discipline as
/// [`Observable::notify_observers`]: a busy observer is queued for the
/// outermost round instead of panicking on the live borrow. For observers
/// that forward to another observer directly, outside any observable's
/// registry.
pub(crate) fn deliver(observer: &SharedMut<dyn Observer>) {
    match observer.try_borrow_mut() {
        Ok(mut delivered) => delivered.update(),
        Err(_) => defer(observer),
    }
}

/// Queues an undeliverable observer for the outermost round, once.
fn defer(observer: &SharedMut<dyn Observer>) {
    let weak = SharedMut::downgrade(observer);
    PENDING.with(|pending| {
        let mut pending = pending.borrow_mut();
        if !pending.iter().any(|queued| queued.ptr_eq(&weak)) {
            pending.push(weak);
        }
    });
}

/// Delivers every queued miss; runs only at depth zero and never nests.
///
/// A drained `update` may notify further observables; their fresh misses land
/// in [`PENDING`] and are picked up by the next pass of the loop. An observer
/// still borrowed here is held by code outside any notification; it stays
/// queued for the next outermost notification rather than spinning.
fn drain_pending() {
    let Some(_draining) = DrainGuard::try_enter() else {
        return;
    };
    let mut stalled: Vec<WeakMut<dyn Observer>> = Vec::new();
    loop {
        let batch: Vec<WeakMut<dyn Observer>> =
            PENDING.with(|pending| pending.borrow_mut().drain(..).collect());
        if batch.is_empty() {
            break;
        }
        for weak in batch {
            let Some(observer) = weak.upgrade() else {
                continue;
            };
            match observer.try_borrow_mut() {
                Ok(mut observer) => observer.update(),
                Err(_) => {
                    if !stalled.iter().any(|queued| queued.ptr_eq(&weak)) {
                        stalled.push(weak);
                    }
                }
            }
        }
    }
    if !stalled.is_empty() {
        PENDING.with(|pending| pending.borrow_mut().append(&mut stalled));
    }
}

/// Object that gets notified when an [`Observable`] it registered with changes.
///
/// Mirrors QuantLib's `Observer`. The corresponding `update()` is the single
/// required method; the registry plumbing lives on [`Observable`].
pub trait Observer {
    /// Called by the observables this instance registered with on a change.
    fn update(&mut self);

    /// Whether a reentrant notification that finds this observer already
    /// updating should be replayed after the borrow releases.
    ///
    /// Divergence: C++ has no such hook. It exists because this port detects
    /// re-entrancy as a failed `RefCell::try_borrow_mut`, where C++ simply
    /// re-enters `update()` and lets the callee decide. An observer whose
    /// `update` already implements C++'s own recursion guard - a lazy object,
    /// via `updating_` - must return `false` here, or the port would replay a
    /// notification that C++ discards.
    fn defer_reentrant_update(&self) -> bool {
        true
    }
}

/// Contract for types that embed an [`Observable`] and broadcast through it.
///
/// Mirrors QuantLib's "inherits from `Observable`" preconditions (e.g. the
/// `handle.hpp` requirement on handle pointees): observers register with the
/// embedded observable this accessor exposes.
pub trait AsObservable {
    /// Access to the embedded observable for registering observers.
    fn observable(&self) -> &Observable;
}

/// Where a [`ResetThenNotify`] sends the notification once its reset has run.
enum Target {
    /// Broadcast to an observable's own observers.
    Broadcast(Shared<Observable>),
    /// Hand on to a single observer, with [`deliver`]'s re-entrancy discipline.
    Deliver(SharedMut<dyn Observer>),
}

/// The observer half every cache-holding type embeds: drop the stale state,
/// then pass the notification on (the C++ `update()` overrides of `Link`,
/// `DeltaVolQuote`, `FlatForward`, `TermStructure`, the derived quotes and the
/// Black-Scholes process, which all reset something and call the base
/// `update()`).
///
/// The reset runs *before* the notification, so an observer reading the type
/// while being notified sees fresh state rather than the value the
/// notification invalidates.
pub(crate) struct ResetThenNotify {
    reset: Box<dyn Fn()>,
    target: Target,
}

impl ResetThenNotify {
    /// Resets, then broadcasts through `observable`.
    pub(crate) fn broadcasting(
        observable: Shared<Observable>,
        reset: impl Fn() + 'static,
    ) -> SharedMut<ResetThenNotify> {
        shared_mut(ResetThenNotify {
            reset: Box::new(reset),
            target: Target::Broadcast(observable),
        })
    }

    /// Resets, then delivers to `observer` - the shape of a type layered on
    /// another observer half (a curve in front of its term-structure base).
    pub(crate) fn delivering(
        observer: SharedMut<dyn Observer>,
        reset: impl Fn() + 'static,
    ) -> SharedMut<ResetThenNotify> {
        shared_mut(ResetThenNotify {
            reset: Box::new(reset),
            target: Target::Deliver(observer),
        })
    }

    /// Pure forwarding: no state to reset, broadcast through `observable`.
    pub(crate) fn forwarding(observable: Shared<Observable>) -> SharedMut<ResetThenNotify> {
        Self::broadcasting(observable, || {})
    }

    /// Builds a fresh observable and the forwarder wired to it, the shared
    /// construction step of every forwarding type.
    pub(crate) fn forwarder() -> (Shared<Observable>, SharedMut<ResetThenNotify>) {
        let observable = shared(Observable::new());
        let forwarder = Self::forwarding(Shared::clone(&observable));
        (observable, forwarder)
    }
}

impl Observer for ResetThenNotify {
    fn update(&mut self) {
        (self.reset)();
        match &self.target {
            Target::Broadcast(observable) => observable.notify_observers(),
            Target::Deliver(observer) => deliver(observer),
        }
    }
}

/// Object that notifies its changes to a set of observers.
///
/// Mirrors QuantLib's `Observable`. Embed one in any type that needs to notify;
/// register observers with [`register_observer`](Observable::register_observer)
/// and broadcast with [`notify_observers`](Observable::notify_observers).
///
/// The registry lives behind a `RefCell` so notification takes `&self` and never
/// holds a borrow across `update`: an observer may freely register, unregister
/// or otherwise touch this observable while being notified.
#[derive(Default)]
pub struct Observable {
    observers: RefCell<Vec<ObserverEntry>>,
}

impl Observable {
    /// Creates an observable with no registered observers.
    pub fn new() -> Self {
        Observable::default()
    }

    /// Registers an observer, returning `true` if it was newly added.
    ///
    /// Registration is idempotent by pointer identity, mirroring the
    /// `std::set<Observer*>` semantics of the C++ implementation.
    pub fn register_observer(&self, observer: &SharedMut<dyn Observer>) -> bool {
        let weak = SharedMut::downgrade(observer);
        let mut observers = self.observers.borrow_mut();
        observers.retain(|entry| entry.observer.strong_count() > 0);
        if observers.iter().any(|entry| entry.observer.ptr_eq(&weak)) {
            return false;
        }
        observers.push(ObserverEntry::new(observer));
        true
    }

    /// Unregisters an observer, returning `true` if it had been registered.
    pub fn unregister_observer(&self, observer: &SharedMut<dyn Observer>) -> bool {
        let target = SharedMut::downgrade(observer);
        let mut observers = self.observers.borrow_mut();
        let mut removed = false;
        observers.retain(|entry| {
            if entry.observer.ptr_eq(&target) {
                removed = true;
                return false;
            }
            entry.observer.strong_count() > 0
        });
        removed
    }

    /// Notifies every currently registered, still-live observer.
    ///
    /// The live observers are snapshotted (as strong refs) and the registry
    /// borrow is dropped before any `update` runs, so re-entrant
    /// registration/unregistration during `update` is safe and does not affect
    /// this round of notification. Dropped observers are pruned.
    ///
    /// An observer whose `RefCell` is already mutably borrowed when the round
    /// reaches it - typically its own `update` is on the stack and re-entrantly
    /// triggered this notification, the case QuantLib runs as direct recursion -
    /// cannot take the unsatisfiable second `&mut`. Instead of dropping the
    /// notification, the round queues it on a pending list shared by every
    /// observable on the thread, and the outermost notification re-delivers it
    /// once the borrow has been released. Delivery is thus guaranteed no matter
    /// which observable the re-entrant notification arrived through, and every
    /// observer converges on the final state - the fixed point of QuantLib's
    /// recursion. Updates are idempotent invalidations, so a re-delivered
    /// observer may hear one extra round; and as in C++, a graph whose
    /// observers keep writing back forever does not terminate.
    ///
    /// The one case deferred past the current notification is an observer kept
    /// borrowed by code outside any `update` (a caller holding a borrow across
    /// the notification): it stays queued and is delivered at the end of the
    /// next outermost notification on the thread.
    pub fn notify_observers(&self) {
        let snapshot: Vec<(SharedMut<dyn Observer>, bool)> = {
            let mut observers = self.observers.borrow_mut();
            observers.retain(|entry| entry.observer.strong_count() > 0);
            observers
                .iter()
                .filter_map(|entry| {
                    entry
                        .observer
                        .upgrade()
                        .map(|observer| (observer, entry.defer_reentrant))
                })
                .collect()
        };
        {
            let _depth = DepthGuard::enter();
            for (observer, defer_reentrant) in snapshot {
                match observer.try_borrow_mut() {
                    Ok(mut observer) => observer.update(),
                    Err(_) => {
                        if defer_reentrant {
                            defer(&observer);
                        }
                    }
                }
            }
        }
        if DEPTH.with(Cell::get) == 0 {
            drain_pending();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::shared::{Shared, SharedMut, shared_mut};

    /// Mirrors the C++ test `UpdateCounter`: counts received notifications.
    struct UpdateCounter {
        counter: usize,
    }

    impl UpdateCounter {
        fn new() -> SharedMut<UpdateCounter> {
            shared_mut(UpdateCounter { counter: 0 })
        }
    }

    impl Observer for UpdateCounter {
        fn update(&mut self) {
            self.counter += 1;
        }
    }

    fn as_observer(obs: &SharedMut<UpdateCounter>) -> SharedMut<dyn Observer> {
        obs.clone()
    }

    #[test]
    fn notify_increments_registered_observers() {
        let observable = Observable::new();
        let counter = UpdateCounter::new();
        assert!(observable.register_observer(&as_observer(&counter)));
        assert_eq!(counter.borrow().counter, 0);

        observable.notify_observers();
        assert_eq!(counter.borrow().counter, 1);

        observable.notify_observers();
        assert_eq!(counter.borrow().counter, 2);
    }

    #[test]
    fn registration_is_idempotent() {
        let observable = Observable::new();
        let counter = UpdateCounter::new();
        assert!(observable.register_observer(&as_observer(&counter)));
        assert!(!observable.register_observer(&as_observer(&counter)));

        observable.notify_observers();
        assert_eq!(counter.borrow().counter, 1);
    }

    #[test]
    fn unregister_stops_notifications() {
        let observable = Observable::new();
        let counter = UpdateCounter::new();
        observable.register_observer(&as_observer(&counter));

        assert!(observable.unregister_observer(&as_observer(&counter)));
        observable.notify_observers();
        assert_eq!(counter.borrow().counter, 0);

        // unregistering again reports no change
        assert!(!observable.unregister_observer(&as_observer(&counter)));
    }

    #[test]
    fn unregister_on_empty_is_harmless() {
        let observable = Observable::new();
        let counter = UpdateCounter::new();
        assert!(!observable.unregister_observer(&as_observer(&counter)));
    }

    #[test]
    fn unregister_unknown_observer_is_not_confused_by_dead_weaks() {
        // A registered-then-dropped observer leaves a dead weak in the registry;
        // unregistering a never-registered observer must still report `false`
        // rather than mistaking the dead-weak pruning for a real removal.
        let observable = Observable::new();
        let registered = UpdateCounter::new();
        observable.register_observer(&as_observer(&registered));
        {
            let transient = UpdateCounter::new();
            observable.register_observer(&as_observer(&transient));
        }

        let never_registered = UpdateCounter::new();
        assert!(!observable.unregister_observer(&as_observer(&never_registered)));
        // the genuinely registered observer is still notified afterwards
        observable.notify_observers();
        assert_eq!(registered.borrow().counter, 1);
    }

    #[test]
    fn dropped_observers_are_pruned() {
        let observable = Observable::new();
        let survivor = UpdateCounter::new();
        observable.register_observer(&as_observer(&survivor));
        {
            let transient = UpdateCounter::new();
            observable.register_observer(&as_observer(&transient));
        }
        // the transient observer was dropped; notifying must not panic and the
        // survivor still gets its update
        observable.notify_observers();
        assert_eq!(survivor.borrow().counter, 1);
    }

    /// Mirrors `testAddAndDeleteObserverDuringNotifyObservers`: an observer that
    /// registers more observers and drops some during its own `update()` must
    /// not break notification of the initially-registered set.
    struct ReentrantObserver {
        updates: usize,
        observable: Shared<Observable>,
        spawned: SharedMut<Vec<SharedMut<UpdateCounter>>>,
        spawn_count: usize,
    }

    impl Observer for ReentrantObserver {
        fn update(&mut self) {
            self.updates += 1;
            for _ in 0..self.spawn_count {
                let extra = UpdateCounter::new();
                self.observable
                    .register_observer(&(extra.clone() as SharedMut<dyn Observer>));
                self.spawned.borrow_mut().push(extra);
            }
        }
    }

    #[test]
    fn add_observers_during_notify_does_not_miss_initial_observers() {
        // The observable is shared (not wrapped in a RefCell) precisely because
        // notification now takes `&self`; an observer can re-enter it directly.
        let observable = Shared::new(Observable::new());
        let spawned: SharedMut<Vec<SharedMut<UpdateCounter>>> = shared_mut(Vec::new());

        let plain = UpdateCounter::new();
        observable.register_observer(&as_observer(&plain));

        let reentrant = shared_mut(ReentrantObserver {
            updates: 0,
            observable: observable.clone(),
            spawned: spawned.clone(),
            spawn_count: 10,
        });
        observable.register_observer(&(reentrant.clone() as SharedMut<dyn Observer>));

        observable.notify_observers();

        // both initially-registered observers were updated exactly once...
        assert_eq!(plain.borrow().counter, 1);
        assert_eq!(reentrant.borrow().updates, 1);
        // ...and the observers added mid-notification exist but were not part of
        // this notification round (snapshot-before-notify semantics).
        assert_eq!(spawned.borrow().len(), 10);
        assert!(spawned.borrow().iter().all(|o| o.borrow().counter == 0));
    }

    /// Observer that re-enters `notify_observers` from within its own `update`,
    /// as a write-back observer does through the notifying observable.
    struct Renotifier {
        updates: usize,
        observable: Shared<Observable>,
    }

    impl Observer for Renotifier {
        fn update(&mut self) {
            self.updates += 1;
            if self.updates == 1 {
                self.observable.notify_observers();
            }
        }
    }

    /// Listener registered with two observables, mirroring a composite
    /// quote's invalidator: handling observable A's notification triggers
    /// observable B, whose round finds the listener still mid-`update`.
    struct CrossNotifier {
        updates: usize,
        other: Shared<Observable>,
        fired: bool,
    }

    impl Observer for CrossNotifier {
        fn update(&mut self) {
            self.updates += 1;
            if !self.fired {
                self.fired = true;
                self.other.notify_observers();
            }
        }
    }

    #[test]
    fn cross_observable_reentrant_notification_is_redelivered() {
        let a = Shared::new(Observable::new());
        let b = Shared::new(Observable::new());

        let listener = shared_mut(CrossNotifier {
            updates: 0,
            other: b.clone(),
            fired: false,
        });
        a.register_observer(&(listener.clone() as SharedMut<dyn Observer>));
        b.register_observer(&(listener.clone() as SharedMut<dyn Observer>));

        let bystander = UpdateCounter::new();
        b.register_observer(&as_observer(&bystander));

        a.notify_observers();

        // b's round ran while the listener was in-flight from a's round: the
        // miss must be re-delivered once a's round unwinds, not dropped
        assert_eq!(listener.borrow().updates, 2);
        assert_eq!(bystander.borrow().counter, 1);
    }

    #[test]
    fn notification_blocked_by_an_outside_borrow_is_delivered_later() {
        let observable = Observable::new();
        let counter = UpdateCounter::new();
        observable.register_observer(&as_observer(&counter));

        {
            let held = counter.borrow();
            observable.notify_observers();
            // the observer cannot be updated while the caller holds it...
            assert_eq!(held.counter, 0);
        }

        // ...so it stays queued and the next outermost notification delivers
        // both its own round and the queued re-delivery (updates are
        // idempotent invalidations)
        observable.notify_observers();
        assert_eq!(counter.borrow().counter, 2);
    }

    #[test]
    fn reentrant_notification_defers_the_in_flight_observer() {
        let observable = Shared::new(Observable::new());
        let plain = UpdateCounter::new();
        observable.register_observer(&as_observer(&plain));

        let renotifier = shared_mut(Renotifier {
            updates: 0,
            observable: observable.clone(),
        });
        observable.register_observer(&(renotifier.clone() as SharedMut<dyn Observer>));

        observable.notify_observers();

        // instead of panicking on a second mutable borrow, the nested round
        // queues the in-flight observer and the outermost round drains the
        // queue, matching the two updates C++ delivers through recursion...
        assert_eq!(renotifier.borrow().updates, 2);
        // ...and the other observer hears the outer and nested rounds, the
        // exact counts of the C++ recursion
        assert_eq!(plain.borrow().counter, 2);
    }
}