pub struct DynamicContext { /* private fields */ }

Implementations§

source§

impl DynamicContext

source

pub fn new() -> Self

Examples found in repository?
examples/conversation.rs (line 50)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn converse(args: &[&str], _context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
    if args.len() == 0 {
        return Ok("no name provided".to_string());
    }
    println!("interacting with: {}", args[0]);
    let commands = vec![
        Command{command: "hello", func: hello, help_output: "hello - say hello"},
        Command{command: "change", func: change, help_output: "change <name> - change the name of the person with whom you're speaking"},
        //Command{command: "yes", func: yes, help_output: "yes - reply yes"},
        //Command{command: "no", func: no, help_output: "no - reply no"},
    ];

    // let mut name: Option<Box<dyn Any>> = Some(Box::new(args[0].to_string()));
    let mut context = DynamicContext::new();
    context.set(args[0].to_string());
    // passing the arguments for the converse commands as context
    // to the commands in the next command loop for reference
    if let Err(e) = command_loop(&commands, &mut context){
        eprintln!("error running interact command loop: {}", e);
    }
    Ok(String::new())
}

fn main(){
    let commands = vec![
        Command{
            command: "converse",
            func: converse,
            help_output: "converse <name> - interact with a person"
        },
    ];

    // start the command loop with the provided commands
    if let Err(e) = command_loop(&commands, &mut DynamicContext::new()){
        eprintln!("error running command loop: {}", e.to_string());
    }
}
source

pub fn set<T: 'static>(&mut self, value: T)

Examples found in repository?
examples/conversation.rs (line 51)
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
fn converse(args: &[&str], _context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
    if args.len() == 0 {
        return Ok("no name provided".to_string());
    }
    println!("interacting with: {}", args[0]);
    let commands = vec![
        Command{command: "hello", func: hello, help_output: "hello - say hello"},
        Command{command: "change", func: change, help_output: "change <name> - change the name of the person with whom you're speaking"},
        //Command{command: "yes", func: yes, help_output: "yes - reply yes"},
        //Command{command: "no", func: no, help_output: "no - reply no"},
    ];

    // let mut name: Option<Box<dyn Any>> = Some(Box::new(args[0].to_string()));
    let mut context = DynamicContext::new();
    context.set(args[0].to_string());
    // passing the arguments for the converse commands as context
    // to the commands in the next command loop for reference
    if let Err(e) = command_loop(&commands, &mut context){
        eprintln!("error running interact command loop: {}", e);
    }
    Ok(String::new())
}
source

pub fn get<T: 'static>(&self) -> Option<&T>

Examples found in repository?
examples/conversation.rs (line 25)
24
25
26
27
28
29
30
31
32
33
fn hello(_args: &[&str], context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
    match context.get::<String>() {
        None => { 
            return Ok("you are not in a conversation with a person".to_string()) 
        },
        Some(name ) => {
            return Ok(format!("You said hello to {name} I guess"));
        }
    }
}
source

pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T>

Examples found in repository?
examples/conversation.rs (line 15)
14
15
16
17
18
19
20
21
22
fn change(_args: &[&str], context: &mut DynamicContext)->Result<String, Box<dyn Error>>{
    match context.get_mut::<String>() {
        Some(mut_ref) => {
            *mut_ref = _args[0].to_string();
            return Ok(format!("you changed their name to {}", *mut_ref));
        },
        None => return Ok("you are not in a conversation with a person".to_string()),
    }
}

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.