linera_wasmer_compiler/
traits.rs

1//! Generic Artifact abstraction for Wasmer Engines.
2
3use crate::Features;
4use enumset::EnumSet;
5use std::any::Any;
6use std::sync::Arc;
7use wasmer_types::entity::PrimaryMap;
8use wasmer_types::SerializeError;
9use wasmer_types::{
10    CpuFeature, DataInitializerLike, MemoryIndex, MemoryStyle, ModuleInfo, TableIndex, TableStyle,
11};
12
13/// An `Artifact` is the product that the `Engine`
14/// implementation produce and use.
15///
16/// The `Artifact` contains the compiled data for a given
17/// module as well as extra information needed to run the
18/// module at runtime, such as [`ModuleInfo`] and [`Features`].
19pub trait ArtifactCreate<'a>: Send + Sync + Upcastable {
20    /// Type of `OwnedDataInitializer` returned by the `data_initializers` method
21    type OwnedDataInitializer: DataInitializerLike<'a> + 'a;
22    /// Type of iterator returned by the `data_initializers` method
23    type OwnedDataInitializerIterator: Iterator<Item = Self::OwnedDataInitializer>;
24
25    /// Create a `ModuleInfo` for instantiation
26    fn create_module_info(&'a self) -> Arc<ModuleInfo>;
27
28    /// Sets the `ModuleInfo` name
29    fn set_module_info_name(&'a mut self, name: String) -> bool;
30
31    /// Returns the `ModuleInfo` for instantiation
32    fn module_info(&'a self) -> &ModuleInfo;
33
34    /// Returns the features for this Artifact
35    fn features(&'a self) -> &Features;
36
37    /// Returns the CPU features for this Artifact
38    fn cpu_features(&'a self) -> EnumSet<CpuFeature>;
39
40    /// Returns the memory styles associated with this `Artifact`.
41    fn memory_styles(&'a self) -> &PrimaryMap<MemoryIndex, MemoryStyle>;
42
43    /// Returns the table plans associated with this `Artifact`.
44    fn table_styles(&'a self) -> &PrimaryMap<TableIndex, TableStyle>;
45
46    /// Returns data initializers to pass to `VMInstance::initialize`
47    fn data_initializers(&'a self) -> Self::OwnedDataInitializerIterator;
48
49    /// Serializes an artifact into bytes
50    fn serialize(&'a self) -> Result<Vec<u8>, SerializeError>;
51}
52
53// Implementation of `Upcastable` taken from https://users.rust-lang.org/t/why-does-downcasting-not-work-for-subtraits/33286/7 .
54/// Trait needed to get downcasting of `Engine`s to work.
55pub trait Upcastable {
56    /// upcast ref
57    fn upcast_any_ref(&'_ self) -> &'_ dyn Any;
58    /// upcast mut ref
59    fn upcast_any_mut(&'_ mut self) -> &'_ mut dyn Any;
60    /// upcast boxed dyn
61    fn upcast_any_box(self: Box<Self>) -> Box<dyn Any>;
62}
63
64impl<T: Any + Send + Sync + 'static> Upcastable for T {
65    #[inline]
66    fn upcast_any_ref(&'_ self) -> &'_ dyn Any {
67        self
68    }
69    #[inline]
70    fn upcast_any_mut(&'_ mut self) -> &'_ mut dyn Any {
71        self
72    }
73    #[inline]
74    fn upcast_any_box(self: Box<Self>) -> Box<dyn Any> {
75        self
76    }
77}
78
79impl<'a, O, I>
80    dyn ArtifactCreate<'a, OwnedDataInitializer = O, OwnedDataInitializerIterator = I> + 'static
81{
82    /// Try to downcast the artifact into a given type.
83    #[inline]
84    pub fn downcast_ref<T: 'static>(&'a self) -> Option<&'a T> {
85        self.upcast_any_ref().downcast_ref::<T>()
86    }
87
88    /// Try to downcast the artifact into a given type mutably.
89    #[inline]
90    pub fn downcast_mut<T: 'static>(&'a mut self) -> Option<&'a mut T> {
91        self.upcast_any_mut().downcast_mut::<T>()
92    }
93}