These are chat archives for rust-lang/rust

9th
Sep 2018
trsh
@trsh
Sep 09 2018 06:58
@omni-viral , rlua, comment more
Zakarum
@omni-viral
Sep 09 2018 06:59
@trsh hlua seems bit abandoned
rlua was updated month ago
Farzeen
@happycoder97
Sep 09 2018 07:00
#[derive(PartialEq)]     
struct A(u64);           

fn main() {              
    let a = A(10);       
    let b = A(10);       
    let c = &a;          
    if *c == b {                 // Why this line doesn't give 'move when borrowed' error?         
        println!("OK");  
    } else {             
        println!("FAIL");
    }                    
    let _d = *c;              // But this line gives error.
}
Zakarum
@omni-viral
Sep 09 2018 07:01
*c == b doesn't move arguments
Farzeen
@happycoder97
Sep 09 2018 07:02
I thought *foo always moved foo
When does *foo move and when it doesn't?
Zakarum
@omni-viral
Sep 09 2018 07:02
No. &*foo doesn't move foo but reborows
It doens't move when reference is taken immediately after dereferencing
Farzeen
@happycoder97
Sep 09 2018 07:03
Ohh.. Is there an implcit & happening here?
Zakarum
@omni-viral
Sep 09 2018 07:03
a == b is just sugared Eq::eq(&a, &b)
trsh
@trsh
Sep 09 2018 07:05
@omni-viral what about others? Any exp?
Farzeen
@happycoder97
Sep 09 2018 07:06
Thanks @omni-viral :+1:
Zakarum
@omni-viral
Sep 09 2018 07:06
@trsh No, actually I have expierence only with reference lua implementation
I wonder what this uses under the hood
Sam Johnson
@sam0x17
Sep 09 2018 07:14
is there some way to store a pointer to a closure in a struct?
like what I'm looking to do is to have a callback as a field in a struct
trsh
@trsh
Sep 09 2018 07:30
Anybody has tried to Run JS in Rust?
@omni-viral interesting, but https://github.com/kyren/rlua as https://github.com/tomaka/hlua ar updated few days ago, both
Zakarum
@omni-viral
Sep 09 2018 07:34
@sam0x17 you can't name closure type so it has to be type parameter
struct Foo<F> { closure: F, }
trsh
@trsh
Sep 09 2018 07:34
Guess have to try both
Zakarum
@omni-viral
Sep 09 2018 07:35
@sam0x17 or use dyn Trait
struct Foo { closure: Box<dyn Fn(Arg) -> Ret>, }
Sam Johnson
@sam0x17
Sep 09 2018 07:37
so with the dyn method, how do I assign to that field? like f.closure = |data| { ... } ?
that's what I'm looking to be able to do @omni-viral
trsh
@trsh
Sep 09 2018 07:39
"The base types that can be read and written are: i8, i16, i32, u8, u16, u32, f32, f64, bool, String. &str can be written but not read."
So ir rlua
No Vec
Zakarum
@omni-viral
Sep 09 2018 07:45
@sam0x17 with generic struct you just assign closure when initializing struct
Foo { closure: |arg| { ... }
With boxed dynamic trait you need to box the closure
Sam Johnson
@sam0x17
Sep 09 2018 07:49
ok thanks @omni-viral I will use the boxed version.. is it possible for the closure to have no return type?
Zakarum
@omni-viral
Sep 09 2018 07:50
Yes. Just ommit
Actually it will have () as return type
Sam Johnson
@sam0x17
Sep 09 2018 07:53
@omni-viral ok so how would I box the closure as I define it i.e. the following doesn't compile:
        self.on_data = Box<|d| {

        }>;
also thanks a bunch I really appreciate this
right now this is my field on the struct btw:
on_data: Box<dyn Fn(&[u8]) -> ()>
Zakarum
@omni-viral
Sep 09 2018 08:04
Box::new(|d| {...})
-> () can be ommited
Sam Johnson
@sam0x17
Sep 09 2018 18:05
how do I write a function that takes a closure, like map etc? I can't seem to find anywhere how to do that
Vitaly
@Virtuos86
Sep 09 2018 18:26
fn main() {
    fn map_vec<T, U>(closure: impl Fn(T) -> U, collection: Vec<T>) -> Vec<U> {
        let mut res = Vec::with_capacity(collection.len());
        for i in collection.into_iter() {
            res.push(closure(i));
        }
        res
    }
    println!("{:?}", map_vec(|s| s.to_string(), vec![1, 2, 3]));
}
https://play.rust-lang.org/?gist=56990a139ee9ceec8bcf30ff119c06df&version=stable&mode=debug&edition=2015
Sam Johnson
@sam0x17
Sep 09 2018 18:44
@Virtuos86 thanks -- what I have now is like this but I am getting lifetime compalints and not sure how to fix:
pub struct Stream {
    data_callback: Box<dyn Fn(&[u8])>
}

impl Stream {
    fn on_data(&mut self, callback: impl Fn(&[u8])) {
        self.data_callback = Box::new(callback);
    }
}
Vitaly
@Virtuos86
Sep 09 2018 18:48
@sam0x17 may be callback: impl Fn(&[u8]) + 'static?
Sam Johnson
@sam0x17
Sep 09 2018 18:51
@Virtuos86 that made it compile -- what exactly does that mean?
Vitaly
@Virtuos86
Sep 09 2018 18:53
@sam0x17 callback lives for as long as the program works
@sam0x17 what is a lifetime of a Stream instance?
Vitaly
@Virtuos86
Sep 09 2018 19:02

another solution:

pub struct Stream<'a> {
    data_callback: Box<dyn Fn(&[u8]) + 'a>
}

impl<'a> Stream<'a> {
    fn on_data(&mut self, callback: impl Fn(&[u8]) + 'a) {
        self.data_callback = Box::new(callback);
    }
}

https://play.rust-lang.org/?gist=0c34c11288ceac007eb07b9585a72030&version=stable&mode=debug&edition=2015

Sam Johnson
@sam0x17
Sep 09 2018 19:10
ok so that would make the callback live as long as the instance? that's what I want I think