veecle-os-runtime 0.1.0

Veecle OS Runtime
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
// Everything defined in here except the macro are internal helpers, they often mention private types.
#![expect(private_bounds)]
#![expect(private_interfaces)]

use crate::actor::{Actor, Datastore, StoreRequest};
use crate::cons::{Cons, Nil, TupleConsToCons};
use crate::datastore::{
    ExclusiveReader, InitializedReader, Reader, Slot, Storable, Writer, generational,
};
use core::any::TypeId;
use core::pin::Pin;

/// Internal helper to implement [`Datastore::slot`] recursively for a cons-list of slots.
trait Slots {
    /// See [`Datastore::slot`].
    fn slot<T>(self: Pin<&Self>) -> Pin<&Slot<T>>
    where
        T: Storable + 'static;

    /// Returns the [`TypeId`] and type names for all the slots stored in this type.
    fn all_slots() -> impl Iterator<Item = (TypeId, &'static str)>;
}

impl Slots for Nil {
    fn slot<T>(self: Pin<&Self>) -> Pin<&Slot<T>>
    where
        T: Storable + 'static,
    {
        panic!("no slot available for `{}`", core::any::type_name::<T>())
    }

    fn all_slots() -> impl Iterator<Item = (TypeId, &'static str)> {
        core::iter::empty()
    }
}

impl<U, R> Slots for Cons<Slot<U>, R>
where
    U: Storable + 'static,
    R: Slots,
{
    fn slot<T>(self: Pin<&Self>) -> Pin<&Slot<T>>
    where
        T: Storable + 'static,
    {
        let this = self.project_ref();
        if TypeId::of::<U>() == TypeId::of::<T>() {
            this.0.assert_is_type()
        } else {
            this.1.slot::<T>()
        }
    }

    fn all_slots() -> impl Iterator<Item = (TypeId, &'static str)> {
        R::all_slots().chain(core::iter::once((
            TypeId::of::<U>(),
            core::any::type_name::<U>(),
        )))
    }
}

/// Internal helper to take a cons-list of `Storable` types and return a cons-list of slots for them.
trait IntoSlots {
    /// A cons-list that contains a slot for every type in this cons-list.
    type Slots: Slots;

    /// Creates a new instance of the slot cons-list with all slots empty.
    fn make_slots() -> Self::Slots;
}

impl IntoSlots for Nil {
    type Slots = Nil;

    fn make_slots() -> Self::Slots {
        Nil
    }
}

impl<T, R> IntoSlots for Cons<T, R>
where
    T: Storable + 'static,
    R: IntoSlots,
{
    type Slots = Cons<Slot<T>, R::Slots>;

    fn make_slots() -> Self::Slots {
        Cons(Slot::<T>::new(), R::make_slots())
    }
}

// This lint is hit when documenting with `--document-private-items`.
// If we use `expect`, a warning is emitted when not using `--document-private-items`.
// If we remove the lint, a warning is emitted when using `--document-private-items`.
// To be able to deny warning, we need to allow the lint here.
// https://github.com/rust-lang/rust/issues/145449
#[allow(rustdoc::private_intra_doc_links)]
/// Given a slot cons-list, combines it with a [`generational::Source`] to implement [`Datastore`].
impl<S: Slots> Datastore for Cons<generational::Source, S>
where
    S: Slots,
{
    fn source(self: Pin<&Self>) -> Pin<&generational::Source> {
        let this = self.project_ref();
        this.0
    }

    fn slot<T>(self: Pin<&Self>) -> Pin<&Slot<T>>
    where
        T: Storable + 'static,
    {
        let this = self.project_ref();
        this.1.slot::<T>()
    }
}

/// Given a cons-list of [`Storable`] types, returns a complete [`Datastore`] that contains a slot for each type.
pub fn make_store<T>() -> impl Datastore
where
    T: IntoSlots,
{
    Cons(generational::Source::new(), T::make_slots())
}

/// Internal helper to query how a [`StoreRequest`] type will use a specific type.
pub trait AccessKind {
    /// Returns whether this is a writer.
    fn writer(_type_id: TypeId) -> bool {
        false
    }

