These are chat archives for rust-lang/rust

6th
Jul 2018
tsoernes
@tsoernes
Jul 06 2018 00:05
That makes sense. Still I think (a, b) = fn(..) should be allowed.
On another note, just finished porting a non-trivial reinforcement learning (CPU not GPU) agent from Python with Numpy/Numba/Tensorflow over to Rust with ndarray. Got 6.8x speedup even though nearly all heavy lifting in python code is done in C-libraries.
Judson Lester
@nyarly
Jul 06 2018 01:56
I am somewhat stymied trying to figure out how to address the error here: https://github.com/nyarly/gotham/blob/hyper-update/gotham/src/test/mod.rs#L145 - Rustc says type annotations needed, consider giving this closure parameter a type
jjia-acme
@jjia-acme
Jul 06 2018 02:17
Sorry for interrupt your conversation. I wonder why not just label rust v2018 as v2?
Denis Lisov
@tanriol
Jul 06 2018 07:12
@jjia-acme Rust has declared that it uses semantic versioning, in which v2 means "incompatible with v1". The whole point of 2018 edition is to introduce some significant changes, but stay completely compatible.
James Sewell
@jamessewell
Jul 06 2018 07:22
If I'm trying to use .buffer_unordered on a Stream, and getting the trait futures::IntoFuture is not implemented for (bytes::BytesMut, bool, usize)
So I guess I need Futures back, not values
The Stream.poll returns Result<Async<Option<Self::Item>> -> but I don't know how to avoid unwrapping that to Self::Item (which is the tuple above)
Ooops I thought this was tokio -> still any help welcomed
Brian Schwind
@bschwind
Jul 06 2018 08:48

Is there a good way in Serde to deserialize a missing field to an empty Vec instead of throwing an error? I don't want the field to be Option<Vec<String>>, and my current solution isn't very fun:

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Settings {
    #[serde(default = "Vec::new")]
    pub languages: Vec<String>,
}

This is a bit hard to google for

Brian Schwind
@bschwind
Jul 06 2018 09:31
Seems I can use #[serde(default)] on the field which is slightly nicer, or #[serde(default)] on the entire struct if the struct implements Default, though I don't want something like a number field to default to 0 if it's missing
Sylwester Rąpała
@xoac
Jul 06 2018 09:41
whats wrong with #[serde(default)]? For missing filed like x or y if are optional use Option<i64>
for deserialize you can still use #[serde(default)] for Option<T> it's None and if you want to skip it whit ser #[serde(skip_serializing_if = "Option::is_none")]
Brian Schwind
@bschwind
Jul 06 2018 09:48

[serde(default)] isn't too bad, I wrote this question before I discovered that and thought I had to use serde(default = "Vec::new")]. I was hoping that for collection types, an empty collection would be returned instead of an error, because I don't see when I would want to distinguish between None and Some(vec!()). I suppose if you had a "update" API that clears out a vector in a DB if it's Some(vec!()), and doesn't touch the vector in the DB if it's None, that would be a good use case.

If [serde(default)] is the way to go for those fields then that's totally fine

Tom Anderson
@tomwhoiscontrary
Jul 06 2018 10:32
I once worked on a multi-tenant system where users were restricted to only seeing the data for their company by adding restrictions to the SQL queries made for them. The restriction was in the form of a set of user IDs they were allowed to see. The SQL query layer didn't distinguish between an empty set and a missing set. A missing set was used to mean "don't apply this restriction". So if there was ever a user with no permissions at all, suddenly, they had no restriction, and could see everything. That was pretty funny.
THEDESIGNER.DESIGN
@iamcharleschege
Jul 06 2018 15:26
Hi everyone! Has rust nightly had an update of late as I have rustup showing no updates, last nightly 2018-06-28 ?
Sylwester Rąpała
@xoac
Jul 06 2018 15:29
What is a correct way to design a "map" that can be looking by multiple keys. I mean sth like key1, key2, key3 value and I would like to looking for a value where key1 is a and key3 is b.
Dmitriy
@dpogretskiy
Jul 06 2018 15:30
@xoac why is this should be a map and not an outer logic?
if you need a 3 key map, why not just have 3 maps?
Sylwester Rąpała
@xoac
Jul 06 2018 15:32
I need one value
Dmitriy
@dpogretskiy
Jul 06 2018 15:32
then you can store reference to a value
Zakarum
@omni-viral
Jul 06 2018 15:32
@dpogretskiy He needs to find and intersect of three Map<K, Set<V>>
Sylwester Rąpała
@xoac
Jul 06 2018 15:33
this don;t have to be the value map. Sth like SQL API on memor-in db ..
Dmitriy
@dpogretskiy
Jul 06 2018 15:34
well, there are couple of dozens of very specific questions you need to answer to find fitting solution
piperRyan
@piperRyan
Jul 06 2018 15:45
@xoac are you looking for an intersection of multiple sets or do you just have one set of that data that is functionally determined by three keys? I am reading your message as either one.
tsoernes
@tsoernes
Jul 06 2018 17:27

