These are chat archives for rust-lang/rust

28th
Aug 2018
A Dinesh
@dineshadepu
Aug 28 2018 04:41
Hi all. I am reading "The Book". In lifetimes with generics section, we have a longest function, which takes two strings which have same lifetimes
What if we have the input strings with different lifetimes
How is this function valid?
Vitaly
@Virtuos86
Aug 28 2018 05:49
Read on, Listing 10-23 describes this moment.
Derk Bell
@derkbell
Aug 28 2018 07:12

Is it possible to pass a struct/type (not sure how to call it) to a function? I'm trying to build a kind of plugin system but I'm stuck here:

struct Main { plugins: Vec<Box<Plugin>> }
impl Main {
  fn register<T: Plugin>(&self, plugin: T) {}
}
trait Plugin {}
struct MyPlugin {}
impl Plugin for MyPlugin {}
fn main () {
  let m = Main { plugins: Vec::new()};
  m.register(MyPlugin);
}

Can it be done in this way? I need Main::register to perform some steps based on its state, eventually calling into one of the trait methods, so I don't want to initialise a plugin struct by hand. m.register(MyPlugin::create_some_intermediate_form()) is unwantend.
Or do I have to reverse it and do something like MyPlugin::register_into(m)?

Michal 'vorner' Vaner
@vorner
Aug 28 2018 07:18
Hello. What happens if I have pub extern "C" fn something, called from C and panic! in there? Is it UB, or will the program abort with a message?
Derk Bell
@derkbell
Aug 28 2018 07:25

@derkbell You were so close, let register have a type parameter:

struct Main { plugins: Vec<Box<Plugin>> }
impl Main {
  fn register<T: Plugin>(&self) {}
}
trait Plugin {}
struct MyPlugin {}
impl Plugin for MyPlugin {}
fn main () {
  let m = Main { plugins: Vec::new() };
  m.register::<MyPlugin>();
}

thanks

Denis Lisov
@tanriol
Aug 28 2018 07:40
@vorner IIUC, it's UB for now (see rust-lang/rust#52652 )
Michal 'vorner' Vaner
@vorner
Aug 28 2018 08:04
The panic = 'abort' has no effect on staticlib/cdylib crate targets?
Vitaly
@Virtuos86
Aug 28 2018 09:38
@derkbell
struct Main {
    plugins: Vec<Box<dyn Plugin>>,
}

impl Main {}

trait Plugin {}

struct MyPlugin {}
struct MyPlugin1 {}

impl Plugin for MyPlugin {}
impl Plugin for MyPlugin1 {}

trait Extendable<T> {
    fn register(&self);
}

impl Extendable<MyPlugin> for Main {
    fn register(&self) {}
}
impl Extendable<MyPlugin1> for Main {
    fn register(&self) {}
}

fn main() {
    let m = Main {
        plugins: Vec::new(),
    };
    <Main as Extendable<MyPlugin>>::register(&m);
    <Main as Extendable<MyPlugin1>>::register(&m);
}
Krys
@Krysme
Aug 28 2018 12:33
hello, I'm wondering what happened to async/await stuff, I can't even run tokio-async-await examples
with nightly version
Derk Bell
@derkbell
Aug 28 2018 14:22
@Virtuos86 thanks!
tandrysyawaludin
@tandrysyawaludin
Aug 28 2018 16:36
how to call module in different folder?
Lyle Mantooth
@IslandUsurper
Aug 28 2018 16:50
@tandrysyawaludin, with use path::to::module, but the exact path depends on where the mosule is in relation to the one useing it
tandrysyawaludin
@tandrysyawaludin
Aug 28 2018 16:56

here the structure

a/mod_a.rs
b/mod_b.rs

in mod_b.rs i wanna use mod_a

Vitaly
@Virtuos86
Aug 28 2018 16:59
use super::a?
tandrysyawaludin
@tandrysyawaludin
Aug 28 2018 17:01
   |
11 | use super::a;
   |     ^^^^^^^^^^^^^^^^^^^^ no `a` in `b`
i got this error
Lyle Mantooth
@IslandUsurper
Aug 28 2018 17:04
You should probably rename them to a/mod.rs and b/mod.rs. There has to be a mod.rs in every folder for rustc to find the modules.
AND, in your main.rs or lib.rs that sits alongside a/ and b/, there should be
mod a;
mod b;
tandrysyawaludin
@tandrysyawaludin
Aug 28 2018 17:05
i got the solver #[path = "../a/mod.rs"] mod a;
thanks all
Lyle Mantooth
@IslandUsurper
Aug 28 2018 17:08
@tandrysyawaludin, is that line in your b module? It'd be better to put it in the upper level so you don't need the #[path] attribute. Anyone who comes to your project is going to expect the standards and conventions to be followed.
If that is in b, you're saying that mod a exists within mod b. Any other modules in your crate are going to have to use b::a; to get to it. (Though right now they can't because it's not a pub mod a;).
tandrysyawaludin
@tandrysyawaludin
Aug 28 2018 17:12
yes, in b module. Actually, a module is a helper in my project. So, is it better to put a module within b module ?
Lyle Mantooth
@IslandUsurper
Aug 28 2018 17:16
If that's how you think of it, then yes. I'd say the files should be b/mod.rs and b/a.rs.
Then you can just say mod a; in b/mod.rs and rustc will find it.
In the future, if a needs submodules, then mkdir b/a; mv b/a.rs b/a/mod.rs
tandrysyawaludin
@tandrysyawaludin
Aug 28 2018 17:23
okay, thanks a lot
Lyle Mantooth
@IslandUsurper
Aug 28 2018 17:23
Well. . . is a a helper for b or for the whole crate?
tandrysyawaludin
@tandrysyawaludin
Aug 28 2018 17:25
for the whole, for many modules
Lyle Mantooth
@IslandUsurper
Aug 28 2018 17:35
Then it don't put it in b. Go back to my original suggestion. Declare mod a; and mod b; both in your main.rs or lib.rs, whichever you have.
Then each module that needs a needs use a; to bring it in scope.
Justin Kilpatrick
@jkilpatr
Aug 28 2018 18:47
so I'm using tosocketaddrs to resolve some domain names, how do I set a timeout?