These are chat archives for rust-lang/rust

12th
Aug 2017
Neno
@nenadl
Aug 12 2017 00:25
Hi rust-lang. Is it possible to have a module implementation across multiple files? I read somewhere that I can create sub-modules and then re-export types from the parent, but I was wondering if it's possible to split the implementation of a module across multiple files. I can't find anything about this in the book or anywhere else.
eurie
@iseurie
Aug 12 2017 05:08
i do not believe so, i have seen nothing on that either; modules and reexports appear to be the only way
buuuut if you create a parent module you can just reexport all its children by enumerating them with wildcards; pub use child::*;
might even be able to do something like pub use self::*; and export everything in its subordinate scope
i dont know the depth of pub use but if you can do that it'd allow you to spread impl across as many children as you wanted and hardly notice they even had different names
eurie
@iseurie
Aug 12 2017 05:14
my one worry would be that in all those submodules you wouldn't get the benefit of automatic public access to the fields of everything sans pub I don't think, unless you enclosed their definitions in the same file, because they would be in different modules
but i think wildcards are about as close as you can get
eurie
@iseurie
Aug 12 2017 05:19
oh oops
@nenadl
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:32

Hello. If I want one trait to „depend“ on another, it is simple to do:

trait A: B {
}

However, the Deserialize trait from serde is lifetime-parametric. I'd like to say that my trait depends on Deserialize with all the lifetimes (something like ∀ 'de), not that my trait is also lifetime parametric. Therefore, something like this:

trait A: Deserialize<'de> {
}

But rustc rightly complains it doesn't know where the 'de' comes from. Is there a syntax for that?

eurie
@iseurie
Aug 12 2017 06:35
if a trait depends on another trait, doesnt it implicitly depend on that trait's lifetimes?
Restioson
@Restioson
Aug 12 2017 06:37
Try trait A<'de>: Deserialize<'de> {}?
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:38
@Restioson That's not what I want. Basically, there's infinite number of Deserialize traits, each one with a different lifetime attached to it. I don't want to create infinite number of A traits, also each one with a lifetime. I want one A that depends on all that infinite number of Deserialize traits.
Restioson
@Restioson
Aug 12 2017 06:39
Couldn't you use that and then just vary A's 'de lifetime?
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:40
Huh? Sorry, I don't understand what you mean.
Restioson
@Restioson
Aug 12 2017 06:40
Wdym by infinite Deserialise traits?
eurie
@iseurie
Aug 12 2017 06:42
i dont know how you can express a trait having the lifetimes of another trait beyond parameterizing it the same way but i also dont understand how that's insufficient
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:44

OK, I'll try to explain. If I have the std trait From<T>, then there's actually many such traits ‒ From<i32>, From<IpAddr>, ... And a type can implement just some of them by this:

impl From<i32> for MyType { ... }

or all of them by:

