Skip to main content

bus_contract/
contract.rs

1//! Capability contracts as data: [`Contract`], what a point needs from a
2//! bus, and [`Facet`], what a point's declaration says for one contract.
3//! The core names no capability: each capability contract crate exports
4//! its `CONTRACT` and its facets.
5
6use std::any::{Any, TypeId};
7use std::fmt;
8
9use crate::Bus;
10
11/// A capability contract a bus may offer: its name, its version line and
12/// the trait object type a bus answers for in [`Bus::capability`]. Each
13/// contract crate exports one as `CONTRACT`
14/// (`bus_contract_journal::CONTRACT`); a point names those it needs in
15/// its table ([`PointDecl::needs`](crate::PointDecl::needs)) and
16/// [`declare_all`](crate::declare_all) refuses it on a bus without them.
17///
18/// Two contracts are the same when their trait object types are: two
19/// semver-incompatible versions of a contract crate are two contracts,
20/// so a library on one is refused, with a reason, by a bus that
21/// implements the other.
22#[derive(Clone, Copy)]
23pub struct Contract {
24    /// The contract's name (`journal`), for messages and introspection.
25    pub name: &'static str,
26    /// The version line of the crate that defines it (`0.2`).
27    pub version: &'static str,
28    /// The contracts a bus that offers this one offers too (crossing
29    /// requires journal); [`offers`] checks them.
30    pub requires: &'static [&'static Contract],
31    type_id: fn() -> TypeId,
32}
33
34impl Contract {
35    /// The contract `name`, version line `version`, offered by a bus that
36    /// answers for the trait object type `C` (`dyn Journal`).
37    pub const fn new<C: ?Sized + 'static>(
38        name: &'static str,
39        version: &'static str,
40        requires: &'static [&'static Contract],
41    ) -> Contract {
42        Contract {
43            name,
44            version,
45            requires,
46            type_id: TypeId::of::<C>,
47        }
48    }
49
50    /// The `TypeId` a bus answers for in [`Bus::capability`].
51    pub fn type_id(&self) -> TypeId {
52        (self.type_id)()
53    }
54}
55
56impl PartialEq for Contract {
57    fn eq(&self, other: &Contract) -> bool {
58        self.type_id() == other.type_id()
59    }
60}
61
62impl Eq for Contract {}
63
64impl fmt::Debug for Contract {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        write!(f, "{} {}", self.name, self.version)
67    }
68}
69
70impl fmt::Display for Contract {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        f.write_str(self.name)
73    }
74}
75
76/// Whether `bus` offers `contract`: it answers the lookup for the
77/// contract's type ([`Bus::capability`]), and offers every contract it
78/// requires.
79pub fn offers(bus: &dyn Bus, contract: &Contract) -> bool {
80    bus.capability(contract.type_id()).is_some() && contract.requires.iter().all(|r| offers(bus, r))
81}
82
83/// What a point's declaration says for one capability contract: the
84/// contract, and data only that contract's crate reads (a journaled
85/// mark, a codec). Made by the contract crate, in a `const`
86/// (`bus_contract_journal::JOURNALED`,
87/// `bus_contract_crossing::relayable(&CODEC)`), and carried by
88/// [`PointDecl::facets`](crate::PointDecl::facets) and
89/// [`PointSpec::facets`](crate::PointSpec::facets). A point with a facet
90/// needs its contract.
91#[derive(Clone, Copy)]
92pub struct Facet {
93    /// The contract the facet belongs to.
94    pub contract: &'static Contract,
95    /// What it says, read back by the contract's crate ([`Facet::get`]).
96    pub data: &'static (dyn Any + Send + Sync),
97}
98
99impl Facet {
100    /// A facet of `contract` saying `data`.
101    pub const fn new(contract: &'static Contract, data: &'static (dyn Any + Send + Sync)) -> Facet {
102        Facet { contract, data }
103    }
104
105    /// The facet's data as a `T`; `None` when it is another type.
106    pub fn get<T: Any>(&self) -> Option<&'static T> {
107        self.data.downcast_ref::<T>()
108    }
109
110    /// The first facet among `facets` of `contract` whose data is a `T`.
111    pub fn find<T: Any>(facets: &[Facet], contract: &Contract) -> Option<&'static T> {
112        facets
113            .iter()
114            .filter(|f| f.contract == contract)
115            .find_map(Facet::get::<T>)
116    }
117}
118
119/// Two facets are the same when they belong to the same contract and
120/// carry data of the same type.
121impl PartialEq for Facet {
122    fn eq(&self, other: &Facet) -> bool {
123        self.contract == other.contract && (*self.data).type_id() == (*other.data).type_id()
124    }
125}
126
127impl Eq for Facet {}
128
129impl fmt::Debug for Facet {
130    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131        write!(f, "Facet({})", self.contract.name)
132    }
133}