    /// Returns whether this is a reader (both exclusive and non-exclusive).
    fn reader(_type_id: TypeId) -> bool {
        false
    }

    /// Returns whether this is an exclusive reader.
    fn exclusive_reader(_type_id: TypeId) -> bool {
        false
    }
}

impl<T> AccessKind for Writer<'_, T>
where
    T: Storable + 'static,
{
    fn writer(type_id: TypeId) -> bool {
        type_id == TypeId::of::<T>()
    }
}

impl<T> AccessKind for Reader<'_, T>
where
    T: Storable + 'static,
{
    fn reader(type_id: TypeId) -> bool {
        type_id == TypeId::of::<T>()
    }
}

impl<T> AccessKind for InitializedReader<'_, T>
where
    T: Storable + 'static,
{
    fn reader(type_id: TypeId) -> bool {
        type_id == TypeId::of::<T>()
    }
}

impl<T> AccessKind for ExclusiveReader<'_, T>
where
    T: Storable + 'static,
{
    fn reader(type_id: TypeId) -> bool {
        type_id == TypeId::of::<T>()
    }

    fn exclusive_reader(type_id: TypeId) -> bool {
        type_id == TypeId::of::<T>()
    }
}

/// Internal helper to query how a cons-lists of [`StoreRequest`] types will use a specific type.
pub trait AccessCount {
    /// Returns how many writers for the given type exist in this list.
    fn writers(type_id: TypeId) -> usize;

    /// Returns how many readers for the given type exist in this list (both exclusive and non-exclusive).
    fn readers(type_id: TypeId) -> usize;

    /// Returns how many exclusive readers for the given type exist in this list.
    fn exclusive_readers(type_id: TypeId) -> usize;
}

impl AccessCount for Nil {
    fn writers(_type_id: TypeId) -> usize {
        0
    }

    fn readers(_type_id: TypeId) -> usize {
        0
    }

    fn exclusive_readers(_type_id: TypeId) -> usize {
        0
    }
}

impl<T, U> AccessCount for Cons<T, U>
where
    T: AccessKind,
    U: AccessCount,
{
    fn writers(type_id: TypeId) -> usize {
        (if T::writer(type_id) { 1 } else { 0 }) + U::writers(type_id)
    }

    fn readers(type_id: TypeId) -> usize {
        (if T::reader(type_id) { 1 } else { 0 }) + U::readers(type_id)
    }

    fn exclusive_readers(type_id: TypeId) -> usize {
        (if T::exclusive_reader(type_id) { 1 } else { 0 }) + U::exclusive_readers(type_id)
    }
}

/// Internal helper to query how a cons-list of cons-lists of [`StoreRequest`] types will use a specific type.
pub trait NestedAccessCount {
    /// Returns how many writers for the given type exist in this list of lists.
    fn writers(type_id: TypeId) -> usize;

    /// Returns how many readers for the given type exist in this list of lists (both exclusive and
    /// non-exclusive).
    fn readers(type_id: TypeId) -> usize;

    /// Returns how many exclusive readers for the given type exist in this list of lists.
    fn exclusive_readers(type_id: TypeId) -> usize;
}

impl NestedAccessCount for Nil {
    fn writers(_type_id: TypeId) -> usize {
        0
    }

    fn readers(_type_id: TypeId) -> usize {
        0
    }

    fn exclusive_readers(_type_id: TypeId) -> usize {
        0
    }
}

impl<T, U> NestedAccessCount for Cons<T, U>
where
    T: AccessCount,
    U: NestedAccessCount,
{
    fn writers(type_id: TypeId) -> usize {
        T::writers(type_id) + U::writers(type_id)
    }

    fn readers(type_id: TypeId) -> usize {
        T::readers(type_id) + U::readers(type_id)
    }

    fn exclusive_readers(type_id: TypeId) -> usize {
        T::exclusive_readers(type_id) + U::exclusive_readers(type_id)
    }
}

/// Internal helper to access details about a cons-list of actors so they can be validated against a store.
pub trait ActorList<'a> {
    /// A cons-list-of-cons-list-of-store-requests for this cons-list (essentially `self.map(|actor| actor.store_request)`
    /// where each actor has a cons-list of store-requests).
    type StoreRequests: NestedAccessCount;

