These are chat archives for rust-lang/rust

14th
May 2018
tandrysyawaludin
@tandrysyawaludin
May 14 2018 04:13
51 | #[derive(Insertable, Queryable, Identifiable, Debug, PartialEq)]
   |          ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `std::string::String`
anyone know why?
tandrysyawaludin
@tandrysyawaludin
May 14 2018 10:32
is there type like this Option<JSON>?
Denis Lisov
@tanriol
May 14 2018 10:33
Do you mean in Rust or specifically in Diesel?
tandrysyawaludin
@tandrysyawaludin
May 14 2018 10:34
both
Denis Lisov
@tanriol
May 14 2018 10:38
Do you need to work with some unstructured JSON or with some structure expected in it?
tandrysyawaludin
@tandrysyawaludin
May 14 2018 10:38
i have column abc with type JSON and it is unstructured
Denis Lisov
@tanriol
May 14 2018 10:39
The most used type for a JSON is serde_json::Value
And yes, it can be wrapped in an Option
tandrysyawaludin
@tandrysyawaludin
May 14 2018 10:39
Option<serde_json::Value> like this?
Denis Lisov
@tanriol
May 14 2018 10:41
For example, like this. Or you can use the Value type and then it's just Option<Value>
tandrysyawaludin
@tandrysyawaludin
May 14 2018 10:44
now i get this error
48 | #[derive(Serialize, Deserialize, Insertable)]
   |                                  ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `rocket_contrib::Value`
Andy Bell
@Andy-Bell
May 14 2018 10:55
looks like you have two libraries importing Value - I would specify Option<serde_json::Value> and see if that works
tandrysyawaludin
@tandrysyawaludin
May 14 2018 10:59
is an error show because i have imported two libraries?
Andy Bell
@Andy-Bell
May 14 2018 11:11
it is saying that it is not implemented for rocket_contrib::Value, so you must be importing Value from that library as well as from serde_json
tandrysyawaludin
@tandrysyawaludin
May 14 2018 11:14
i have used this Option<serde_json::Value> but the error still same
Bryan Burgers
@bryanburgers
May 14 2018 11:55

I need to move a trait from one crate to another. In order to be as backward-compatible as possible, I want to deprecate the trait in the old crate, and re-export it in the new crate.

I don't believe I can copy/paste the trait to the new crate, because then it would be a separate trait, so there would be an ecosystem split between existing implementers that implemented the trait from the old crate, and new implementers that implement the trait from the new crate.

Is there a way I can deprecate the trait in the old crate, but then un-deprecate the re-export from the new crate?

https://play.rust-lang.org/?gist=32cdde841efc19cc00542cf2d666e9c8&version=stable&mode=debug

Berkus Decker
@berkus
May 14 2018 13:02
@tandrysyawaludin you need to pull in some diesel traits for this to work i believe
although i don't see anything in my code at the moment and serde_json::Values work fine.
@tandrysyawaludin ah, make sure you have proper features for the crate
[dependencies.diesel]
features = [
    "postgres",
    "chrono",
    "serde_json", # <-- this
]
Michal 'vorner' Vaner
@vorner
May 14 2018 13:05

Hello. I'm trying to use the leak sanitizer. But my program uses custom derives (what programs don't these days) and the custom derive thing complains it can't compile with -Z sanitizer:

   Compiling failure_derive v0.1.1
error: Only executables, staticlibs, cdylibs, dylibs and rlibs can be compiled with `-Z sanitizer`

How do I pass the flag only to the rustc invocations for my final program, but not for the bits that generate code?

Oh, found a report and a workaround for that. I should use google first.
Maciej Gorywoda
@makingthematrix
May 14 2018 17:59

A newbie question here. I want to make a random number generator, but with a pseudo-normal distribution. That is, probability of a generated number should follow the normal distribution curve, but it's approximated by a polynomial to make it faster. Anyway, my problem is, I have this additional field, a, which is used in the polynomial. I want my Rng to always use the same a, so naturally I'd like to provide it in the constructor. So I created a struct:

pub struct PseudoNormalRng {
    a: f64,
    rng: Rng,
}

impl Rng for PseudoNormalRng {
// ...
}

and now I want to create a PseudoNormalRng::new(a: f64) method, which would internally create a ThreadRng and assign it to rng. But rustc complains that it doesn't know the size of Rng at compile time. And if I change the rng signature to rng: &Rng then it switches to bugging me about the lifetimes:

error[E0597]: borrowed value does not live long enough
  --> src/utils/prng.rs:37:40
   |
37 |         PseudoNormalRng { a, rng: &mut rand::thread_rng() }
   |                                        ^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
38 |     }
   |     - temporary value only lives until here
Is there a good way to do it?
Maciej Gorywoda
@makingthematrix
May 14 2018 18:05
well, putting rng in a Box seems to work, but... do I have to do that?
James McCoy
@jamessan
May 14 2018 18:45
Rng is a Trait, not a type, so it doesn't have a size. Pretty sure you have to box it
Hans W. Uhlig
@huhlig
May 14 2018 18:46
or generic the container
pub struct PseudoNormalRng<T: Rng> {
    a: f64,
    rng: T,
}
Maciej Gorywoda
@makingthematrix
May 14 2018 19:42
yes!
thanks, @jamessan and @huhlig :)
Hans W. Uhlig
@huhlig
May 14 2018 19:43
np
Maciej Gorywoda
@makingthematrix
May 14 2018 19:47
so, I'm still "suffering" because of my Scala background. I tried to use the trait as a field in the struct. Of course, it was not possible to know the size of the trait - but in Scala that wouldn't matter, and was all the time thinking about my problem as if I was coding in Scala.
Boxing worked, and using the concrete class worked as well.
Hans W. Uhlig
@huhlig
May 14 2018 19:47
heh. I feel for you
I still have to use Java and C
getting out of those habits is fscking difficult
Maciej Gorywoda
@makingthematrix
May 14 2018 20:12
Rust is difficult ;)
Hans W. Uhlig
@huhlig
May 14 2018 20:12
nah, rust is opinionated
and most of it’s opinions are good ones
it’s just hard if you’re used to being lazy
Michal 'vorner' Vaner
@vorner
May 14 2018 20:13
Actually, I believe it's not Rust that's being difficult. It learning to be clear in your thinking and knowing what you're about to do. Rust just insists on the later.
Maciej Gorywoda
@makingthematrix
May 14 2018 20:15
still, I would call it difficult, but not in the sense that it's bad. I really need to know what I'm doing to make it work.
thanks once again, and good night :)
Hans W. Uhlig
@huhlig
May 14 2018 20:16
night
apiraino
@apiraino
May 14 2018 21:15
My .2 cents: I'm learning both German and Rust. They pretty much require the same effort: dedication, stubborness; They are indeed difficult but you can see they're not messy. And when you finally build up something that works, you're proud of your accomplishment. And you know that one day you'll eventually speak fluently both.