rhaiscript / book

The Rhai Book.
https://rhai.rs/book
22 stars 22 forks source link

getter-setter example doesn't compile #15

Closed tguichaoua closed 2 years ago

tguichaoua commented 2 years ago

The following example doesn't compile.

https://github.com/rhaiscript/book/blob/dc74fc9f6f3d74e69e6615b113a3fdd801b0329f/src/rust/getters-setters.md?plain=1#L63-L103

In new .to_string() is missing

    fn new() -> Self {
        Self { field: "hello".to_string() }
    }

Also it's not possible to register both getter and setter with register_get_set because one returns a String and the other expects a &str.

error[E0631]: type mismatch in function arguments
   --> rhai_\src\main.rs:32:57
    |
16  |     fn set_field(&mut self, new_val: &str) {
    |     -------------------------------------- found signature of `for<'r, 's> fn(&'r mut TestStruct, &'s str) -> _`
...
32  |         .register_get_set("xyz", TestStruct::get_field, TestStruct::set_field)
    |          ----------------                               ^^^^^^^^^^^^^^^^^^^^^ expected signature of `for<'r> fn(&'r mut TestStruct, String) -> _`
    |          |
    |          required by a bound introduced by this call
    |
note: required by a bound in `rhai::api::register::<impl Engine>::register_get_set`
   --> C:\Users\_\.cargo\registry\src\github.com-1ecc6299db9ec823\rhai-1.8.0\src\api\register.rs:555:22
    |
555 |         set_fn: impl Fn(&mut T, V) + SendSync + 'static,
    |                      ^^^^^^^^^^^^^ required by this bound in `rhai::api::register::<impl Engine>::register_get_set`

For more information about this error, try `rustc --explain E0631`.
schungx commented 2 years ago

Good catch. It is a typo.

schungx commented 2 years ago

It'll be fixed during the next up-sync.