This repository was archived by the owner on Oct 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtraits.rs
More file actions
61 lines (55 loc) · 2.43 KB
/
Copy pathtraits.rs
File metadata and controls
61 lines (55 loc) · 2.43 KB
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
use rkyv::rancor::{Panic, Strategy};
use rkyv::ser::allocator::{AllocationTracker, GlobalAllocator};
use rkyv::ser::{AllocSerializer, Composite};
use rkyv::util::AlignedVec;
use rkyv::{Archive, Deserialize, Serialize};
use crate::common::types::{Event, RoleIdentifier};
pub trait RkyvSerializable = rkyv::Serialize<
Strategy<Composite<AlignedVec, AllocationTracker<GlobalAllocator>, Panic>, Panic>,
> + Serialize<Strategy<AllocSerializer<256>, Panic>>;
pub trait CallArgument = Sized + RkyvSerializable;
pub trait CallReturn = ?Sized + Clone + Default + RkyvSerializable + Archive;
/// A data struct that is aware of it's own ID
pub trait SelfIdentify {
fn get_self_identity(&self) -> RoleIdentifier;
#[allow(dead_code)]
fn set_self_identity(&mut self, id: RoleIdentifier);
}
/// `Call` trait provides methods `send` & `receive` to use an
/// underlying type as a message-passing system.
pub trait Call: SelfIdentify {
/// `send` emulates a function call to the `resolver` with
/// `argument` args and returns the value returned by it.
/// Under the hood, wherever required, it uses `rkyv` for
/// deserialization. This func never serializes in `mozakvm`.
fn send<A, R>(
&mut self,
recipient: RoleIdentifier,
argument: A,
resolver: impl Fn(A) -> R,
) -> R
where
A: CallArgument + PartialEq,
R: CallReturn,
<A as Archive>::Archived: Deserialize<A, Strategy<(), Panic>>,
<R as Archive>::Archived: Deserialize<R, Strategy<(), Panic>>;
/// `receive` emulates a function call directed towards the
/// program, presents back with a three tuple of the form
/// `(P, A, R)` where `P` is the identifier of the caller
/// program, `A` the arguments they presented and `R` being
/// the result that they want us to ensure is correct.
/// Under the hood, wherever required, it uses `rkyv` for
/// deserialization. This func never serializes in `mozakvm`.
fn receive<A, R>(&mut self) -> Option<(RoleIdentifier, A, R)>
where
A: CallArgument + PartialEq,
R: CallReturn,
<A as Archive>::Archived: Deserialize<A, Strategy<(), Panic>>,
<R as Archive>::Archived: Deserialize<R, Strategy<(), Panic>>;
}
/// `EventEmit` trait provides method `emit` to use the underlying
/// tape as an output device
pub trait EventEmit: SelfIdentify {
/// `emit` emulates an output device write
fn emit(&mut self, event: Event);
}