These are chat archives for rust-lang/rust

19th
Mar 2019
Sam Johnson
@sam0x17
Mar 19 00:01
it's saying I'm trying to use an undeclared module/symbol on the use line, and suggests I do extern crate bitfort but when I do that it says it cannot find the crate bitfort. It's odd because in my main.rs all of this imports and works fine
Brian Knapp
@knappador
Mar 19 00:28
It seems weird to export tests
Idk really. Still practically new to the import system
Sam Johnson
@sam0x17
Mar 19 03:50
yeah I just realized people like to do tests with implementation in rust so I've switched to that
kylegoetz
@kylegoetz
Mar 19 04:16
When did AtomicUSize get added to std::sync::atomic? I am on v1.33 but only AtomicBool and AtomicPtr exist in ::atomic for me according to my code completion.
I'm not an expert at parsing Rust docs yet, but https://doc.rust-lang.org/std/sync/atomic/struct.AtomicUsize.html doesn't make any mention of this being some nightlies-only thing.
kylegoetz
@kylegoetz
Mar 19 04:30
nvm, typo: AtomicUsize
Sam Johnson
@sam0x17
Mar 19 04:54
is there some way I can "or together" generic trait bounds e.g. struct SomeStruct<T: TypeA | TypeB | TypeC>?
Hoss Aja
@katetsu
Mar 19 08:01
do you think its a good idea to use rust for developing a compiler?
Zakarum
@omni-viral
Mar 19 10:29
@sam0x17 no
And in fact, you should avoid trait bounds on type definitions
And add them only on impls and functions that actually use them
Then you can implement same functions for T: TypeA, T: TypeB etc
Daniel Nordberg
@dnordberg
Mar 19 10:51
hey, i'm having trouble using pyo3-pack and nightly-2019-02-06
basically, rustup default nightly-2019-02-06 run successfully, but when i run rustc --version i still get another version
i've tried multiple docker images including those provided by rustlang, none seem to allow me to switch to a different version of rustc
any ideas?
Denis Lisov
@tanriol
Mar 19 10:55
I'd check which rustc executable actually runs :-) maybe PATH not configured properly or something like that?
Daniel Nordberg
@dnordberg
Mar 19 10:56
/root/.cargo/bin/rustup
thats using the dockerfile at https://github.com/PyO3/pyo3-pack
which should be stable
however, i've also tried https://hub.docker.com/r/rustlang/rust/, same issue
Denis Lisov
@tanriol
Mar 19 10:58
Erm... don't want to be picky, but do they really run rust as root?
Anyway, this was the rustup executable. Does rustc link to it?
Daniel Nordberg
@dnordberg
Mar 19 10:59
/root/.cargo/bin/rustc
in docker, yes they run as root

when i run:
rustup default nightly-2019-02-06
rustup show

i get nightly-2019-02-06 in the installed list but not active

Denis Lisov
@tanriol
Mar 19 11:02
And no overrides?
Daniel Nordberg
@dnordberg
Mar 19 11:04
@tanriol how can i check for this?
rust-toolchain?
doesn't exist
heres the dockerfile i'm using, https://github.com/PyO3/pyo3-pack/blob/master/Dockerfile, thanks allot for the help by the way, this has been bugging me for days
Sam Johnson
@sam0x17
Mar 19 16:05
@omni-viral thanks

guys I'm having a fight with the borrow checker -- I need num to be modified in this closure, but even RefCell isn't letting me:

#[test]
fn test_passthrough_limiter_large_write_alt() {
    let num = RefCell::new(0);
    let mut pas = Limiter::new(Passthrough);
    let mut acc = Limiter::new(Accumulator::new());
    acc.on_stateless_limit(|_ac| {
        *num.borrow_mut() += 1;
        Ok(())
    });
    pas.targets.push(Box::new(acc));
    assert_eq!(*num.borrow(), 3);
}
error[E0597]: `num` does not live long enough
   --> src/stream.rs:342:10
    |
