@@ -84,7 +84,6 @@ pub use crate::{exec::Ext, Config};
84
84
pub use frame_system:: Config as SysConfig ;
85
85
pub use pallet_contracts_primitives:: ReturnFlags ;
86
86
pub use sp_core:: crypto:: UncheckedFrom ;
87
- pub use state:: Init as InitState ;
88
87
89
88
/// Result that returns a [`DispatchError`] on error.
90
89
pub type Result < T > = sp_std:: result:: Result < T , DispatchError > ;
@@ -198,15 +197,15 @@ pub enum RetVal {
198
197
///
199
198
/// It uses [typestate programming](https://docs.rust-embedded.org/book/static-guarantees/typestate-programming.html)
200
199
/// 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 > {
202
201
/// The actual data of this type.
203
202
inner : Inner < ' a , ' b , E > ,
204
203
/// `S` is only used in the type system but never as value.
205
204
phantom : PhantomData < S > ,
206
205
}
207
206
208
207
/// 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 >
210
209
where
211
210
<E :: T as SysConfig >:: AccountId : UncheckedFrom < <E :: T as SysConfig >:: Hash > + AsRef < [ u8 ] > ,
212
211
{
@@ -264,7 +263,7 @@ where
264
263
///
265
264
/// Those are the functions that determine how the arguments to the chain extensions
266
265
/// 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 > {
268
267
/// Creates a new environment for consumption by a chain extension.
269
268
///
270
269
/// 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> {
284
283
}
285
284
286
285
/// 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 > {
288
287
Environment { inner : self . inner , phantom : PhantomData }
289
288
}
290
289
291
290
/// 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 > {
293
292
Environment { inner : self . inner , phantom : PhantomData }
294
293
}
295
294
296
295
/// 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 > {
298
297
Environment { inner : self . inner , phantom : PhantomData }
299
298
}
300
299
}
301
300
302
301
/// 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 > {
304
303
/// The `input_ptr` argument.
305
304
pub fn val0 ( & self ) -> u32 {
306
305
self . inner . input_ptr
@@ -313,7 +312,7 @@ impl<'a, 'b, E: Ext, S: state::PrimIn> Environment<'a, 'b, E, S> {
313
312
}
314
313
315
314
/// 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 > {
317
316
/// The `output_ptr` argument.
318
317
pub fn val2 ( & self ) -> u32 {
319
318
self . inner . output_ptr
@@ -326,7 +325,7 @@ impl<'a, 'b, E: Ext, S: state::PrimOut> Environment<'a, 'b, E, S> {
326
325
}
327
326
328
327
/// 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 >
330
329
where
331
330
<E :: T as SysConfig >:: AccountId : UncheckedFrom < <E :: T as SysConfig >:: Hash > + AsRef < [ u8 ] > ,
332
331
{
@@ -389,7 +388,7 @@ where
389
388
}
390
389
391
390
/// 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 >
393
392
where
394
393
<E :: T as SysConfig >:: AccountId : UncheckedFrom < <E :: T as SysConfig >:: Hash > + AsRef < [ u8 ] > ,
395
394
{
@@ -438,31 +437,54 @@ struct Inner<'a, 'b, E: Ext> {
438
437
output_len_ptr : u32 ,
439
438
}
440
439
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 { }
468
490
}
0 commit comments