These are chat archives for rust-lang/rust

22nd
Jul 2018
tsoernes
@tsoernes
Jul 22 2018 01:07
how to invert a vec of bools? mapping seems very inefficient.
matrixbot
@matrixbot
Jul 22 2018 01:13
bspeice Would .iter().rev() work? Reference here
Ichoran
@Ichoran
Jul 22 2018 02:52
Invert meaning true to false, or opposite order?
Dmitriy
@dpogretskiy
Jul 22 2018 07:18
@tsoernes use bitset instead?
and xor it with 1s
Akos Vandra
@axos88
Jul 22 2018 09:13
Where can I find information of overhead for wrapper types? Scala has ValueTypes for example that do not have any overhead, I’m interested in something similar, concretly wether making this prettier notation comes with an overhead or not:
enum Presence {
    Required,
    Optional
}

impl From<bool> for Presence {
    fn from(v: bool) -> Self {
        match v {
            true => Presence::Required,
            false => Presence::Optional
        }
    }
}

impl Into<bool> for Presence {
    fn into(self) -> bool {
        match self {
            Presence::Required => true,
            Presence::Optional => false
        }
    }
}

impl Default for Presence {
    fn default() -> Self {
        Presence::Optional
    }
}
Denis Lisov
@tanriol
Jul 22 2018 10:14
Approximately zero overhead. However, are you sure that you really need From<bool>, Into<bool> and Default implementations? I'd probably avoid at least the first two.
Akos Vandra
@axos88
Jul 22 2018 10:19
Not sure, i’m reimpllementing a bit of code, and might be able to get away with removing them at the end
what’s the overhead?
Sajjad Pourali
@SajjadPourali
Jul 22 2018 10:20

I’m writing a http app via actix-web that have many http requests (something such as a web crawler (but as a service))
running actix system in each method call is logical ?
somthing like this :

fn http(x:String) {
    actix::run(
        || client::get(&x)
            .header("User-Agent", "Actix-web")
            .finish().unwrap()
            .send()
            .map_err(|_| ())
            .and_then(|response| {
                println!("Response: {:?}", response);
                Ok(())
            }),
    );
}

Notice: Sometimes requests are really heavy for example i must send 500 request in a second.

Akos Vandra
@axos88
Jul 22 2018 10:21
@SajjadPourali there’s an actix comunity on gitter you may be better off asking there
Sajjad Pourali
@SajjadPourali
Jul 22 2018 10:23
@axos88 I asked it there too.
Denis Lisov
@tanriol
Jul 22 2018 10:30
@axos88 Well, I'd expect it to be zero - the LLVM optimizer is pretty good. Or almost zero in case you use From/Into often (these are not no-ops, but very close to that).
@SajjadPourali No, using actix::run from each request is certainly not a good idea.
Michal 'vorner' Vaner
@vorner
Jul 22 2018 10:50
Nightly had this nice progress bar thing for a while when compiling. But it is gone now. Anybody knows why?
Zakarum
@omni-viral
Jul 22 2018 10:52
@tanriol @axos88 From/Into will give you zero overhead if representation is the same. Which might be the case if swap order in enum. But no guarantee
tsoernes
@tsoernes
Jul 22 2018 11:56
@tanriol why would you avoid from/into bool?
Denis Lisov
@tanriol
Jul 22 2018 12:02
@tsoernes Well, the idea of Presence::{Required, Optional} is probably to be obvious to the reader. A bool is not obvious here. If one does let raw: bool = presence.into();, it's not obvious at all which value is treated as true. At least, instead of Into I'd use a method fn is_required(self) -> bool
tsoernes
@tsoernes
Jul 22 2018 12:04
makes sense, thank you
Akos Vandra
@axos88
Jul 22 2018 12:14
Is there any way to define negative trait bounds?
Such as:
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub enum Validation<T> {
    // This field is not validated
    Ignored,

    // This field is validated using T
    Validate(T)
}

