These are chat archives for rust-lang/rust

4th
Aug 2018
Ichoran
@Ichoran
Aug 04 2018 00:57
Why not create accessors instead? r() etc.
That kind of overloading doesn't sound safe anyway. I would avoid it.
qq00
@qq00
Aug 04 2018 02:22
Why unsafe? it's all f32.
Unrelated question: is there a way to define a type Foo so that for any time T, Foo<T> always maps to i32 ?
type Result<T> = i32;
tandrysyawaludin
@tandrysyawaludin
Aug 04 2018 03:00
@ozkriff i tried to do example.bytes() but somehow i need example as a string, how to solve?
@hlavaatch when i printed out there is no \ but after i put in vec![] char \ is available
qq00
@qq00
Aug 04 2018 03:04
struct Foo {
  a: A,
  b: B,
  c: C,
}
is there a way to programatically determine the location of "b", "c" relative to the start of struct "Foo"
@tandrysyawaludin converting str to string or moving it to vec doesn't add any actual escaping characters
what are you trying to do? just print Vec<String> without escape chars?
for s in &myvec { println!("{}", s); - just print individual strings using Display trait ({} in format string)
Jan Hlavatý
@hlavaatch
Aug 04 2018 09:03
@qq00 std::mem::size_of<T>()
Jan Hlavatý
@hlavaatch
Aug 04 2018 09:37
@tandrysyawaludin The problem is String is encoded in UTF-8 while raw bytes have no encoding - if you know the bytes are in UTF-8 you can use String::from_utf8(), for other emcodings use something like the "encoding" crate
Michal 'vorner' Vaner
@vorner
Aug 04 2018 10:45
Hello. I have a crate and I'd like to have a feature „mio“ which brings in dependencies „mio“ and „mio-uds“. I don't seem to have much luck with that, I'm fighting errors like „Features and dependencies cannot have the same name“. Is there a pattern how to work around that without renaming the feature?
Matthias Berndt
@mberndt123
Aug 04 2018 11:34
I don't really understand the Sync trait. Plenty of people seem to implement this trait for structs that contain raw pointers to mutable data, so they probably can't be safely shared across threads. Yet Sync is supposed to be a trait for types where that is possible. Why is that?
Denis Lisov
@tanriol
Aug 04 2018 11:36
@vorner Not at the moment. There's an unstable Cargo feature related to this.
@mberndt123 Depends on what operations on the data they allow. If you point at some example, we can discuss it in more detail.
Matthias Berndt
@mberndt123
Aug 04 2018 11:38
well, assuming they actually mutate the data that the pointer points to it wouldn't be safe, right?
assuming they don't do some sort of locking themselves
Denis Lisov
@tanriol
Aug 04 2018 11:42
Well, they're supposed to either do some locking, or provide the guarantees required in some other way...
Matthias Berndt
@mberndt123
Aug 04 2018 11:42
I was thinking of the libusb-rs library. The Device Handle struct implements Sync: https://github.com/dcuddeback/libusb-rs/blob/c658c422433d35b4a5a71038ebbf0eb1b926e2e2/src/device_handle.rs#L39
I don't know a lot about USB, but I don't see how there could be a way to make a handle to a hardware device thread safe
Denis Lisov
@tanriol
Aug 04 2018 11:53
The methods here delegate to the libusb API, which is documented to be thread-safe, so this is safe from the Rust point of view (memory safety).
The question of multiple threads interacting with the device itself is a separate question that depends on the API the device provides...
Matthias Berndt
@mberndt123
Aug 04 2018 12:26
I see
Thanks
tsoernes
@tsoernes
Aug 04 2018 12:31
someone want to do a code review? the code is decently commented and runs fine; I don't know if it is idiomatic Rust or not: https://github.com/tsoernes/machine_learning_basics
Akos Vandra
@axos88
Aug 04 2018 13:14

Does anyone know how to make deserialization work with structural composition?

Eg having:

#[derive(Deserialize)]
struct Comp1 { a: u32, b: u32 };
#[derive(Deserialize)]
struct Comp2 { c: u32, d: u32 };
#[derive(Deserialize)]
struct Stuff { 
  #[serde(flat?!)]
  c1: Comp1, 
  #[serde(flat?!)]
  c2: Comp2 
};

Work with deserialising:

{ 
  “a”: 1,
  “b”: 2,
  “c”: 3,
  “d”: 4
}
Akos Vandra
@axos88
Aug 04 2018 13:16
lol, yeah just RTFM-ed it too
sorry
Jeffrey van Aswegen
@jeffmess
Aug 04 2018 13:35
hey, just started using rust today... any reason why cargo update takes forever?
cargo update Updating registry `https://github.com/rust-lang/crates.io-index` Fetch [=============> ] 26.99%
is there another registry i can maybe point to?
tsoernes
@tsoernes
Aug 04 2018 16:52
any idea how to create a random ndarray? The example in their manual (https://docs.rs/ndarray-rand/0.7.0/ndarray_rand/trait.RandomExt.html) does not compile for me:
    let gn = Uniform::new(-10.0, 10.0);
    let centers: Array2<f64> = Array::random((2, 2), gn);
 the trait bound `rand::distributions::Uniform<{float}>: rand::distributions::IndependentSample<_>` is not satisfied (the trait `rand::distributions::IndependentSample<_>` is not implemented for `rand::distributions::Uniform<{float}>`) (rust-cargo)
 required by `ndarray_rand::RandomExt::random` (rust-cargo)
Judson Lester
@nyarly
Aug 04 2018 18:10
Is there a flag I can pass to cargo/rustc to get it to pretty-print types in error messages? Especially long chains of futures get to be unweildy.
I start to appreciate the long Haskell errors that bounced me away initially ;)
matrixbot
@matrixbot
Aug 04 2018 19:33
bspeice Yep. There's also a stage that says 'a will fix it, but you don't want to deal with references, so you just Arc everything. Your version of events is likely a better implementation though.
tsoernes
@tsoernes
Aug 04 2018 21:23

the Cargo.toml file for a project has specified rand = "0.5.4". When compiling, it says:

Compiling rand v0.4.2
Compiling rand v0.5.4

Apparantly, 0.4.2 is actually used in my project which causes errors for a crate relying on >v0.5. How to fix?

qq00
@qq00
Aug 04 2018 21:40
What does @{...} notation in Rust mean ?
Denis Lisov
@tanriol
Aug 04 2018 21:40
@qq00 Example?
qq00
@qq00
Aug 04 2018 21:42
js!( return new ArrayBuffer( @{length} ); )
Denis Lisov
@tanriol
Aug 04 2018 21:46
Looks like it is some special syntax used by the js! macro.
qq00
@qq00
Aug 04 2018 21:48
Okay, thanks, I haven't studied macros yet.
qq00
@qq00
Aug 04 2018 22:35
Is there a way, for arbitrary T, to convert Vec<T> to Vec<u8> ?
Denis Lisov
@tanriol
Aug 04 2018 22:36
No safe way to do that. This operation could expose uninitialized padding.
qq00
@qq00
Aug 04 2018 23:00
Suppose I absolutely wanted to shoot myself in the foot, https://doc.rust-lang.org/std/mem/fn.transmute.html would be the right way to do it right?
Denis Lisov
@tanriol
Aug 04 2018 23:03
Not for the Vec itself, but for something like slices it could work.
qq00
@qq00
Aug 04 2018 23:09
is Vec<T>.as_slice() constant time or linear time ?
Denis Lisov
@tanriol
Aug 04 2018 23:45
Constant time.