These are chat archives for rust-lang/rust

11th
Feb 2019
Zakarum
@omni-viral
Feb 11 07:32
@Gladdy you can still write impl Fn...
struct Sample<'a> {
    b : Box<dyn Fn(u32) -> u32 + 'a>,
}

impl<'a> Sample<'a> {
    fn new(b : impl Fn(u32) -> u32 + 'a) -> Self {
        Self {
            b : Box::new(b)
        }
    }
    fn run(&self, i : u32) {
        let x = (self.b)(i);
        println!("{}", x);
    }
}
fn main() {
    let b = Sample::new(|y|{
        y + 1
    });
    b.run(100);
}
Tritone
@tritone11
Feb 11 15:26
hello, I have a i32 and I want have a u8 from it but I cannot get around it, anybody could help me?
Ingvar Stepanyan
@RReverser
Feb 11 15:26
@tritone11 did you try as?
or, if you're using nightly, it's better to use try_from but otherwise it's still unstable unfortunately
Tritone
@tritone11
Feb 11 15:29
@RReverser thanks Ingvar :) didnt think about as
Ingvar Stepanyan
@RReverser
Feb 11 15:30
Note that you probably want to enforce bounds checking yourself if you're using as
To make sure you don't just clip invalid values
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:47

Hello, there. i am learning rust and trying to create something as following

trait Executable {
    fn execute(&self) -> Result<(), String>;
}

struct MyList {
    list: HashMap<String, Executable>,
}

struct Command {
    name: String
}
impl Executable for Command {
    fn execute(&self) -> Result<(), String> {
        println!("Command {} is executed", self.name);
        Ok(())
    }
}

But, obviously, it gives me an error

--> examples/exec/main.rs:8:5
  |
8 |     list: HashMap<String, Executable>,
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |

how it could be changed to have an ability to have kind of dynamic list with structs which implements a trait?
Thanks.

sorry:)
Zakarum
@omni-viral
Feb 11 16:48
Executable is not type
It is trait
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:48
yes
Zakarum
@omni-viral
Feb 11 16:48
However dyn Executable is type
But it is unsized
It means compiler can't know how much memory it takes to store value of that type
Because any implementation of trait can be coerced to the dyn Trait
Simplest solution is to put it into Box
Box<T> is always sized
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:49
but, if is this the only way?
Ingvar Stepanyan
@RReverser
Feb 11 16:50

Simplest solution

Depends whether he actually needs arbitrary Executable in the same list

If list is expected to be uniform, then you can add generic type to MyList instead
trait Executable {
    fn execute(&self) -> Result<(), String>;
}

struct MyList<T: Executable> {
    list: HashMap<String, T>,
}

struct Command {
    name: String
}

impl Executable for Command {
    fn execute(&self) -> Result<(), String> {
        println!("Command {} is executed", self.name);
        Ok(())
    }
}
This is preferable when possible
Zakarum
@omni-viral
Feb 11 16:51
@RReverser is right
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:51
my goal is use RON files with stored commands and load it...and then execute.so i would like to have as much clean file as possible.
Zakarum
@omni-viral
Feb 11 16:51
Except I'd avoid having trait bounds in type declarations
Ingvar Stepanyan
@RReverser
Feb 11 16:52
@lykhouzov Hmm, do you even need trait then? If it's all just commands, you can put Command directly into your list?
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:52
ok. it could be different command:)
Zakarum
@omni-viral
Feb 11 16:52
enum
Ingvar Stepanyan
@RReverser
Feb 11 16:52
^ this
Zakarum
@omni-viral
Feb 11 16:53
In all similar situations prefer either generic type or enum. Use dyn Trait as last resort
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:53
hmm. enum could be a solution...i'm gonna try
unfortunately i am very newbie in rust:) os have to read a lot to get all these points
Zakarum
@omni-viral
Feb 11 16:54
You gonna parse file, so you should know beforehand all possible command types
You can define all command types and then create enum that can store any of them
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:55
@omni-viral yes, this is what i am thinking about. the only think every new command will required update the enum...but for the moment i can survive
Zakarum
@omni-viral
Feb 11 16:56
Shoudn't be too hard to maintain
Unless you wan't downstream crates to provide you with custom command types enum should work just find
There are even crates to automactially derive traits for enums
But for learning you'd better do it manually )
Aleksandr Lykhouzov
@lykhouzov
Feb 11 16:58
in my thoughts, for the moment, to have a crate with predefined commands, but then, somehow create new commands without re-compiling main crate. i don't know if it's possible:)
yeah, i will start from simple, for learning purpose and then gonna see...
Thanks for all
Zakarum
@omni-viral
Feb 11 17:00

somehow create new commands without re-compiling

Either define new types in downstream crates - you'd have to use dyn Trait then.

Or make commands data-driven instead
Not sure how you can parse dyn Trait from file anyway
Probably this can help https://crates.io/crates/serde_dyn
Aleksandr Lykhouzov
@lykhouzov
Feb 11 17:03
for the beginning, I have enough :)
tsoernes
@tsoernes
Feb 11 18:32
what library to use for fixed point numbers?
mmynsted
@mmynsted
Feb 11 19:45
@tsoernes What are you wanted to do? Would something like this help? https://crates.io/crates/rug
tsoernes
@tsoernes
Feb 11 21:44
that's it. thanks