// This doesn't compile currently
impl<T> Debug for Validation<T> where T: ?Debug {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), Error> {
        match self {
            Validation::Ignored => write!(f, "Ignored"),
            Validation::Validate(_) => write!(f, “Validate using an unprintable value")
        }
    }
}
Denis Lisov
@tanriol
Jul 22 2018 12:16
@axos88 No, in general you cannot depend on the fact a trait is not implemented for something. Specialization will probably fix that, but it's unstable at the moment.
Akos Vandra
@axos88
Jul 22 2018 12:18
Gotcha. What’s the usual timeline for stabilizing features btw? few months?
Akos Vandra
@axos88
Jul 22 2018 12:30
During tests is the recommended way to test equality of Result::Err, or to unwrap, and test for panic? My gut sais the first one is nicer and should be faster too
Denis Lisov
@tanriol
Jul 22 2018 12:31
During tests you can just unwrap, as a panic is the correct signal for test failure.
Akos Vandra
@axos88
Jul 22 2018 12:32
Sorry, I did not include that I want to make sure the correct error occured
Denis Lisov
@tanriol
Jul 22 2018 12:32
Specialization is long unstable and will probably be unstable for more time... I'd guess a year at least.
Then an equality test is better, yes.
Akos Vandra
@axos88
Jul 22 2018 12:35
Thx!
A Dinesh
@dineshadepu
Jul 22 2018 12:37
Quick question. I have have bunch of examples, I want to add them to toml file, since I am organising them into sub folders
What's the syntax
I remember it to be similar to specifying binaries
and binary name and binay files
Denis Lisov
@tanriol
Jul 22 2018 12:38
See the docs
A Dinesh
@dineshadepu
Jul 22 2018 12:39
Will there be path variable in it? @tanriol
Denis Lisov
@tanriol
Jul 22 2018 12:39
Yes
Roman Proskuryakov
@kpp
Jul 22 2018 12:40
Well, you may put them into examples directory and cargo will compile them when you call cargo build --examples
A Dinesh
@dineshadepu
Jul 22 2018 12:41
That's true
But what If I have 5 subfolders?
So I have to manually add them to toml
Roman Proskuryakov
@kpp
Jul 22 2018 12:41
Yes
A Dinesh
@dineshadepu
Jul 22 2018 12:41
That was the question
Thanks @kpp , @tanriol
Ichoran
@Ichoran
Jul 22 2018 18:47
@axos88 - Actually, value types in Scala do have overhead sometimes. They absolutely have overhead in extra forwarders unless the JIT compiler can remove them (usually can), and if they extend a trait that triggers object creation (as do a number of other things). The situation is considerably simpler in Rust. I don't know about your exact case, but if the difference between zero and minimal is important to you, you should be examining assembly (play.rust-lang.org makes it easy, for instance) and/or benchmarking.
Judson Lester
@nyarly
Jul 22 2018 19:14
Are transitive conversion traits supported?
e.g. if I have a fn that accepts From<X> (and X isn't in my crate), and I want to pass a Y, can I implement From<Y> and Into<X> on my own type?
Robin Miller
@grenewode
Jul 22 2018 19:22
@nyarly Yes, as long as Y is in your crate. Also, you only need to implement From<Y> for X, and Into<X> for Y> will be implemented for you. For example, see this: https://play.rust-lang.org/?gist=07d266d172e34224834bbd29fd7513e3&version=stable&mode=debug&edition=2015
Judson Lester
@nyarly
Jul 22 2018 19:24
I was familiar with the auto-impl of Into. It was "if I need From<X>, and I have a Y, can impl From<Y> on A, and Into<X> on A, and then be able to fn(y)?" Or will rustc not like that level of fiddly.
Judson Lester
@nyarly
Jul 22 2018 19:35
To what degree is it allowed under orphan rules to implement Trait<MyType>? Is there a special exception for convert, or a clause in the orphan rules I don't understand?
Denis Lisov
@tanriol
Jul 22 2018 19:35
@nyarly What do you want to do?
Judson Lester
@nyarly
Jul 22 2018 19:37
re the orphan rules, I was looking at Robin's second example there.
From<String> for MyString and From<MyString> for String.
The latter surprises me when I see it, because I own neither From nor String.
What makes it allowed?
Robin Miller
@grenewode
Jul 22 2018 19:38
You don't own the From trait, but you do own the From<MyString> trait.
Judson Lester
@nyarly
Jul 22 2018 19:39
But would I own NotMyTrait<MyString, String>?
what about NotMyTrait<MyString, MyInt>?
Denis Lisov
@tanriol
Jul 22 2018 19:40
IIRC (may be mistaken), the first type in the trait has to be yours...
Judson Lester
@nyarly
Jul 22 2018 19:41
Oh, that's important to know when defining traits.
Robin Miller
@grenewode
Jul 22 2018 19:42
I'm not certain, but I would be surprised if it was that strict. I think if you don't own the trait, but you pass it a generic parameter that you do own, you should be able to implement it.
Judson Lester
@nyarly
Jul 22 2018 19:42
But I'm having a hard time finding anything definitive.
One owned parameter makes it okay?
Robin Miller
@grenewode
Jul 22 2018 19:42
wait, no
Denis Lisov
@tanriol
Jul 22 2018 19:42
The best explanation is probably this blog post (the "covered first" version)
nyarly @nyarly Judson's furrowed brow is starting to hurt.
Denis Lisov
@tanriol
Jul 22 2018 19:43
We can improve the covered self approach by saying that some type parameter of the trait must meet the rules (some local type; impl type params covered by a local type), but not necessarily Self. Any type parameters which precede this covered parameter must consist exclusively of remote types (no impl type parameters, in particular).
Judson Lester
@nyarly
Jul 22 2018 19:44
My read there is that that was a proposal. Did it get implemented?
Related to coherence, is it possible to do blanket impls with exceptions?
Denis Lisov
@tanriol
Jul 22 2018 19:44
rust-lang/rfcs#1856 implies so
Judson Lester
@nyarly
Jul 22 2018 19:45
In effect to say "a type that doesn't implement X itself, this is the implementation of X?"
Denis Lisov
@tanriol
Jul 22 2018 19:45
AFAIK, no way. By the way, are you asking in the context of Gotham?
Judson Lester
@nyarly
Jul 22 2018 19:46
I've been contemplating a Rust WebMachine implementation, but it's existing approach relies on inheritance.
Not really, no.
Alternatively, a match for impl X vs doesn't
I think I recall something like ?X coming?
Denis Lisov
@tanriol
Jul 22 2018 19:48
What you want sounds like specialization and it's still ways off...
Judson Lester
@nyarly
Jul 22 2018 19:49
Okay. I will accept pipe dreams blocked on language features!
Judson Lester
@nyarly
Jul 22 2018 21:05
It seems (currently?) impossible to define a derive for a parameterized trait. Is that true?
IOW, the compiler complains either that parameters have to be supplied in the derive, or that the < is unexpected.
Judson Lester
@nyarly
Jul 22 2018 21:18
examples/query_string/introduction/src/main.rs|45 col 34 error 244| wrong number of type arguments: expected 0, found 1
||    |
|| 45 | #[derive(Deserialize, StateData, StaticResponseExtender)]
||    |                                  ^^^^^^^^^^^^^^^^^^^^^^ expected no type arguments
examples/query_string/introduction/src/main.rs|45 col 34 error 243| wrong number of type arguments: expected 1, found 0
||    |
|| 45 | #[derive(Deserialize, StateData, StaticResponseExtender)]
||    |                                  ^^^^^^^^^^^^^^^^^^^^^^ expected 1 type argument
Which is it, rustc?
Denis Lisov
@tanriol
Jul 22 2018 21:22
@nyarly Wanted to ask, what's the reason for this separate StaticResponseExtender trait?
Judson Lester
@nyarly
Jul 22 2018 21:23
Excellent question!
At the moment, I'm trying to perturb Gotham as little as possible as I update Hyper.
It's been an ongoing challenge, but SRE is an existing type.
I think the upshot is that it's common enough to want to reply e.g. to 404 with a static reponse to make it a derivable trait.
Denis Lisov
@tanriol
Jul 22 2018 21:28
So, IIUC, it's only purpose is to indicate what's to be done if the extractor fails and the derived version returns a BadRequest, correct?
Judson Lester
@nyarly
Jul 22 2018 21:28
I believe that's the case, yes.
Reviewing the code just now, that's what I think.
Denis Lisov
@tanriol
Jul 22 2018 21:30
By the way, the hyper 0.12 work happens on your hyper-update branch, correct? I still intend to suggest some simpler handler API, but probably after 0.12 to avoid delaying it...
Judson Lester
@nyarly
Jul 22 2018 21:37
It does.
And yes, trying my best to getting that done ASAP so that things like simpler handler APIs can advance.
Denis Lisov
@tanriol
Jul 22 2018 22:08
Sorry, looks like I was too eager to answer...
Denis Lisov
@tanriol
Jul 22 2018 22:38
@nyarly Looks like I've found the derive errors with argument count
The trait name does not need one, while the Response in the impl does
Judson Lester
@nyarly
Jul 22 2018 23:55
Yeah, I tidied that up. It's just a weird error.