341 |     acc.on_stateless_limit(|_ac| {
    |                            ----- value captured here
342 |         *num.borrow_mut() += 1;
    |          ^^^ borrowed value does not live long enough
...
345 |     pas.targets.push(Box::new(acc));
    |                      ------------- cast requires that `num` is borrowed for `'static`
346 |     assert_eq!(*num.borrow(), 3);
347 | }
    | - `num` dropped here while still borrowed

any ideas? I don't understand why it thinks it is still borrowed when both pas and ac drop out of scope, as at that point there is nothing left to borrow it

if I remove the pas.targets.push(Box::new(acc)); line it compiles (but this breaks the functionality)
John
@onFireForGod_gitlab
Mar 19 18:08
how can I save a struct to file?
Zakarum
@omni-viral
Mar 19 18:08
Use serde
John
@onFireForGod_gitlab
Mar 19 18:09
I use a library that has:
#[derive(Debug)]
pub struct GaussianProcess<T: Kernel, U: MeanFunc> {
    ker: T,
    mean: U,
    /// The observation noise of the GP.
    pub noise: f64,
    alpha: Option<Vector<f64>>,
    train_mat: Option<Matrix<f64>>,
    train_data: Option<Matrix<f64>>,
}
Matrix is from linalg library
I need to save this struct
Zakarum
@omni-viral
Mar 19 18:09
You still can do it using serde
John
@onFireForGod_gitlab
Mar 19 18:10
How should I approach this?
There are trait bounds for both GuassianProcess and Matrix, Vector can auto derive
Zakarum
@omni-viral
Mar 19 18:11
Add serde to dependencies.
Add #[derive(Serialize, Deserialize) to your struct.
For fields whose type don't implement Serialize or Deserialize use #[serde(with = "...")] attribute
It has pretty good documentation where you can find out details
https://serde.rs/
Also, avoid adding trait bounds on type definitions
In 95% of cases they are redundant
Like this one in snippet
John
@onFireForGod_gitlab
Mar 19 18:13
you mean ker: T and mean:u parts?
someone wrote this I am just using the lib
Zakarum
@omni-viral
Mar 19 18:14
I mean this part <T: Kernel, U: MeanFunc> can be just <T, U>
John
@onFireForGod_gitlab
Mar 19 18:14
k
Zakarum
@omni-viral
Mar 19 18:14
Oh, if this type is from dependency you still can make fields of that type serializable using serde(remote) attribute
John
@onFireForGod_gitlab
Mar 19 18:16
I will be importing a object GaussianProcess from a lib.
And will be using it like:
pub struct GaussianProcessModel { 
    model: GaussianProcess
}
#[derive(Serialize, Deserialize)]
pub struct GaussianProcessModel { 
    #[serde(remote)]
    model: GaussianProcess
}
Like this?
Zakarum
@omni-viral
Mar 19 18:19
Yes. And you would have to duplicate GaussianProcess structure (not its methods) as described here https://serde.rs/remote-derive.html
John
@onFireForGod_gitlab
Mar 19 18:20
thanks will take a look into that.
John
@onFireForGod_gitlab
Mar 19 18:34
So the matrix definition looks like:
pub struct Matrix<T> {
    rows: usize,
    cols: usize,
    data: Vec<T>,
}
Can I specify that I only want to deserialize for when T: f64
John
@onFireForGod_gitlab
Mar 19 20:37
I ended up modifing the libraries lol
Denis Lisov
@tanriol
Mar 19 20:38
@dnordberg Sorry, I'm not familiar enough with Docker.
@sam0x17 Looks like the type of pas.targets requires the vector elements to be 'static (possibly implicitly)
Arthur Gautier
@baloo
Mar 19 22:42
would anyone know a way to tell rustfmt to touch some part of a file? (a marker or similar)
matrixbot
@matrixbot
Mar 19 22:42
bspeice Rustfmt normally works on a per-file basis, and while you can exclude sections, I don't think there's a way to say explicitly that only some sections should be checked.
i got tests data that looked like this
maybe I should push them in other files and use include! macro
yeah, that works