Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit e7aa858

Browse files
authored
Make state types public (#12032)
1 parent 2cb89be commit e7aa858

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

frame/contracts/src/chain_extension.rs

+60-38
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ pub use crate::{exec::Ext, Config};
8484
pub use frame_system::Config as SysConfig;
8585
pub use pallet_contracts_primitives::ReturnFlags;
8686
pub use sp_core::crypto::UncheckedFrom;
87-
pub use state::Init as InitState;
8887

8988
/// Result that returns a [`DispatchError`] on error.
9089
pub type Result<T> = sp_std::result::Result<T, DispatchError>;
@@ -198,15 +197,15 @@ pub enum RetVal {
198197
///
199198
/// It uses [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html)
200199
/// to enforce the correct usage of the parameters passed to the chain extension.
201-
pub struct Environment<'a, 'b, E: Ext, S: state::State> {
200+
pub struct Environment<'a, 'b, E: Ext, S: State> {
202201
/// The actual data of this type.
203202
inner: Inner<'a, 'b, E>,
204203
/// `S` is only used in the type system but never as value.
205204
phantom: PhantomData<S>,
206205
}
207206

208207
/// Functions that are available in every state of this type.
209-
impl<'a, 'b, E: Ext, S: state::State> Environment<'a, 'b, E, S>
208+
impl<'a, 'b, E: Ext, S: State> Environment<'a, 'b, E, S>
210209
where
211210
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
212211
{
@@ -264,7 +263,7 @@ where
264263
///
265264
/// Those are the functions that determine how the arguments to the chain extensions
266265
/// should be consumed.
267-
impl<'a, 'b, E: Ext> Environment<'a, 'b, E, state::Init> {
266+
impl<'a, 'b, E: Ext> Environment<'a, 'b, E, InitState> {
268267
/// Creates a new environment for consumption by a chain extension.
269268
///
270269
/// It is only available to this crate because only the wasm runtime module needs to
@@ -284,23 +283,23 @@ impl<'a, 'b, E: Ext> Environment<'a, 'b, E, state::Init> {
284283
}
285284

286285
/// Use all arguments as integer values.
287-
pub fn only_in(self) -> Environment<'a, 'b, E, state::OnlyIn> {
286+
pub fn only_in(self) -> Environment<'a, 'b, E, OnlyInState> {
288287
Environment { inner: self.inner, phantom: PhantomData }
289288
}
290289

291290
/// Use input arguments as integer and output arguments as pointer to a buffer.
292-
pub fn prim_in_buf_out(self) -> Environment<'a, 'b, E, state::PrimInBufOut> {
291+
pub fn prim_in_buf_out(self) -> Environment<'a, 'b, E, PrimInBufOutState> {
293292
Environment { inner: self.inner, phantom: PhantomData }
294293
}
295294

296295
/// Use input and output arguments as pointers to a buffer.
297-
pub fn buf_in_buf_out(self) -> Environment<'a, 'b, E, state::BufInBufOut> {
296+
pub fn buf_in_buf_out(self) -> Environment<'a, 'b, E, BufInBufOutState> {
298297
Environment { inner: self.inner, phantom: PhantomData }
299298
}
300299
}
301300

302301
/// Functions to use the input arguments as integers.
303-
impl<'a, 'b, E: Ext, S: state::PrimIn> Environment<'a, 'b, E, S> {
302+
impl<'a, 'b, E: Ext, S: PrimIn> Environment<'a, 'b, E, S> {
304303
/// The `input_ptr` argument.
305304
pub fn val0(&self) -> u32 {
306305
self.inner.input_ptr
@@ -313,7 +312,7 @@ impl<'a, 'b, E: Ext, S: state::PrimIn> Environment<'a, 'b, E, S> {
313312
}
314313

315314
/// Functions to use the output arguments as integers.
316-
impl<'a, 'b, E: Ext, S: state::PrimOut> Environment<'a, 'b, E, S> {
315+
impl<'a, 'b, E: Ext, S: PrimOut> Environment<'a, 'b, E, S> {
317316
/// The `output_ptr` argument.
318317
pub fn val2(&self) -> u32 {
319318
self.inner.output_ptr
@@ -326,7 +325,7 @@ impl<'a, 'b, E: Ext, S: state::PrimOut> Environment<'a, 'b, E, S> {
326325
}
327326

328327
/// Functions to use the input arguments as pointer to a buffer.
329-
impl<'a, 'b, E: Ext, S: state::BufIn> Environment<'a, 'b, E, S>
328+
impl<'a, 'b, E: Ext, S: BufIn> Environment<'a, 'b, E, S>
330329
where
331330
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
332331
{
@@ -389,7 +388,7 @@ where
389388
}
390389

391390
/// Functions to use the output arguments as pointer to a buffer.
392-
impl<'a, 'b, E: Ext, S: state::BufOut> Environment<'a, 'b, E, S>
391+
impl<'a, 'b, E: Ext, S: BufOut> Environment<'a, 'b, E, S>
393392
where
394393
<E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,
395394
{
@@ -438,31 +437,54 @@ struct Inner<'a, 'b, E: Ext> {
438437
output_len_ptr: u32,
439438
}
440439

441-
/// Private submodule with public types to prevent other modules from naming them.
442-
mod state {
443-
pub trait State {}
444-
445-
pub trait PrimIn: State {}
446-
pub trait PrimOut: State {}
447-
pub trait BufIn: State {}
448-
pub trait BufOut: State {}
449-
450-
/// The initial state of an [`Environment`](`super::Environment`).
451-
/// See [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html).
452-
pub enum Init {}
453-
pub enum OnlyIn {}
454-
pub enum PrimInBufOut {}
455-
pub enum BufInBufOut {}
456-
457-
impl State for Init {}
458-
impl State for OnlyIn {}
459-
impl State for PrimInBufOut {}
460-
impl State for BufInBufOut {}
461-
462-
impl PrimIn for OnlyIn {}
463-
impl PrimOut for OnlyIn {}
464-
impl PrimIn for PrimInBufOut {}
465-
impl BufOut for PrimInBufOut {}
466-
impl BufIn for BufInBufOut {}
467-
impl BufOut for BufInBufOut {}
440+
/// Any state of an [`Environment`] implements this trait.
441+
/// See [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html).
442+
pub trait State: sealed::Sealed {}
443+
444+
/// A state that uses primitive inputs.
445+
pub trait PrimIn: State {}
446+
447+
/// A state that uses primitive outputs.
448+
pub trait PrimOut: State {}
449+
450+
/// A state that uses a buffer as input.
451+
pub trait BufIn: State {}
452+
453+
/// A state that uses a buffer as output.
454+
pub trait BufOut: State {}
455+
456+
/// The initial state of an [`Environment`].
457+
pub enum InitState {}
458+
459+
/// A state that uses all arguments as primitive inputs.
460+
pub enum OnlyInState {}
461+
462+
/// A state that uses two arguments as primitive inputs and the other two as buffer output.
463+
pub enum PrimInBufOutState {}
464+
465+
/// Uses a buffer for input and a buffer for output.
466+
pub enum BufInBufOutState {}
467+
468+
mod sealed {
469+
use super::*;
470+
471+
/// Trait to prevent users from implementing `State` for anything else.
472+
pub trait Sealed {}
473+
474+
impl Sealed for InitState {}
475+
impl Sealed for OnlyInState {}
476+
impl Sealed for PrimInBufOutState {}
477+
impl Sealed for BufInBufOutState {}
478+
479+
impl State for InitState {}
480+
impl State for OnlyInState {}
481+
impl State for PrimInBufOutState {}
482+
impl State for BufInBufOutState {}
483+
484+
impl PrimIn for OnlyInState {}
485+
impl PrimOut for OnlyInState {}
486+
impl PrimIn for PrimInBufOutState {}
487+
impl BufOut for PrimInBufOutState {}
488+
impl BufIn for BufInBufOutState {}
489+
impl BufOut for BufInBufOutState {}
468490
}

0 commit comments

Comments
 (0)