pub trait Router<S, I = Self>{
type Storage;
// Required methods
fn route(storage: &mut S, selector: u32, input: &[u8]) -> Option<ArbResult>;
fn receive(storage: &mut S) -> Option<Result<(), Vec<u8>>>;
fn fallback(storage: &mut S, calldata: &[u8]) -> Option<ArbResult>;
fn constructor(storage: &mut S, calldata: &[u8]) -> Option<ArbResult>;
}Expand description
Executes a method given a selector and calldata.
This trait can be automatically implemented via #[public].
Composition with other routers is possible via #[inherit].
Required Associated Types§
Sourcetype Storage
type Storage
The type the TopLevelStorage borrows into. Usually just Self.
Required Methods§
Sourcefn route(storage: &mut S, selector: u32, input: &[u8]) -> Option<ArbResult>
fn route(storage: &mut S, selector: u32, input: &[u8]) -> Option<ArbResult>
Tries to find and execute a method for the given selector, returning None if none is
found. Routes add via #[inherit] will only execute if no match is found among Self.
This means that it is possible to override a method by redefining it in Self.
Sourcefn receive(storage: &mut S) -> Option<Result<(), Vec<u8>>>
fn receive(storage: &mut S) -> Option<Result<(), Vec<u8>>>
Receive function for this contract. Called when no calldata is provided. A receive function may not be defined, in which case this method will return None. Receive functions are always payable, take in no inputs, and return no outputs. If defined, they will always be called when a transaction does not send any calldata, regardless of the transaction having a value attached.
Sourcefn fallback(storage: &mut S, calldata: &[u8]) -> Option<ArbResult>
fn fallback(storage: &mut S, calldata: &[u8]) -> Option<ArbResult>
Called when no receive function is defined or when the transaction has calldata but it doesn’t match any function selector. If no #[fallback] function is defined in the contract, then any transactions with calldata that do not match a selector will revert. A fallback function may have two different implementations. It can be either declared without any input or output, or with bytes input calldata and bytes output. If a user defines a fallback function with no input or output, then this method will be called and the underlying user-defined function will simply be invoked with no input. A fallback function can be declared as payable. If not payable, then any transactions that trigger a fallback with value attached will revert.
Sourcefn constructor(storage: &mut S, calldata: &[u8]) -> Option<ArbResult>
fn constructor(storage: &mut S, calldata: &[u8]) -> Option<ArbResult>
The router_entrypoint calls the constructor when the selector is CONSTRUCTOR_SELECTOR. The implementation should: decode the calldata and pass the parameters to the user-defined constructor; and call internal::constructor_guard to ensure it is only executed once. Since each constructor has its own set of parameters, this function won’t call the constructors for inherited structs automatically. Instead, the user-defined function should call the base classes constructors. A constructor function can be declared as payable. If not payable, then any transactions that trigger the constructor with value attached will revert.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".