These are chat archives for rust-lang/rust

8th
Jul 2018
Kelly Thomas Kline
@kellytk
Jul 08 2018 01:45
Can UnixListener from tokio_uds or std::os::unix::net be configured to bind to a socket and creating its file with permissions I specify?
Luo Jia
@luojia65
Jul 08 2018 03:02
Hello! Is there any way to do this impl ErrorTrait in Rust? Thus a Box is not needed and memory can be saved.
fn foo() -> Result<ReturnType, impl ErrorTrait> { ... }
Zakarum
@omni-viral
Jul 08 2018 08:32
Should be possible currently
tsoernes
@tsoernes
Jul 08 2018 08:38

Any help with this one:

pub type Grid<S: Data<Elem = bool>> = ArrayBase<S, Ix3>;
pub fn mymut<T: Data<Elem = bool>>(
    grid: &mut Grid<T>,
) {
    // cannot assign to immutable indexed content (cannot borrow as mutable) (rust-cargo)
    grid[[0, 0, 0]] = false;
}

where ArrayBase is from ndarray

Sylwester Rąpała
@xoac
Jul 08 2018 08:49
I have a struct MyStruct and a Struct that I get from external crate. Can I implement From<MyStruct> for StructFromExternalCrateStruct?
Luo Jia
@luojia65
Jul 08 2018 08:51
@xoac If I'm not mistaken, yes. You can even impl From<MyStruct> for i32 where i32 is a primitive type.
… and use i32::from(my_struct) to perform this conversation.
@omni-viral No it’s not possible, an error will be given by compiler.
Looks like OK to me
Luo Jia
@luojia65
Jul 08 2018 09:58
Wow
look like I need to update my Rust, thanks
Zakarum
@omni-viral
Jul 08 2018 10:00
Note that you'd have to annotate Ok in case rustc couldn't infer what type to use for impl ErrorType
Luo Jia
@luojia65
Jul 08 2018 10:02
Emmmm, Okay
trsh
@trsh
Jul 08 2018 10:09
Hello! My rust 'target' folder takes about 11 GB. Is it not an overkill? Can I cut it down?
Denis Lisov
@tanriol
Jul 08 2018 10:26
@trsh Depends on the number of dependencies...
...you may want to cargo clean and rebuild the project to get rid of outdated deps, if any.
trsh
@trsh
Jul 08 2018 10:35
@tanriol anyway the size like WTF. I do not have DVD movies in my decencies.
@tanriol could be that most stuff is for compiling? And that the executable will be small?
Otherwise I need to buy new hosting for my web project :D
Denis Lisov
@tanriol
Jul 08 2018 10:40
I've just cleaned and rebuilt a small project (4k lines of Rust) and have a 760MB target. The two executables are around 90MB with debuginfo or 20MB without each.
Dylan DPC
@Dylan-DPC
Jul 08 2018 10:41
@trsh the size will be less when you create a production build
trsh
@trsh
Jul 08 2018 10:42
mhm.. that seams +/- ok. Should be smaller in my opinion.
20 mb for 4 lines
But as it can live on it self, maybe its ok
Denis Lisov
@tanriol
Jul 08 2018 10:44
Stripped and in release build they are 10MB
trsh
@trsh
Jul 08 2018 10:45
Cleaning was very helpfull. Down to 1.3 GB from 11,3
@tanriol ok, tnx, then no worries
Denis Lisov
@tanriol
Jul 08 2018 10:46
Most likely these were the leftover old dependency versions.
trsh
@trsh
Jul 08 2018 10:59
I also think, as I once played with Nightly, it was x2
tsoernes
@tsoernes
Jul 08 2018 12:16
Can anyone elaborate on this error?
pub type Grid<S: Data<Elem = bool>> = ArrayBase<S, Ix3>;
bounds on generic parameters are not enforced in type aliases (rust-cargo)
the bound will not be checked when the type alias is used, and should be removed (rust-cargo)
Denis Lisov
@tanriol
Jul 08 2018 12:16
Is this an error or a warning?
tsoernes
@tsoernes
Jul 08 2018 12:17
@tanriol Warning
Denis Lisov
@tanriol
Jul 08 2018 12:20
It says that your S: Data<Elem = bool>> will not be checked by the compiler. A type is an alias for some combination of types, it has no extra bounds apart from what the types in it require.
tsoernes
@tsoernes
Jul 08 2018 14:42
According to the book; if you have tests in separate files ie tests/sometest.rs then in those file you should use extern crate myprog to import whatever you want to test. I use the same myprog as specified by the name field in Cargo.toml, but cargo test gives me "can't find crate" for my own crate. what to do?
Denis Lisov
@tanriol
Jul 08 2018 14:46
@tsoernes The problem is probably that the test is trying to find a library crate rustdca, but your crate is binary only, not library.
tsoernes
@tsoernes
Jul 08 2018 14:47
oh I see. So tests for binaries should never be in separate folder then?
Denis Lisov
@tanriol
Jul 08 2018 14:50
Well, they can be if you want to test the compiled binary and not its internal components :-)
Even for library projects these tests can access public APIs only.
Adler Oliveira
@adleroliveira
Jul 08 2018 14:53
Hello all, does anyone know a practical way to go from 0b11110000 to a struct instance where for the 4 initial bits each one is true and each zero is false on a struct like:
struct Options {
  a: bool,
  b: bool,
  c: bool,
  d: bool
}
Denis Lisov
@tanriol
Jul 08 2018 14:59
For just one value/structure? Manually.
Maybe using a macro to extract the Nth bit.
Adler Oliveira
@adleroliveira
Jul 08 2018 15:00
it can be any combination of the first four bits and I need to end up with a struct like this one
so manually then..
Denis Lisov
@tanriol
Jul 08 2018 15:02
If you have lots of parsing, you may want to use some parser library like nom
If the representation does not strictly need to be a struct, you may want to take a look at the bitflags crate.
Adler Oliveira
@adleroliveira
Jul 08 2018 15:04
yes, I was considering it but it end up as a single value of an enum and I need individual flag values
Denis Lisov
@tanriol
Jul 08 2018 15:06
Are you asking about doing it at one point of your code or at many places?
Adler Oliveira
@adleroliveira
Jul 08 2018 15:06
just one
Denis Lisov
@tanriol
Jul 08 2018 15:09
For clarity I'd probably go with bitflags and Options { a: flags.contains(Flags::A), ... }
Adler Oliveira
@adleroliveira
Jul 08 2018 15:13
it make sense. Thanks
Joseph Lenton
@joe-askattest
Jul 08 2018 17:26
Hey, I'm using the new use extern::some_crate feature on a project. How do I import macros?
Is there another feature flag I need to use?
In particular I'm using StructOpt, and that exports Clap. I need to use a macro from Clap, and would like to be able to use it without having to add Clap as another dependency (since I'm already getting it via StructOpt).
matrixbot
@matrixbot
Jul 08 2018 17:34
bspeice I could be misunderstanding things, but in order to use macros from a crate, I'm pretty confident you need to add it as a dependency. You already need to add crates as a dependency to use them regularly, I don't think you're allowed to bring them in via extern if they're only transitive dependencies.
bspeice EDIT: missed reading the part that said StructOpt exports Clap. Not sure I'm totally able to speak on this scenario.
Joseph Lenton
@joe-askattest
Jul 08 2018 17:42
Ok, np. What I'm trying to avoid is the worry that I'm using a different version of a dependency, than my libraries are. I know Rust goes to lengths to ensure this isn't an issue. I'd still sleep better at night if I'm not mixing different versions of the same dependency.
matrixbot
@matrixbot
Jul 08 2018 17:59
bspeice Source replacements I think should be able to accomplish what you're after: https://doc.rust-lang.org/cargo/reference/source-replacement.html
Adler Oliveira
@adleroliveira
Jul 08 2018 22:32
is it possible for a struct to hold either an instance of another struct or a reference of an instance? What is the best practice in this case?
Denis Lisov
@tanriol
Jul 08 2018 22:33
Sure it's possible. Depends on the use case, but for me an instance usually makes more sense.
Adler Oliveira
@adleroliveira
Jul 08 2018 22:38
imagine that I have a reqwest (http client) instance and want to spin up for instances of a service that share a reference of said client (so I don't need to create 4 htttp clients). The other case is that I don't want multiple instances, just one so instead of a reference I want to pass an owned instance of the http client
what I want is to be able to hold either