Skip to main content

hydro_lang/live_collections/sliced/
style.rs

1//! Styled wrappers for live collections used with the `sliced!` macro.
2//!
3//! This module provides wrapper types that store both a collection and its associated
4//! non-determinism guard, allowing the nondet to be properly passed through during slicing.
5
6#[cfg(stageleft_runtime)]
7use std::marker::PhantomData;
8
9use super::Slicable;
10#[cfg(stageleft_runtime)]
11use crate::forward_handle::{CycleCollection, CycleCollectionWithInitial};
12use crate::forward_handle::{TickCycle, TickCycleHandle};
13use crate::live_collections::boundedness::{Bounded, Boundedness, IsBounded, Unbounded};
14use crate::live_collections::keyed_singleton::{
15    BoundedValue, KeyedSingletonBound, KeyedSingletonSnapshotBound,
16};
17use crate::live_collections::optional::OptionalBound;
18use crate::live_collections::singleton::SingletonBound;
19use crate::live_collections::stream::{ExactlyOnce, Ordering, Retries, TotalOrder};
20use crate::location::Location;
21use crate::location::tick::{DeferTick, Tick};
22use crate::nondet::{NonDet, nondet};
23
24/// Default style wrapper that stores a collection and its non-determinism guard.
25///
26/// This is used by the `sliced!` macro when no explicit style is specified. This style is
27/// deprecated; use the explicit [`batch`] or [`snapshot`] styles instead.
28pub struct Default<T> {
29    pub(crate) collection: T,
30    pub(crate) nondet: NonDet,
31}
32
33impl<T> Default<T> {
34    /// Creates a new default-styled wrapper.
35    pub fn new(collection: T, nondet: NonDet) -> Self {
36        Self { collection, nondet }
37    }
38}
39
40/// Helper function for unstyled `use` in `sliced!` macro - wraps the collection in Default style.
41#[doc(hidden)]
42#[deprecated(
43    note = "use `use::batch(...)` for stream-like collections or `use::snapshot(...)` for singleton-like collections instead"
44)]
45pub fn default<T>(t: T, nondet: NonDet) -> Default<T> {
46    Default::new(t, nondet)
47}
48
49/// Batch style wrapper that stores a stream-like collection and its non-determinism guard.
50///
51/// This is used by the `sliced!` macro when `use::batch(...)` is specified.
52pub struct Batch<T, H = ()> {
53    pub(crate) collection: T,
54    pub(crate) nondet: NonDet<H>,
55}
56
57impl<T, H> Batch<T, H> {
58    /// Creates a new batch-styled wrapper.
59    pub fn new(collection: T, nondet: NonDet<H>) -> Self {
60        Self { collection, nondet }
61    }
62}
63
64/// Wraps a stream-like live collection (such as a [`Stream`](crate::live_collections::Stream),
65/// [`KeyedStream`](crate::live_collections::KeyedStream), or a
66/// [`KeyedSingleton`](crate::live_collections::KeyedSingleton) with bounded values) to be
67/// sliced into non-deterministic batches of asynchronously arriving elements.
68pub fn batch<T, H>(t: T, nondet: NonDet<H>) -> Batch<T, H> {
69    Batch::new(t, nondet)
70}
71
72/// Snapshot style wrapper that stores a singleton-like collection and its non-determinism guard.
73///
74/// This is used by the `sliced!` macro when `use::snapshot(...)` is specified.
75pub struct Snapshot<T, H = ()> {
76    pub(crate) collection: T,
77    pub(crate) nondet: NonDet<H>,
78}
79
80impl<T, H> Snapshot<T, H> {
81    /// Creates a new snapshot-styled wrapper.
82    pub fn new(collection: T, nondet: NonDet<H>) -> Self {
83        Self { collection, nondet }
84    }
85}
86
87/// Wraps a singleton-like live collection (such as a
88/// [`Singleton`](crate::live_collections::Singleton),
89/// [`Optional`](crate::live_collections::Optional), or a
90/// [`KeyedSingleton`](crate::live_collections::KeyedSingleton) with asynchronously updated
91/// values) to be sliced into non-deterministic snapshots of its continuously changing value.
92pub fn snapshot<T, H>(t: T, nondet: NonDet<H>) -> Snapshot<T, H> {
93    Snapshot::new(t, nondet)
94}
95
96/// Time style wrapper that stores the non-determinism guard for reading the wall clock.
97///
98/// This is used by the `sliced!` macro when `use::time(...)` is specified. It carries no
99/// collection: the slice is the tick's current wall-clock reading
100/// ([`Tick::current_tick_instant`]).
101#[cfg(feature = "tokio")]
102pub struct Time {
103    pub(crate) nondet: NonDet,
104}
105
106#[cfg(feature = "tokio")]
107impl Time {
108    /// Creates a new time-styled wrapper.
109    pub fn new(nondet: NonDet) -> Self {
110        Self { nondet }
111    }
112}
113
114/// Reads the wall clock at each tick of the slice, as a bounded [`Singleton`] of
115/// [`tokio::time::Instant`] (the value depends on when the tick executes, hence the
116/// non-determinism guard). Since it carries no collection, it cannot be the first `use`
117/// of a `sliced!` block (the tick's location comes from a collection).
118///
119/// [`Singleton`]: crate::live_collections::Singleton
120#[cfg(feature = "tokio")]
121pub fn time(nondet: NonDet) -> Time {
122    Time::new(nondet)
123}
124
125/// Atomic style wrapper that stores a collection and its non-determinism guard.
126///
127/// This is used by the `sliced!` macro when `use::atomic(...)` is specified.
128pub struct Atomic<T> {
129    pub(crate) collection: T,
130    pub(crate) nondet: NonDet,
131}
132
133impl<T> Atomic<T> {
134    /// Creates a new atomic-styled wrapper.
135    pub fn new(collection: T, nondet: NonDet) -> Self {
136        Self { collection, nondet }
137    }
138}
139
140/// Wraps a live collection to be treated atomically during slicing.
141pub fn atomic<T>(t: T, nondet: NonDet) -> Atomic<T> {
142    Atomic::new(t, nondet)
143}
144
145/// Clone style wrapper that stores a bounded singleton-like collection.
146///
147/// This is used by the `sliced!` macro when `use::clone(...)` is specified.
148pub struct Cloned<T> {
149    pub(crate) collection: T,
150}
151
152impl<T> Cloned<T> {
153    /// Creates a new clone-styled wrapper.
154    pub fn new(collection: T) -> Self {
155        Self { collection }
156    }
157}
158
159/// Wraps a top-level **bounded** singleton-like live collection (a
160/// [`Singleton`](crate::live_collections::Singleton) or an
161/// [`Optional`](crate::live_collections::Optional)), such as a configuration value fixed when
162/// the flow is built, to be read in every slice.
163///
164/// Unlike [`snapshot`], this takes no non-determinism guard: a bounded collection has a single
165/// immutable value, so every slice observes that same value. Unbounded collections are
166/// rejected at compile time (use [`snapshot`] for those).
167///
168/// The slice is a bounded [`Singleton`](crate::live_collections::Singleton) (or
169/// [`Optional`](crate::live_collections::Optional)) on the tick of the `sliced!` body, as
170/// produced by [`Singleton::clone_into_tick`](crate::live_collections::Singleton::clone_into_tick).
171/// The value is **cloned into every tick**, so `T` must implement [`Clone`]; reading it by
172/// reference instead requires snapshots of singleton-like collections to return references
173/// ([hydro#2415](https://github.com/hydro-project/hydro/issues/2415)), and a `use::reference`
174/// style is planned for that.
175///
176/// ```rust
177/// # #[cfg(feature = "deploy")] {
178/// # use hydro_lang::prelude::*;
179/// # use futures::StreamExt;
180/// # tokio_test::block_on(hydro_lang::test_util::stream_transform_test(|process| {
181/// let offset = process.singleton(q!(100));
182/// let input = process.source_iter(q!(vec![1, 2, 3]));
183/// sliced! {
184///     let batch = use::batch(input, nondet!(/** test */));
185///     let offset = use::clone(offset);
186///     batch.cross_singleton(offset).map(q!(|(x, offset)| x + offset))
187/// }
188/// # }, |mut stream| async move {
189/// // 101, 102, 103
190/// # for w in vec![101, 102, 103] {
191/// #     assert_eq!(stream.next().await.unwrap(), w);
192/// # }
193/// # }));
194/// # }
195/// ```
196pub fn clone<T>(t: T) -> Cloned<T> {
197    Cloned::new(t)
198}
199
200/// Creates a stateful cycle with an initial value for use in `sliced!`.
201///
202/// The tick (which is the source of truth for lifetimes) is bound first, returning a
203/// [`StateBuilder`] which accepts the user-provided initializer via [`StateBuilder::build`].
204/// This two-step layout ensures that type errors caused by a bad initializer are attributed
205/// to the initializer argument rather than the tick or the entire macro invocation.
206///
207/// The initial value is computed from a closure that receives the location
208/// for the body of the slice.
209///
210/// The initial value is used on the first iteration, and subsequent iterations receive
211/// the value assigned to the mutable binding at the end of the previous iteration.
212#[cfg(stageleft_runtime)]
213pub fn state<'t, S, L>(tick: &'t Tick<L>) -> StateBuilder<'t, S, L> {
214    StateBuilder {
215        tick,
216        _phantom: PhantomData,
217    }
218}
219
220/// Builder returned by [`state`], which accepts the user-provided initializer.
221#[cfg(stageleft_runtime)]
222pub struct StateBuilder<'t, S, L> {
223    tick: &'t Tick<L>,
224    _phantom: PhantomData<S>,
225}
226
227#[cfg(stageleft_runtime)]
228impl<'t, 'a, S, L: Location<'a>> StateBuilder<'t, S, L> {
229    /// Supplies the initializer closure and creates the stateful cycle.
230    ///
231    /// The initializer takes the tick at the builder's `'t` lifetime (rather than a
232    /// higher-ranked `for<'x>` bound), since the builder already stores the tick reference.
233    /// This way, an initializer that requires a specific tick reference lifetime produces a
234    /// borrow error directly on the tick, instead of a confusing "implementation of `Fn` is
235    /// not general enough" error that blames an unrelated variable.
236    #[expect(
237        private_bounds,
238        reason = "only Hydro collections can implement CycleCollectionWithInitial"
239    )]
240    pub fn build(self, initial_fn: impl FnOnce(&'t Tick<L>) -> S) -> (TickCycleHandle<'a, S>, S)
241    where
242        S: CycleCollectionWithInitial<'a, TickCycle, Location = Tick<L::DropConsistency>>,
243    {
244        let initial = initial_fn(self.tick);
245        initial.location().clone().cycle_with_initial(initial)
246    }
247}
248
249/// Creates a stateful cycle without an initial value for use in `sliced!`.
250///
251/// The tick (which is the source of truth for lifetimes) is bound first, returning a
252/// [`StateNullBuilder`] which creates the cycle via [`StateNullBuilder::build`].
253///
254/// On the first iteration, the state will be null/empty. Subsequent iterations receive
255/// the value assigned to the mutable binding at the end of the previous iteration.
256#[cfg(stageleft_runtime)]
257pub fn state_null<'t, S, L>(tick: &'t Tick<L>) -> StateNullBuilder<'t, S, L> {
258    StateNullBuilder {
259        tick,
260        _phantom: PhantomData,
261    }
262}
263
264/// Builder returned by [`state_null`], which creates the cycle.
265#[cfg(stageleft_runtime)]
266pub struct StateNullBuilder<'t, S, L> {
267    tick: &'t Tick<L>,
268    _phantom: PhantomData<S>,
269}
270
271#[cfg(stageleft_runtime)]
272impl<'t, 'a, S, L: Location<'a>> StateNullBuilder<'t, S, L> {
273    /// Creates the stateful cycle, which starts as null/empty on the first iteration.
274    #[expect(
275        private_bounds,
276        reason = "only Hydro collections can implement CycleCollection"
277    )]
278    pub fn build(self) -> (TickCycleHandle<'a, S>, S)
279    where
280        S: CycleCollection<'a, TickCycle, Location = Tick<L::DropConsistency>> + DeferTick,
281    {
282        self.tick.cycle::<S, _>()
283    }
284}
285
286// ============================================================================
287// Time style Slicable implementation
288// ============================================================================
289
290#[cfg(feature = "tokio")]
291impl<'a, L: Location<'a>> Slicable<'a, L> for Time {
292    type Slice =
293        crate::live_collections::Singleton<tokio::time::Instant, Tick<L::DropConsistency>, Bounded>;
294    type Backtrace = crate::compile::ir::backtrace::Backtrace;
295
296    fn get_location(&self) -> L {
297        unreachable!(
298            "`use::time(...)` carries no collection; it cannot be the first `use` of a `sliced!` block"
299        )
300    }
301    fn slice(self, tick: &Tick<L>, backtrace: Self::Backtrace) -> Self::Slice {
302        let out = tick.current_tick_instant(self.nondet);
303        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
304        out
305    }
306}
307
308// ============================================================================
309// Default style Slicable implementations
310//
311// All of these drop consistency because they are performing non-deterministic
312// batching / snapshotting.
313// ============================================================================
314
315impl<'a, T, L: Location<'a>, B: Boundedness, O: Ordering, R: Retries>
316    Slicable<'a, L::DropConsistency> for Default<crate::live_collections::Stream<T, L, B, O, R>>
317{
318    type Slice = crate::live_collections::Stream<T, Tick<L::DropConsistency>, Bounded, O, R>;
319    type Backtrace = crate::compile::ir::backtrace::Backtrace;
320
321    fn get_location(&self) -> L::DropConsistency {
322        self.collection.location().drop_consistency()
323    }
324    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
325        let _ = self.nondet;
326        let out = self.collection.batch(
327            tick,
328            nondet!(/** justified by the guard stored in this style wrapper */),
329        );
330        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
331        out
332    }
333}
334
335impl<'a, T, L: Location<'a>, B: SingletonBound> Slicable<'a, L::DropConsistency>
336    for Default<crate::live_collections::Singleton<T, L, B>>
337{
338    type Slice = crate::live_collections::Singleton<T, Tick<L::DropConsistency>, Bounded>;
339    type Backtrace = crate::compile::ir::backtrace::Backtrace;
340
341    fn get_location(&self) -> L::DropConsistency {
342        self.collection.location().drop_consistency()
343    }
344    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
345        let _ = self.nondet;
346        let out = self.collection.snapshot(
347            tick,
348            nondet!(/** justified by the guard stored in this style wrapper */),
349        );
350        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
351        out
352    }
353}
354
355impl<'a, T, L: Location<'a>, B: OptionalBound> Slicable<'a, L::DropConsistency>
356    for Default<crate::live_collections::Optional<T, L, B>>
357{
358    type Slice = crate::live_collections::Optional<T, Tick<L::DropConsistency>, Bounded>;
359    type Backtrace = crate::compile::ir::backtrace::Backtrace;
360
361    fn get_location(&self) -> L::DropConsistency {
362        self.collection.location().drop_consistency()
363    }
364    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
365        let out = self.collection.snapshot(tick, self.nondet);
366        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
367        out
368    }
369}
370
371impl<'a, K, V, L: Location<'a>, B: Boundedness, O: Ordering, R: Retries>
372    Slicable<'a, L::DropConsistency>
373    for Default<crate::live_collections::KeyedStream<K, V, L, B, O, R>>
374{
375    type Slice =
376        crate::live_collections::KeyedStream<K, V, Tick<L::DropConsistency>, Bounded, O, R>;
377    type Backtrace = crate::compile::ir::backtrace::Backtrace;
378
379    fn get_location(&self) -> L::DropConsistency {
380        self.collection.location().drop_consistency()
381    }
382    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
383        let _ = self.nondet;
384        let out = self.collection.batch(
385            tick,
386            nondet!(/** justified by the guard stored in this style wrapper */),
387        );
388        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
389        out
390    }
391}
392
393impl<'a, K, V, L: Location<'a>, B: KeyedSingletonSnapshotBound<ValueBound = Unbounded>>
394    Slicable<'a, L::DropConsistency>
395    for Default<crate::live_collections::KeyedSingleton<K, V, L, B>>
396{
397    type Slice = crate::live_collections::KeyedSingleton<K, V, Tick<L::DropConsistency>, Bounded>;
398    type Backtrace = crate::compile::ir::backtrace::Backtrace;
399
400    fn get_location(&self) -> L::DropConsistency {
401        self.collection.location().drop_consistency()
402    }
403    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
404        let _ = self.nondet;
405        let out = self.collection.snapshot(
406            tick,
407            nondet!(/** justified by the guard stored in this style wrapper */),
408        );
409        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
410        out
411    }
412}
413
414impl<'a, K, V, L: Location<'a>> Slicable<'a, L::DropConsistency>
415    for Default<crate::live_collections::KeyedSingleton<K, V, L, BoundedValue>>
416{
417    type Slice = crate::live_collections::KeyedSingleton<K, V, Tick<L::DropConsistency>, Bounded>;
418    type Backtrace = crate::compile::ir::backtrace::Backtrace;
419
420    fn get_location(&self) -> L::DropConsistency {
421        self.collection.location().drop_consistency()
422    }
423    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
424        let _ = self.nondet;
425        let out = self.collection.batch(
426            tick,
427            nondet!(/** justified by the guard stored in this style wrapper */),
428        );
429        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
430        out
431    }
432}
433
434// ============================================================================
435// Batch style Slicable implementations (stream-like collections)
436//
437// All of these drop consistency because they are performing non-deterministic
438// batching.
439// ============================================================================
440
441impl<'a, T, L: Location<'a>, B: Boundedness, O: Ordering, R: Retries>
442    Slicable<'a, L::DropConsistency>
443    for Batch<
444        crate::live_collections::Stream<T, L, B, O, R>,
445        Option<crate::sim_hooks::BatchHook<T, O, R, L::SimHookScope>>,
446    >
447{
448    type Slice = crate::live_collections::Stream<T, Tick<L::DropConsistency>, Bounded, O, R>;
449    type Backtrace = crate::compile::ir::backtrace::Backtrace;
450
451    fn get_location(&self) -> L::DropConsistency {
452        self.collection.location().drop_consistency()
453    }
454    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
455        let out = self.collection.batch(tick, self.nondet);
456        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
457        out
458    }
459}
460
461impl<'a, K, V, L: Location<'a>, B: Boundedness, O: Ordering, R: Retries>
462    Slicable<'a, L::DropConsistency>
463    for Batch<
464        crate::live_collections::KeyedStream<K, V, L, B, O, R>,
465        Option<crate::sim_hooks::KeyedBatchHook<K, V, O, R, L::SimHookScope>>,
466    >
467{
468    type Slice =
469        crate::live_collections::KeyedStream<K, V, Tick<L::DropConsistency>, Bounded, O, R>;
470    type Backtrace = crate::compile::ir::backtrace::Backtrace;
471
472    fn get_location(&self) -> L::DropConsistency {
473        self.collection.location().drop_consistency()
474    }
475    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
476        let out = self.collection.batch(tick, self.nondet);
477        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
478        out
479    }
480}
481
482impl<'a, K, V, L: Location<'a>, B: KeyedSingletonBound<ValueBound = Bounded>>
483    Slicable<'a, L::DropConsistency>
484    for Batch<
485        crate::live_collections::KeyedSingleton<K, V, L, B>,
486        Option<crate::sim_hooks::KeyedBatchHook<K, V, TotalOrder, ExactlyOnce, L::SimHookScope>>,
487    >
488{
489    type Slice = crate::live_collections::KeyedSingleton<K, V, Tick<L::DropConsistency>, Bounded>;
490    type Backtrace = crate::compile::ir::backtrace::Backtrace;
491
492    fn get_location(&self) -> L::DropConsistency {
493        self.collection.location().drop_consistency()
494    }
495    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
496        let out = self.collection.batch(tick, self.nondet);
497        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
498        out
499    }
500}
501
502// ============================================================================
503// Snapshot style Slicable implementations (singleton-like collections)
504//
505// All of these drop consistency because they are performing non-deterministic
506// snapshotting.
507// ============================================================================
508
509impl<'a, T, L: Location<'a>, B: SingletonBound> Slicable<'a, L::DropConsistency>
510    for Snapshot<
511        crate::live_collections::Singleton<T, L, B>,
512        Option<crate::sim_hooks::SnapshotHook<T, L::SimHookScope>>,
513    >
514{
515    type Slice = crate::live_collections::Singleton<T, Tick<L::DropConsistency>, Bounded>;
516    type Backtrace = crate::compile::ir::backtrace::Backtrace;
517
518    fn get_location(&self) -> L::DropConsistency {
519        self.collection.location().drop_consistency()
520    }
521    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
522        let out = self.collection.snapshot(tick, self.nondet);
523        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
524        out
525    }
526}
527
528impl<'a, T, L: Location<'a>, B: OptionalBound> Slicable<'a, L::DropConsistency>
529    for Snapshot<crate::live_collections::Optional<T, L, B>>
530{
531    type Slice = crate::live_collections::Optional<T, Tick<L::DropConsistency>, Bounded>;
532    type Backtrace = crate::compile::ir::backtrace::Backtrace;
533
534    fn get_location(&self) -> L::DropConsistency {
535        self.collection.location().drop_consistency()
536    }
537    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
538        let out = self.collection.snapshot(tick, self.nondet);
539        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
540        out
541    }
542}
543
544impl<'a, K, V, L: Location<'a>, B: KeyedSingletonSnapshotBound> Slicable<'a, L::DropConsistency>
545    for Snapshot<
546        crate::live_collections::KeyedSingleton<K, V, L, B>,
547        Option<crate::sim_hooks::KeyedSnapshotHook<K, V, L::SimHookScope>>,
548    >
549{
550    type Slice = crate::live_collections::KeyedSingleton<K, V, Tick<L::DropConsistency>, Bounded>;
551    type Backtrace = crate::compile::ir::backtrace::Backtrace;
552
553    fn get_location(&self) -> L::DropConsistency {
554        self.collection.location().drop_consistency()
555    }
556    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
557        let out = self.collection.snapshot(tick, self.nondet);
558        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
559        out
560    }
561}
562
563// ============================================================================
564// Atomic style Slicable implementations
565// ============================================================================
566
567impl<'a, T, L: Location<'a>, B: Boundedness, O: Ordering, R: Retries>
568    Slicable<'a, L::DropConsistency>
569    for Atomic<crate::live_collections::Stream<T, crate::location::Atomic<L>, B, O, R>>
570{
571    type Slice = crate::live_collections::Stream<T, Tick<L::DropConsistency>, Bounded, O, R>;
572    type Backtrace = crate::compile::ir::backtrace::Backtrace;
573    fn get_location(&self) -> L::DropConsistency {
574        self.collection.location().tick.l.drop_consistency()
575    }
576
577    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
578        let _ = self.nondet;
579        let out = self.collection.batch_atomic(
580            tick,
581            nondet!(/** justified by the guard stored in this style wrapper */),
582        );
583        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
584        out
585    }
586}
587
588impl<'a, T, L: Location<'a>, B: SingletonBound> Slicable<'a, L::DropConsistency>
589    for Atomic<crate::live_collections::Singleton<T, crate::location::Atomic<L>, B>>
590{
591    type Slice = crate::live_collections::Singleton<T, Tick<L::DropConsistency>, Bounded>;
592    type Backtrace = crate::compile::ir::backtrace::Backtrace;
593    fn get_location(&self) -> L::DropConsistency {
594        self.collection.location().tick.l.drop_consistency()
595    }
596
597    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
598        let _ = self.nondet;
599        let out = self.collection.snapshot_atomic(
600            tick,
601            nondet!(/** justified by the guard stored in this style wrapper */),
602        );
603        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
604        out
605    }
606}
607
608impl<'a, T, L: Location<'a>, B: OptionalBound> Slicable<'a, L::DropConsistency>
609    for Atomic<crate::live_collections::Optional<T, crate::location::Atomic<L>, B>>
610{
611    type Slice = crate::live_collections::Optional<T, Tick<L::DropConsistency>, Bounded>;
612    type Backtrace = crate::compile::ir::backtrace::Backtrace;
613    fn get_location(&self) -> L::DropConsistency {
614        self.collection.location().tick.l.drop_consistency()
615    }
616
617    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
618        let out = self.collection.snapshot_atomic(tick, self.nondet);
619        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
620        out
621    }
622}
623
624impl<'a, K, V, L: Location<'a>, B: Boundedness, O: Ordering, R: Retries>
625    Slicable<'a, L::DropConsistency>
626    for Atomic<crate::live_collections::KeyedStream<K, V, crate::location::Atomic<L>, B, O, R>>
627{
628    type Slice =
629        crate::live_collections::KeyedStream<K, V, Tick<L::DropConsistency>, Bounded, O, R>;
630    type Backtrace = crate::compile::ir::backtrace::Backtrace;
631    fn get_location(&self) -> L::DropConsistency {
632        self.collection.location().tick.l.drop_consistency()
633    }
634
635    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
636        let out = self.collection.batch_atomic(tick, self.nondet);
637        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
638        out
639    }
640}
641
642impl<'a, K, V, L: Location<'a>, B: KeyedSingletonSnapshotBound<ValueBound = Unbounded>>
643    Slicable<'a, L::DropConsistency>
644    for Atomic<crate::live_collections::KeyedSingleton<K, V, crate::location::Atomic<L>, B>>
645{
646    type Slice = crate::live_collections::KeyedSingleton<K, V, Tick<L::DropConsistency>, Bounded>;
647    type Backtrace = crate::compile::ir::backtrace::Backtrace;
648    fn get_location(&self) -> L::DropConsistency {
649        self.collection.location().tick.l.drop_consistency()
650    }
651
652    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
653        let out = self.collection.snapshot_atomic(tick, self.nondet);
654        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
655        out
656    }
657}
658
659impl<'a, K, V, L: Location<'a>> Slicable<'a, L::DropConsistency>
660    for Atomic<
661        crate::live_collections::KeyedSingleton<K, V, crate::location::Atomic<L>, BoundedValue>,
662    >
663{
664    type Slice = crate::live_collections::KeyedSingleton<K, V, Tick<L::DropConsistency>, Bounded>;
665    type Backtrace = crate::compile::ir::backtrace::Backtrace;
666    fn get_location(&self) -> L::DropConsistency {
667        self.collection.location().tick.l.drop_consistency()
668    }
669
670    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
671        let out = self.collection.batch_atomic(tick, self.nondet);
672        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
673        out
674    }
675}
676
677// ============================================================================
678// Clone style Slicable implementations (bounded singleton-like collections)
679//
680// These drop consistency so that they compose with the other styles in the same `sliced!`
681// block, but they introduce no non-determinism: a bounded collection has a single immutable
682// value, which is cloned into every slice. Like `clone_into_tick`, this lowers to a lazy
683// `snapshot()` of that value, so the simulator never makes a choice for it.
684// ============================================================================
685
686impl<'a, T: Clone, L: Location<'a>, B: SingletonBound + IsBounded> Slicable<'a, L::DropConsistency>
687    for Cloned<crate::live_collections::Singleton<T, L, B>>
688{
689    type Slice = crate::live_collections::Singleton<T, Tick<L::DropConsistency>, Bounded>;
690    type Backtrace = crate::compile::ir::backtrace::Backtrace;
691
692    fn get_location(&self) -> L::DropConsistency {
693        self.collection.location().drop_consistency()
694    }
695
696    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
697        let out = self.collection.clone_into_tick(tick);
698        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
699        out
700    }
701}
702
703impl<'a, T: Clone, L: Location<'a>, B: OptionalBound + IsBounded> Slicable<'a, L::DropConsistency>
704    for Cloned<crate::live_collections::Optional<T, L, B>>
705{
706    type Slice = crate::live_collections::Optional<T, Tick<L::DropConsistency>, Bounded>;
707    type Backtrace = crate::compile::ir::backtrace::Backtrace;
708
709    fn get_location(&self) -> L::DropConsistency {
710        self.collection.location().drop_consistency()
711    }
712
713    fn slice(self, tick: &Tick<L::DropConsistency>, backtrace: Self::Backtrace) -> Self::Slice {
714        let out = self.collection.weaken_consistency().clone_into_tick(tick);
715        out.ir_node.borrow_mut().op_metadata_mut().backtrace = backtrace;
716        out
717    }
718}