Skip to content

Commit

Permalink
linera-{execution,witty}: allow non-Send types in UserData`
Browse files Browse the repository at this point in the history
  • Loading branch information
Twey committed May 15, 2024
1 parent ec7e24f commit 91b1bb4
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 67 deletions.
21 changes: 7 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,8 @@ opt-level = 3
version = "0.4.1"
git = "https://github.com/Twey/rust-indexed-db"
branch = "no-uuid-wasm-bindgen"

[patch.crates-io.wasmer]
version = "4.3.0-alpha.1"
git = "https://github.com/Twey/wasmer"
branch = "non-send-environments"
21 changes: 7 additions & 14 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ strip = 'debuginfo'

[profile.bench]
debug = true

[patch.crates-io.wasmer]
version = "4.3.0-alpha.1"
git = "https://github.com/Twey/wasmer"
branch = "non-send-environments"
5 changes: 5 additions & 0 deletions linera-execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,8 @@ cfg_aliases.workspace = true

[package.metadata.cargo-machete]
ignored = ["serde_bytes"]

[patch.crates-io.wasmer]
version = "4.3.0-alpha.1"
git = "https://github.com/Twey/wasmer"
branch = "non-send-environments"
4 changes: 2 additions & 2 deletions linera-execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ pub type UserContractCode = Arc<dyn UserContractModule + Send + Sync + 'static>;
pub type UserServiceCode = Arc<dyn UserServiceModule + Send + Sync + 'static>;

/// An implementation of [`UserContract`].
pub type UserContractInstance = Box<dyn UserContract + Send + Sync + 'static>;
pub type UserContractInstance = Box<dyn UserContract + 'static>;

/// An implementation of [`UserService`].
pub type UserServiceInstance = Box<dyn UserService + Send + Sync + 'static>;
pub type UserServiceInstance = Box<dyn UserService + 'static>;

/// A factory trait to obtain a [`UserContract`] from a [`UserContractModule`]
pub trait UserContractModule {
Expand Down
4 changes: 2 additions & 2 deletions linera-execution/src/test_utils/mock_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl UserContractModule for MockApplication {
fn instantiate(
&self,
runtime: ContractSyncRuntime,
) -> Result<Box<dyn UserContract + Send + Sync + 'static>, ExecutionError> {
) -> Result<Box<dyn UserContract + 'static>, ExecutionError> {
Ok(Box::new(self.create_mock_instance(runtime)))
}
}
Expand All @@ -204,7 +204,7 @@ impl UserServiceModule for MockApplication {
fn instantiate(
&self,
runtime: ServiceSyncRuntime,
) -> Result<Box<dyn UserService + Send + Sync + 'static>, ExecutionError> {
) -> Result<Box<dyn UserService + 'static>, ExecutionError> {
Ok(Box::new(self.create_mock_instance(runtime)))
}
}
Expand Down
6 changes: 3 additions & 3 deletions linera-execution/src/wasm/system_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub struct ContractSystemApi<Caller>(PhantomData<Caller>);
impl<Caller, Runtime> ContractSystemApi<Caller>
where
Caller: Instance<UserData = SystemApiData<Runtime>>,
Runtime: ContractRuntime + Send + 'static,
Runtime: ContractRuntime + 'static,
{
/// Returns the ID of the current chain.
fn get_chain_id(caller: &mut Caller) -> Result<ChainId, RuntimeError> {
Expand Down Expand Up @@ -354,7 +354,7 @@ pub struct ServiceSystemApi<Caller>(PhantomData<Caller>);
impl<Caller, Runtime> ServiceSystemApi<Caller>
where
Caller: Instance<UserData = SystemApiData<Runtime>>,
Runtime: ServiceRuntime + Send + 'static,
Runtime: ServiceRuntime + 'static,
{
/// Returns the ID of the current chain.
fn get_chain_id(caller: &mut Caller) -> Result<ChainId, RuntimeError> {
Expand Down Expand Up @@ -503,7 +503,7 @@ pub struct ViewSystemApi<Caller>(PhantomData<Caller>);
impl<Caller, Runtime> ViewSystemApi<Caller>
where
Caller: Instance<UserData = SystemApiData<Runtime>>,
Runtime: BaseRuntime + WriteBatch + Send + 'static,
Runtime: BaseRuntime + WriteBatch + 'static,
{
/// Creates a new promise to check if the `key` is in storage.
fn contains_key_new(caller: &mut Caller, key: Vec<u8>) -> Result<u32, RuntimeError> {
Expand Down
18 changes: 4 additions & 14 deletions linera-execution/src/wasm/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ use crate::{
QueryContext, ServiceRuntime,
};

#[cfg(not(web))]
use {
wasmer::sys::EngineBuilder,
wasmer::Cranelift,
wasmer::Singlepass,
};

/// An [`Engine`] instance configured to run application services.
static SERVICE_ENGINE: Lazy<Engine> = Lazy::new(|| {
#[cfg(web)]
Expand Down Expand Up @@ -79,7 +72,7 @@ impl WasmContractModule {

impl<Runtime> WasmerContractInstance<Runtime>
where
Runtime: ContractRuntime + WriteBatch + Clone + Send + Sync + Unpin + 'static,
Runtime: ContractRuntime + WriteBatch + Clone + Unpin + 'static,
{
/// Prepares a runtime instance to call into the Wasm contract.
pub fn prepare(
Expand Down Expand Up @@ -114,7 +107,7 @@ impl WasmServiceModule {

impl<Runtime> WasmerServiceInstance<Runtime>
where
Runtime: ServiceRuntime + WriteBatch + Clone + Send + Sync + Unpin + 'static,
Runtime: ServiceRuntime + WriteBatch + Clone + Unpin + 'static,
{
/// Prepares a runtime instance to call into the Wasm service.
pub fn prepare(service_module: &Module, runtime: Runtime) -> Result<Self, WasmExecutionError> {
Expand All @@ -132,7 +125,7 @@ where

impl<Runtime> crate::UserContract for WasmerContractInstance<Runtime>
where
Runtime: ContractRuntime + Send + Unpin + 'static,
Runtime: ContractRuntime + Unpin + 'static,
{
fn instantiate(
&mut self,
Expand Down Expand Up @@ -174,10 +167,7 @@ where
}
}

impl<Runtime> crate::UserService for WasmerServiceInstance<Runtime>
where
Runtime: Send + 'static,
{
impl<Runtime: 'static> crate::UserService for WasmerServiceInstance<Runtime> {
fn handle_query(
&mut self,
_context: QueryContext,
Expand Down
2 changes: 1 addition & 1 deletion linera-witty/src/runtime/wasmer/export_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ macro_rules! export_function {
where
$( $types: FromToNativeWasmType, )*
FlatResult: MaybeFlatType + WasmTypeList,
UserData: Send + 'static,
UserData: 'static,
HandlerError: Error + Send + Sync + 'static,
Handler:
Fn(
Expand Down
2 changes: 1 addition & 1 deletion linera-witty/src/runtime/wasmer/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ macro_rules! impl_instance_with_function_for {
where
$( $types: FlatType + FromToNativeWasmType + NativeWasmTypeInto, )*
Results: FlatLayout + WasmerResults,
UserData: Send + 'static,
UserData: 'static,
{
type Function = TypedFunction<
<HList![$( $types ),*] as WasmerParameters>::ImportParameters,
Expand Down
10 changes: 2 additions & 8 deletions linera-witty/src/runtime/wasmer/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ use crate::{GuestPointer, RuntimeError, RuntimeMemory};

macro_rules! impl_memory_traits {
($instance:ty) => {
impl<UserData> InstanceWithMemory for $instance
where
UserData: Send + 'static,
{
impl<UserData: 'static> InstanceWithMemory for $instance {
fn memory_from_export(&self, export: Extern) -> Result<Option<Memory>, RuntimeError> {
Ok(match export {
Extern::Memory(memory) => Some(memory),
Expand All @@ -24,10 +21,7 @@ macro_rules! impl_memory_traits {
}
}

impl<UserData> RuntimeMemory<$instance> for Memory
where
UserData: Send + 'static,
{
impl<UserData: 'static> RuntimeMemory<$instance> for Memory {
fn read<'instance>(
&self,
instance: &'instance $instance,
Expand Down
10 changes: 2 additions & 8 deletions linera-witty/src/runtime/wasmer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ pub struct InstanceBuilder<UserData> {
environment: Environment<UserData>,
}

impl<UserData> InstanceBuilder<UserData>
where
UserData: Send + 'static,
{
impl<UserData: 'static> InstanceBuilder<UserData> {
/// Creates a new [`InstanceBuilder`].
pub fn new(engine: Engine, user_data: UserData) -> Self {
InstanceBuilder {
Expand Down Expand Up @@ -160,10 +157,7 @@ impl<UserData> Instance for EntrypointInstance<UserData> {
/// guest.
pub type ReentrantInstance<'a, UserData> = FunctionEnvMut<'a, Environment<UserData>>;

impl<UserData> Instance for ReentrantInstance<'_, UserData>
where
UserData: Send + 'static,
{
impl<UserData: 'static> Instance for ReentrantInstance<'_, UserData> {
type Runtime = Wasmer;
type UserData = UserData;
type UserDataReference<'a> = MutexGuard<'a, UserData>
Expand Down

0 comments on commit 91b1bb4

Please sign in to comment.