Function create_action
pub fn create_action<I, O, F, Fu>(action_fn: F) -> Action<I, O>
👎Deprecated: This function is being removed to conform to Rust idioms. Please use
Action::new()
instead.Expand description
Creates a new action. This is lazy: it does not run the action function until some value is dispatched.
The constructor takes a function which will create a new Future
from some input data.
When the action is dispatched, this action_fn
will run, and the Future
it returns will
be spawned.
The action_fn
must be Send + Sync
so that the ArcAction
is Send + Sync
. The
Future
must be Send
so that it can be moved across threads by the async executor as
needed. In order to be stored in the Copy
arena, the input and output types should also
be Send + Sync
.
let act = Action::new(|n: &u8| {
let n = n.to_owned();
async move { n * 2 }
});
act.dispatch(3);
assert_eq!(act.input().get(), Some(3));
// Remember that async functions already return a future if they are
// not `await`ed. You can save keystrokes by leaving out the `async move`
let act2 = Action::new(|n: &String| yell(n.to_owned()));
act2.dispatch(String::from("i'm in a doctest"));
// after it resolves
assert_eq!(act2.value().get(), Some("I'M IN A DOCTEST".to_string()));
async fn yell(n: String) -> String {
n.to_uppercase()
}