In ndarray, why is there a difference between a (non-mut) reference to a
OwnedRepr (owned array) and a ViewRepr?
Both are read-only, so what's the difference? The functions available for a
OwnedRepr is a strict subset of those available for ViewRepr's.
How to make function that works for both without copying?
Say you have a function fn that works read-only on (refs to) 1D arrays,
i.e. vectors.
How can you construct that function so that you can, without copying,
a) Pass a (ref to a) regular vector, i.e. fn(&arr1(&[0, 1]))
b) Pass rows of a 2D vector, i.e.

for some_1d_arr in some_2d_arr.outer_iter() {
    fn(&some_1d_arr);
}

One solution, I suppose, would be to make the function take views only,
and convert owned arrays to views each time.
That seems pretty verbose and unnecessary though.

tsoernes
@tsoernes
Jul 06 2018 17:40
if you make a function that takes &Array, you can't pass things from outer_iter, which are ArrayViews
if you make a function that takes ArrayView or &ArrayView you can't pass regular arrays
Denis Lisov
@tanriol
Jul 06 2018 19:03
@tsoernes Like the ndarray methods are?
tsoernes
@tsoernes
Jul 06 2018 19:06
@tanriol by making traits?
Denis Lisov
@tanriol
Jul 06 2018 19:07
By being generic over the array(view) type
tsoernes
@tsoernes
Jul 06 2018 19:07
Can you give a simple, concrete example?
Denis Lisov
@tanriol
Jul 06 2018 19:09
Something like my_func<E, A, S, D>(arr: A) where A: ArrayBase<S, D>, S: Data<Elem = E>, D: Dimension
tsoernes
@tsoernes
Jul 06 2018 19:16
@tanriol this is the closest I got:
extern crate ndarray;
use ndarray::*;
use std::fmt::Debug;

fn pfn<E, A: Debug, S, D>(arr: A)
where
    S: Data<Elem = E>,
    D: Dimension,
    E: std::fmt::Debug,
{
    println!("{:?}", arr);
}

fn main() {
    let a: Array2<usize> = arr2(&[[1, 2], [3, 4]]);
    let b: Array1<usize> = arr1(&[5, 6]);
    // Err below:
    // type annotations required: cannot resolve `_: ndarray::Data` (rust-cargo)
    pfn(b);
    pfn(b.view());
    for avec in a.outer_iter() {
        pfn(avec);
    }
}
if you do where A: ArrayBase<S, D>, you get expected trait, found structArrayBase(not a trait)
Hey, I got it!
fn pfn<E, S, D>(arr: ArrayBase<S, D>)
where
    S: Data<Elem = E>,
    D: Dimension,
    E: std::fmt::Debug,
{
    println!("{:?}", arr);
}
thank you @tanriol
tsoernes
@tsoernes
Jul 06 2018 19:35
how would you make a function equal to pfn which only take array of bools, i.e. Elem = bool?

I tried the obvious:

fn pfn<S>(arr: ArrayBase<Data<Elem=bool>, D>)
where
    D: Dimension,
{
    println!("{:?}", arr);
}

to no avail

Denis Lisov
@tanriol
Jul 06 2018 19:50
Try the obvious:
fn pfn<S, D>(arr: ArrayBase<S, D>)
where
    S: Data<Elem = bool>,
    D: Dimension,
{
    println!("{:?}", arr);
}
tsoernes
@tsoernes
Jul 06 2018 20:03
@tanriol how do you make a type alias for that parameter?
given that dimenion is fixed, e.g. Ix3
tsoernes
@tsoernes
Jul 06 2018 20:09
Okay, found it. type Grid<S: Data<Elem = bool>> = ArrayBase<S, Ix3>; I don't see how it's obvious though, that D: dimension can be replaced with the specific instance Ix3 while S cannot be replace by (some wrapper around) bool.
Denis Lisov
@tanriol
Jul 06 2018 20:12
The exact S contains not only the element type information (bool), but also the storage information (OwnedRepr vs OwnedArcRepr vs ViewRepr), which is exactly what you wanted to abstract over :-)
tsoernes
@tsoernes
Jul 06 2018 20:13
I see. Thank you.
Denis Lisov
@tanriol
Jul 06 2018 20:13
Depending on which wrapper you use, it is either an Array or an ArrayView or something else
Kelly Thomas Kline
@kellytk
Jul 06 2018 20:34
Is https://stackoverflow.com/questions/37430628/rust-constants-in-different-modules/37430681#37430681 still the preferred method of using a common const across and within a project?
Denis Lisov
@tanriol
Jul 06 2018 20:56
One possible method. The "preferred" one depends on your situation.
Kelly Thomas Kline
@kellytk
Jul 06 2018 21:17
Thanks