Attribute Macro oy::Methods [−][src]
#[Methods]
Gives interactive access to a structs methods.
Only methods with supported argument types will be made interactive.
Currently supported argument types are:
bool
, char
, f32
, f64
, i8
, i16
, i32
, i64
, i128
, isize
, u8
, u16
, u32
,
u64
, u128
, usize
, String
, str
References to these types are also supported.
Generic argument types are not supported.
Both String
and str
are only available with default features on.
What it does:
#[Methods] impl Struct { fn ping(&self) -> &str { "pong" } fn frob(&mut self, arg: u32){ unimplemented!() } }
Expands to something like:
(notice how frob
is only available inside eval_method_mut
)
impl Methods for Struct { fn eval_method(&self, method_name: &str, args: &str, f: &mut dyn FnMut(Result<'_, &dyn Debug>)) { match method_name { "ping" => match parse_0_args(method_name, args) { Ok(()) => f(Ok(&self.ping())), Err(e) => f(Err(e)), }, _ => f(Err(MethodNotFound { type_name: "Struct", method_name, })), } } fn eval_method_mut(&mut self, method_name: &str, args: &str, f: &mut dyn FnMut(Result<'_, &dyn Debug>)) { match method_name { "ping" => match parse_0_args(method_name, args) { Ok(()) => f(Ok(&self.ping())), Err(e) => f(Err(e)), }, "frob" => match parse_1_arg(method_name, args) { Ok((arg0,)) => f(Ok(&self.frob(arg0))), Err(e) => f(Err(e)), }, _ => f(Err(MethodNotFound { type_name: "Struct", method_name, })), } } fn get_all_method_names(&self) -> &'static [&'static str] { &["ping", "frob"] } }