pub struct Invoker<'a, Set, Ser> { /* private fields */ }
Expand description
Keep the variable length arguments handler in HashMap
with key Uid
.
§Example
pub struct Count(usize);
// implement Extract for your type
impl Extract<ASet, ASer> for Count {
type Error = Error;
fn extract(_set: &ASet, _ser: &ASer, ctx: &Ctx) -> Result<Self, Self::Error> {
Ok(Self(ctx.args().len()))
}
}
let mut ser = ASer::default();
let mut is = Invoker::new();
let mut set = ASet::default();
let args = ARef::new(Args::from(["--foo", "bar", "doo"]));
let mut ctx = Ctx::default().with_args(args);
ser.sve_insert(ser::Value::new(42i64));
// you can register callback into Invoker
is.entry(0)
.on(
|_set: &mut ASet, _: &mut ASer| -> Result<Option<()>, Error> {
println!("Calling the handler of {{0}}");
Ok(None)
},
)
.then(NullStore);
is.entry(1)
.on(
|_set: &mut ASet, _: &mut ASer, cnt: Count| -> Result<Option<()>, Error> {
println!("Calling the handler of {{1}}");
assert_eq!(cnt.0, 3);
Ok(None)
},
)
.then(NullStore);
is.entry(2)
.on(
|_set: &mut ASet, _: &mut ASer, data: ser::Value<i64>| -> Result<Option<()>, Error> {
println!("Calling the handler of {{2}}");
assert_eq!(data.as_ref(), &42);
Ok(None)
},
)
.then(NullStore);
ctx.set_inner_ctx(Some(InnerCtx::default().with_uid(0)));
is.invoke(&0, &mut set, &mut ser, &mut ctx)?;
ctx.set_inner_ctx(Some(InnerCtx::default().with_uid(1)));
is.invoke(&1, &mut set, &mut ser, &mut ctx)?;
ctx.set_inner_ctx(Some(InnerCtx::default().with_uid(2)));
is.invoke(&2, &mut set, &mut ser, &mut ctx)?;
Implementations§
source§impl<'a, Set, Ser> Invoker<'a, Set, Ser>where
Set: Set,
impl<'a, Set, Ser> Invoker<'a, Set, Ser>where
Set: Set,
pub fn set_raw<H: FnMut(&mut Set, &mut Ser, &mut Ctx) -> Result<bool, Error> + 'a>( &mut self, uid: Uid, handler: H ) -> &mut Self
sourcepub fn set_handler<A, O, H, T>(
&mut self,
uid: Uid,
handler: H,
store: T
) -> &mut Self
pub fn set_handler<A, O, H, T>( &mut self, uid: Uid, handler: H, store: T ) -> &mut Self
Register a callback that will called by Policy
when option set.
The Invoker
first call the invoke
, then
call the process
with the return value.
§Note
| handler: |&mut Set, &mut Ser, { Other Args }| -> Result<Option<Value>, Error>
| storer: |&mut Set, &mut Ser, Option<&RawVal>, Option<Value>| -> Result<bool, Error>
|
wrapped
|
v
| |&mut Set, &mut Ser, &Ctx| -> Option<Value>
|
invoked
|
v
| call Callbacks::invoke(&mut self, &mut Set, &mut Ser, &mut Ctx)
| call Handler::invoke(&mut self, &mut Set, &mut Ser, Args)
| call Args::extract(&Set, &Ser, &Ctx) -> Args
| -> Result<Option<Value>, Error>
| -> call Store::process(&Set, Option<&RawVal>, Option<Value>)
| -> Result<bool, Error>
pub fn has(&self, uid: Uid) -> bool
Trait Implementations§
source§impl<'a, Set, Ser> HandlerCollection<'a, Set, Ser> for Invoker<'a, Set, Ser>where
Set: Set,
impl<'a, Set, Ser> HandlerCollection<'a, Set, Ser> for Invoker<'a, Set, Ser>where
Set: Set,
fn register<H: FnMut(&mut Set, &mut Ser, &mut Ctx) -> Result<bool, Error> + 'a>( &mut self, uid: Uid, handler: H )
fn get_handler( &mut self, uid: &Uid ) -> Option<&mut InvokeHandler<'a, Set, Ser, Error>>
Auto Trait Implementations§
impl<'a, Set, Ser> Freeze for Invoker<'a, Set, Ser>
impl<'a, Set, Ser> !RefUnwindSafe for Invoker<'a, Set, Ser>
impl<'a, Set, Ser> !Send for Invoker<'a, Set, Ser>
impl<'a, Set, Ser> !Sync for Invoker<'a, Set, Ser>
impl<'a, Set, Ser> Unpin for Invoker<'a, Set, Ser>
impl<'a, Set, Ser> !UnwindSafe for Invoker<'a, Set, Ser>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more