    /// A cons-list of init-contexts for this cons-list (essentially `self.map(|actor| actor.init_context)`).
    type InitContexts;
}

impl ActorList<'_> for Nil {
    type StoreRequests = Nil;
    type InitContexts = Nil;
}

impl<'a, T, U> ActorList<'a> for Cons<T, U>
where
    T: Actor<'a, StoreRequest: TupleConsToCons>,
    U: ActorList<'a>,
    <<T as Actor<'a>>::StoreRequest as TupleConsToCons>::Cons: AccessCount,
{
    /// `Actor::StoreRequest` for the `#[actor]` generated types is a tuple-cons-list, for each actor in this list
    /// convert its store requests into our nominal cons-list.
    ///
    /// This doesn't work with manual `Actor` implementations that have non-tuple-cons-list `StoreRequest`s.
    type StoreRequests = Cons<
        <<T as Actor<'a>>::StoreRequest as TupleConsToCons>::Cons,
        <U as ActorList<'a>>::StoreRequests,
    >;

    /// For `Actor::InitContext` we just need to map directly to the associated type.
    type InitContexts = Cons<<T as Actor<'a>>::InitContext, <U as ActorList<'a>>::InitContexts>;
}

/// Internal helper that for given sets of actors and slots validates the guarantees around slot access that we want to
/// always uphold.
///
/// `init_contexts` is a cons-list of the init-context values for the actors in `A`. It is required to be passed here to
/// drive type-inference for `A` but then just returned.
///
/// `_store` is the reference to the store the actors will use. A copy is passed in here as the lifetime of this
/// reference may be required for the init-contexts inference.
pub fn validate_actors<'a, A, S, I>(init_contexts: I, _store: Pin<&'a impl Datastore>) -> I
where
    A: ActorList<'a, InitContexts = I>,
    S: IntoSlots,
{
    for (type_id, type_name) in S::Slots::all_slots() {
        assert!(
            A::StoreRequests::writers(type_id) > 0,
            "missing writer for `{type_name}`",
        );
        assert!(
            A::StoreRequests::readers(type_id) > 0,
            "missing reader for `{type_name}`",
        );
        assert!(
            A::StoreRequests::writers(type_id) == 1,
            "multiple writers for `{type_name}`",
        );
        if A::StoreRequests::exclusive_readers(type_id) > 0 {
            assert!(
                A::StoreRequests::readers(type_id) == 1,
                "conflict with exclusive reader for `{type_name}`",
            );
        }
    }

    init_contexts
}

/// Internal helper to get a full future that initializes and executes an [`Actor`] given a [`Datastore`]
pub async fn execute_actor<'a, A>(
    store: Pin<&'a impl Datastore>,
    init_context: A::InitContext,
) -> core::convert::Infallible
where
    A: Actor<'a>,
{
    let future = A::new(A::StoreRequest::request(store).await, init_context).run();

    #[cfg(feature = "veecle-telemetry")]
    let future = veecle_telemetry::future::FutureExt::with_span(
        future,
        veecle_telemetry::root_span!("actor", actor = core::any::type_name::<A>()),
    );

    match future.await {
        Err(error) => panic!("{error}"),
    }
}