impl<T> From<T> for MyOtherType { ... }
So if I have a trait Deserialize<'de>, there's actually Deserialize<'static> and Deserialize<'whatevere> for each lifetime 'whatever that may exist. The fact that everyone would implement them en-masse by impl<'de> Deserialize<'de> ... doesn't change the fact that these are actually different traits (that fold in the end to produce just one copy of the code).
And because I want to use A as a trait object and I want the trait objects to be 'static, 'A can't have a lifetime in its type. There must be just one A, not many with different lifetimes, because then I'd have different incompatible and hard to use trait objects.
eurie
@iseurie
Aug 12 2017 06:48
then cant you do A: Deserialize + 'static
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:48
So I don't want to say „trait A with lifetime 'static depends on Deserialize<'static> and A with lifetime 'tmp depends on Deserialize<'tmp>“, but „trait A depends on Deserialize<'static> and Deserialize<'tmp> and Deserialize<'whatever>`
No, rustc won't let me :-(
  --> src/data/column.rs:48:35             
   |
48 | pub trait Column: Clone + Debug + Deserialize + Ord + Serialize + 'static {
   |                                   ^^^^^^^^^^^ expected lifetime parameter
eurie
@iseurie
Aug 12 2017 06:51
maybe Deserialize<'static>
and then you rming the 'static bound from the rest of the def
oops
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:54
That doesn't work either. I really need to depend on all the deserialize traits (and it's not much of a restriction, in real situation, because every sane 'static type just implements deserialize for all the lifetimes)
I found a cheat around the actual problem, but I believe I heard there is a syntax for this, I just can't find it. I think it was something with for, but I don't know where to put it.
eurie
@iseurie
Aug 12 2017 06:56
well
gnite
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:57
Bingo. Trying to place that for into random positions finally led to a result that is accepted:
pub trait Column: Clone + Debug + for<'de> Deserialize<'de> + Ord + Serialize + 'static {
eurie
@iseurie
Aug 12 2017 06:58
what the
Michal 'vorner' Vaner
@vorner
Aug 12 2017 06:59
I heard about it in one of those request for explanation podcasts ‒ that it exists, and it might be a bit buggy. But the podcast didn't really say much about the exact syntax and it seems to be… underdocumented.
eurie
@iseurie
Aug 12 2017 07:02
i dont even know what you just did
Michal 'vorner' Vaner
@vorner
Aug 12 2017 07:05
You can look at these constraints as mathematical implications. Ordinary trait A: B means „if a type is A, it is also B, eg A → B in mathematical notation“
This one is that ∀ ‒ A → (∀'de)B<'de>
eg. whenever anyone gives you 'de, there's a dependency on that.
eurie
@iseurie
Aug 12 2017 07:06
what is ∀
Michal 'vorner' Vaner
@vorner
Aug 12 2017 07:08
foreach
eurie
@iseurie
Aug 12 2017 07:09
A is B <'de> for each 'de?
Michal 'vorner' Vaner
@vorner
Aug 12 2017 07:10
Yes, basically.
eurie
@iseurie
Aug 12 2017 07:14
why basically
Michal 'vorner' Vaner
@vorner
Aug 12 2017 07:17
I don't like „is“ in there, as „is“ means equivalence, this is just an implication. But that's beside the point here and that „is“ probably doesn't confuse anyone now.
This is basically the only documentation for for<'a> syntax.
It's pretty funny that the "Advanced Lifetimes" section in new book doesn't even mention it.
eurie
@iseurie
Aug 12 2017 12:08
new book is wip is it not
Restioson
@Restioson
Aug 12 2017 12:34
How would one go about converting a number to a str and then to bytes without std? I want to send it over serial but am a bit lost...
VJ
@00imvj00
Aug 12 2017 14:00
struct User{
Name: string,
}

enum Abc {
  User User,
}
where user is another struct.
can i write something like this. ?
Max Frai
@max-frai
Aug 12 2017 14:02
enum has another syntax in fields
VJ
@00imvj00
Aug 12 2017 14:02
can you provide with example ? @max-frai
Max Frai
@max-frai
Aug 12 2017 14:02
@0freeman00 just try: enum Abc { User(User) }
But i'm not sure that compiler will pass that similar naming for field & struct
VJ
@00imvj00
Aug 12 2017 14:09
@max-frai thnks.
Michal 'vorner' Vaner
@vorner
Aug 12 2017 14:46
@WaDelma Thanks for the link. I've read the nomicon, but really didn't remember these details O:-). I'm not sure if its undocumented on purpose, so people don't use this kind of arcana.
stevensonmt
@stevensonmt
Aug 12 2017 15:41
Still working through the book and got to the rust_gui example in chapter 17. Cargo interprets rust_gui as gui when compiling, so I have to change the extern crate and use lines to accommodate that. Is this the expected behavior or some bug or user error?
 /projects/rust/chap17/rust_gui/ $ cargo run                           git--rust_gui-- . master
   Compiling gui v0.1.0 (file:///projects/rust/chap17/rust_gui)
error[E0463]: can't find crate for `rust_gui`
 --> src/main.rs:1:1
  |
1 | extern crate rust_gui;
  | ^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to previous error

error: Could not compile `gui`.
Max Frai
@max-frai
Aug 12 2017 16:04
@stevensonmt you should add reference to rust_gui in Cargo.toml file
stevensonmt
@stevensonmt
Aug 12 2017 16:05
duh, thanks. I knew it was something I had done.
Max Frai
@max-frai
Aug 12 2017 16:06
@stevensonmt btw, what book are you reading? rust_gui seems to be outdated and not supported anymore
stevensonmt
@stevensonmt
Aug 12 2017 16:08
It's not an actual crate that is used. It's a library created as an example here. It does not actually implement any gui functionality as is.