linera_base/
abi.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! This module defines the notion of Application Binary Interface (ABI) for Linera
5//! applications across Wasm and native architectures.
6
7use std::fmt::Debug;
8
9use serde::{de::DeserializeOwned, Serialize};
10
11// ANCHOR: abi
12/// A trait that includes all the types exported by a Linera application (both contract
13/// and service).
14pub trait Abi: ContractAbi + ServiceAbi {}
15// ANCHOR_END: abi
16
17// T::Parameters is duplicated for simplicity but it must match.
18impl<T> Abi for T where T: ContractAbi + ServiceAbi {}
19
20// ANCHOR: contract_abi
21/// A trait that includes all the types exported by a Linera application contract.
22pub trait ContractAbi {
23    /// The type of operation executed by the application.
24    ///
25    /// Operations are transactions directly added to a block by the creator (and signer)
26    /// of the block. Users typically use operations to start interacting with an
27    /// application on their own chain.
28    type Operation: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
29
30    /// The response type of an application call.
31    type Response: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
32}
33// ANCHOR_END: contract_abi
34
35// ANCHOR: service_abi
36/// A trait that includes all the types exported by a Linera application service.
37pub trait ServiceAbi {
38    /// The type of a query receivable by the application's service.
39    type Query: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
40
41    /// The response type of the application's service.
42    type QueryResponse: Serialize + DeserializeOwned + Send + Sync + Debug + 'static;
43}
44// ANCHOR_END: service_abi
45
46/// Marker trait to help importing contract types.
47pub trait WithContractAbi {
48    /// The contract types to import.
49    type Abi: ContractAbi;
50}
51
52impl<A> ContractAbi for A
53where
54    A: WithContractAbi,
55{
56    type Operation = <<A as WithContractAbi>::Abi as ContractAbi>::Operation;
57    type Response = <<A as WithContractAbi>::Abi as ContractAbi>::Response;
58}
59
60/// Marker trait to help importing service types.
61pub trait WithServiceAbi {
62    /// The service types to import.
63    type Abi: ServiceAbi;
64}
65
66impl<A> ServiceAbi for A
67where
68    A: WithServiceAbi,
69{
70    type Query = <<A as WithServiceAbi>::Abi as ServiceAbi>::Query;
71    type QueryResponse = <<A as WithServiceAbi>::Abi as ServiceAbi>::QueryResponse;
72}