/// Execute a given set of actors without heap allocation.
///
/// ```rust
/// use core::convert::Infallible;
/// use core::fmt::Debug;
///
/// use veecle_os_runtime::{Reader, Storable, Writer};
///
/// #[derive(Debug, Clone, PartialEq, Eq, Default, Storable)]
/// pub struct Ping {
///     value: u32,
/// }
///
/// #[derive(Debug, Clone, PartialEq, Eq, Default, Storable)]
/// pub struct Pong {
///     value: u32,
/// }
///
/// #[veecle_os_runtime::actor]
/// async fn ping_actor(mut ping: Writer<'_, Ping>, pong: Reader<'_, Pong>) -> Infallible {
///     let mut value = 0;
///     ping.write(Ping { value }).await;
///
///     let mut pong = pong.wait_init().await;
///     loop {
///         ping.write(Ping { value }).await;
///         value += 1;
///
///         pong.wait_for_update().await.read(|pong| {
///             println!("Pong: {}", pong.value);
///         });
/// #       // Exit the application to allow doc-tests to complete.
/// #       std::process::exit(0);
///     }
/// }
///
/// #[veecle_os_runtime::actor]
/// async fn pong_actor(mut pong: Writer<'_, Pong>, ping: Reader<'_, Ping>) -> Infallible {
///     let mut ping = ping.wait_init().await;
///     loop {
///         let ping = ping.wait_for_update().await.read_cloned();
///         println!("Ping: {}", ping.value);
///
///         let data = Pong { value: ping.value };
///         pong.write(data).await;
///     }
/// }
///
/// futures::executor::block_on(
///    veecle_os_runtime::execute! {
///        store: [Ping, Pong],
///        actors: [PingActor, PongActor],
///    }
/// )
#[macro_export]
macro_rules! execute {
    (
        store: [
            $($data_type:ty),* $(,)?
        ],
        actors: [
            $($actor_type:ty $(: $init_context:expr )? ),* $(,)?
        ] $(,)?
    ) => {{
        async {
            let store = core::pin::pin!(
                $crate::__exports::make_store::<$crate::__make_cons!(@type $($data_type,)*)>(),
            );
            let store = store.as_ref();

            let init_contexts = $crate::__exports::validate_actors::<
                $crate::__make_cons!(@type $($actor_type,)*),
                $crate::__make_cons!(@type $($data_type,)*),
                _,
            >($crate::__make_cons!(@value $(
                // Wrapper block is used to provide a `()` if no expression is passed.
                { $($init_context)? },
            )*), store);

            // To count how many actors there are, we create an array of `()` with the appropriate length.
            const LEN: usize = [$($crate::discard_to_unit!($actor_type),)*].len();

            let futures: [core::pin::Pin<&mut dyn core::future::Future<Output = core::convert::Infallible>>; LEN] =
                $crate::make_futures! {
                    init_contexts: init_contexts,
                    store: store,
                    actors: [$($actor_type,)*],
                };

            static SHARED: $crate::__exports::ExecutorShared<LEN>
                = $crate::__exports::ExecutorShared::new(&SHARED);

            let executor = $crate::__exports::Executor::new(
                &SHARED,
                $crate::__exports::Datastore::source(store),
                futures,
            );

            executor.run().await
        }
    }};
}

/// Internal helper to construct an array of pinned futures for given actors + init-contexts + store.
///
/// Returns essentially `[Pin<&mut dyn Future<Output = Infallible>; actors.len()]`, but likely needs annotation at the
/// use site to force the unsize coercion.
#[doc(hidden)]
#[macro_export]
macro_rules! make_futures {
    (
        // A cons-list of init-contexts for the passed actors.
        init_contexts: $init_contexts:expr,
        store: $store:expr,
        actors: [
            $($types:ty,)*
        ],
    ) => {
        $crate::make_futures! {
            init_contexts: $init_contexts,
            store: $store,
            done: [],
            todo: [$($types,)*],
            futures: [],
        }
    };

    // When there are no more actors, just return the futures as an array.
    (
        init_contexts: $init_contexts:expr,
        store: $store:expr,
        done: [$($done:ty,)*],
        todo: [],
        futures: [
            $($futures:expr,)*
        ],
    ) => {
        [$($futures,)*]
    };

    // For each actor, add an element to the futures array, using the already done actors as the depth to read from the
    // init-contexts cons-list. Then push this actor onto the done list so that the next actor will read deeper from the
    // init-contexts.
    (
        init_contexts: $init_contexts:expr,
        store: $store:expr,
        done: [$($done:ty,)*],
        todo: [$current:ty, $($todo:ty,)*],
        futures: [
            $($futures:expr,)*
        ],
    ) => {
        $crate::make_futures! {
            init_contexts: $init_contexts,
            store: $store,
            done: [$($done,)* $current,],
            todo: [$($todo,)*],
            futures: [
                $($futures,)*
                core::pin::pin!(
                    $crate::__exports::execute_actor::<$current>(
                        $store,
                        $crate::__read_cons! {
                            from: $init_contexts,
                            depth: [$($done)*],
                        },
                    )
                ),
            ],
        }
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! discard_to_unit {
    ($_:tt) => {
        ()
    };
}