These are chat archives for rust-lang/rust

13th
Oct 2018
Ryan Levick
@rylev
Oct 13 2018 10:00

Anyone have any experience with structopt? I'm getting a pretty opaque error with a validtor:

#[derive(StructOpt, Debug)]
struct Opt {
    #[structopt(validator = my_validator)]
    foo: usize
}

fn my_validator(x: String) -> Result<(), String> {
    Ok(())
}

Error:

error: proc-macro derive panicked
  --> src/main.rs:20:10
   |
20 | #[derive(StructOpt, Debug)]
   |          ^^^^^^^^^
   |
   = help: message: invalid structopt syntax: attr
Ryan Levick
@rylev
Oct 13 2018 11:05
Looking into the code it seems that parsing validator = my_validator as attribute meta fails
Are indentifiers not allowed in attribute meta data?
Seems not. Found some docs: An identifier followed by the equals sign '=' and a literal, providing a key/value pair
Sanchayan Maity
@SanchayanMaity
Oct 13 2018 11:10
I am generating a rust module from flatbuffer schema.
How to include it?
#[path = "./procinfo_generated.rs"]
mod procinfo_generated;

pub use procinfo_generated::rustox::procinfo::{
  LoadAverage, Proc, ProcArgs, ProcBuilder,
  get_root_as_proc
};
  Compiling flatbuffers-examples v0.1.0 (/home/sanchayan/rustox/flatbuffers-examples)
error[E0433]: failed to resolve. Could not find `procinfo_generated` in `{{root}}`
  --> src/lib.rs:11:9
   |
11 | pub use procinfo_generated::rustox::procinfo::{
   |         ^^^^^^^^^^^^^^^^^^ Could not find `procinfo_generated` in `{{root}}`

warning: unused import: `flatbuffers::FlatBufferBuilder`
 --> src/lib.rs:6:5
  |
6 | use flatbuffers::FlatBufferBuilder;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: #[warn(unused_imports)] on by default

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.
error: Could not compile `flatbuffers-examples`.

To learn more, run the command again with --verbose.
Sanchayan Maity
@SanchayanMaity
Oct 13 2018 11:42
Never mind seems to not work with nightly.
S-YOU
@S-YOU
Oct 13 2018 13:07
How to fix this in latest rust 1.29 or nightly?
*self &= !(1 << index);
   |                  ^^^^^^^^^^^^^ expected type parameter, found integral variable
   |
   = note: expected type `T`
              found type `{integer}`
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:15
Needs more context. What kind of thing is self?
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:17
Hello I have one little question
How to register module from upper folder ?
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:19
@AlexisVisco , you can only put mod foo; statements in a mod.rs file. (mod foo {} still works, but that's rarely used except for unit tests.)
So, that file needs to be at src/server/bin/server/mod.rs
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:20
oh okay
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:20
with cli.rs and commands.rs next to it, or in their own cli/mod.rs and commands/mod.rs files.
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:20
Okay
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:20
Oh, I see where they are now.
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:20
yes ;)
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:20
So . . . what you really want, I think is src/server/mod.rs with those mod statements
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:21
yes
but i need to use it in my main
thats embarassing :c
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:22
And . . . if you're going to make two different binaries in this crate, there's a different pattern to the file structure.
Not sure if your server or client is the "default" excutable (maybe neither), but Cargo builds everything in src/bin/*.rs as a separate crate.
Each file there gets its own main() and can import whatever modules in the rest of src/
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:24
Okay, server is a binary and client is another binary both have commons code base and separate code base
(im not sure if am i clear im french :P)
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:25
No worries. I think I understand.
How much is common and how much is separate?
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:26
in a perfect repo structures there is a client folder with client ONLY files, commons folder where server and client can pick libraries from it and a server folder with ONLY server files. I dont understand why it is so complicated to have a clean architecture like this ^^
Maybe there is an example of something like this on github?
maybe i am wrong and there is a better folder structure for this kind of project
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:28
I think there is, but I'm trying to make sure what I'm thinking of is better.
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:28
no problems ;)
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:30
So, I envision you having 3 crates in your repo: server, client, and common libraries.
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:30
Okay
S-YOU
@S-YOU
Oct 13 2018 13:30
@IslandUsurper
impl<T: BitAnd<Output=T> + BitAndAssign + BitOrAssign + Not + PartialEq + Shl<T> + Shr<usize, Output=T>> BitSet for T {
    fn clear(&mut self, index: usize) {
        *self &= !(1 << index);
    }

    fn is_set(&self, index: usize) -> bool {
        (*self >> index) & 1 == 1
    }

    fn set(&mut self, index: usize) {
        *self |= 1 << index;
    }
}
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 13:31
@IslandUsurper maybe need to do this in private no?
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:31
Sure.
S-YOU
@S-YOU
Oct 13 2018 13:34
Any idea?
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:36
I think it wants you to make index a T, not a usize.
The *Assign traits want both sides to be the same, I believe.
Ah, that's a default. But you can change it.
BitAndAssign<Rhs = usize>
Lyle Mantooth
@IslandUsurper
Oct 13 2018 13:42
Do @S-YOU need Shl<T> in that impl? I don't see you using it on self anywhere.
S-YOU
@S-YOU
Oct 13 2018 13:49
That's right. It IS not using anywhere. Its from a book.
self type inference from actual usage? really?
Wondering what is the purpose of those traits there? If compiler can't inference the type.
S-YOU
@S-YOU
Oct 13 2018 13:55
Changing usize to T, become incompatible type for trait error
Removing Shl<T> seems no changes
You can remove Clone constraint. Or you can remove for<'a> &'a T: Shr<usize, Output = T> constraint and replace (self >> index) & Self::one() == Self::one() with (self.clone() >> index) & Self::one() == Self::one()
S-YOU
@S-YOU
Oct 13 2018 14:13
Thanks. Can it be done without using num crate?
Lyle Mantooth
@IslandUsurper
Oct 13 2018 14:13
How can you remove the Clone constraint if you're going to use it?
red75prime
@red75prime
Oct 13 2018 14:14
"Or" is exclusive.
@S-YOU You can define your own trait One. And implement it for all types you are interested in.
Lyle Mantooth
@IslandUsurper
Oct 13 2018 14:18
Ha, it still took me a minute to see which "Or" you were talking about.
S-YOU
@S-YOU
Oct 13 2018 14:18
I see. So, it won't work with Literals
Alexis Viscogliosi
@alexisvisco
Oct 13 2018 15:07
Okay why in intellij there is no completion for:
( std::os::unix)
i am on mac osx so its an unix system ?