These are chat archives for rust-lang/rust

31st
Aug 2018
Nicolas Bigaouette
@nbigaouette
Aug 31 2018 02:43
I'm trying to compile a hello world with a single extern crate (not even using it...) This crate depends itself on another one that has a features flag which I cannot disable and it prevents me from linking the whole thing... Is it possible to control transitive dependencies' features?
Martell Malone
@martell
Aug 31 2018 04:57

hmm, I've been searching for an answer to this in the rust book the docs and using google foo but I'm unsure if the following is okay to do.


pub trait Hello: Sized {
    fn woothere(self);
}

impl<T> Hello for T {
    fn woothere(self) {
        ()
    }
}

fn main() {
    let foo = 42;
    foo.woothere()
}

Is it okay to impl Hello for T without specifying trait conditions for T in practice?
Beside the danger of someone else using the code base calling .woothere() in the wrong place is there anything else I should keep in mind.
I assume auto complete tools will be messy with this kind of use also.
I picked up the rustbook again today after a long time and did a bit of a refresher, it has been awhile :D

Also if this is not the place to ask such a question please feel free to point me somewhere else :)
Martell Malone
@martell
Aug 31 2018 05:26
I should read more, I bet there is something about traits and file context.
mtak-
@mtak-
Aug 31 2018 05:36
the compiler will yell at you if you get some trait math wrong. in generics impl<T> automatically requires Sized. to opt out of Sized change it too impl<T: ?Sized>. of course if you do that, the compiler will yell at you :)
mtak-
@mtak-
Aug 31 2018 05:45
https://doc.rust-lang.org/book/first-edition/unsized-types.html
Sized is the only trait which is by default required for generic types
Derk Bell
@derkbell
Aug 31 2018 07:06
@tanriol @TeXitoi thanks, I've tried a lot but I always end up with rust seeing different types. I'm thinking of moving this to compile time, the vector contains static stuff so I could have a macro that sets up an enum or a tuple with the different types
Denis Lisov
@tanriol
Aug 31 2018 08:54
@derkbell The compiler will see different types with this implementation, that's expected. What do you use the Output for?
Sylwester Rąpała
@xoac
Aug 31 2018 10:39
So trivial things to do. And I don't know how. It's minimal example. (mormaly I use _a and _b) https://play.rust-lang.org/?gist=3c71aa195d13003ff57109b8a68974d5&version=stable&mode=debug&edition=2015
Denis Lisov
@tanriol
Aug 31 2018 10:41
How about match self.e?
Vitaly
@Virtuos86
Aug 31 2018 10:43
@xoac match (*self).e
Sylwester Rąpała
@xoac
Aug 31 2018 10:44
I need to call poll() on the _a or _b so it takes &mut self.
Denis Lisov
@tanriol
Aug 31 2018 10:46
One possible way is to use options and take
What are you trying to do, actually?
Sylwester Rąpała
@xoac
Aug 31 2018 10:51
I need run future a. if it resolve with ok it's end but if not I need call another one b. And if b successes I need poll a.
Denis Lisov
@tanriol
Aug 31 2018 10:55
So at any moment you're either waiting for future a or for future b?
Sylwester Rąpała
@xoac
Aug 31 2018 10:56
I am waiting just for one. But I need to know with one.
Updated minimal example https://play.rust-lang.org/?gist=8b2a20cc7885c64465df297e61c3c537&version=stable&mode=debug&edition=2015
Denis Lisov
@tanriol
Aug 31 2018 10:58
This will compile as written with NLL (which is expected in Rust 2018)
Sylwester Rąpała
@xoac
Aug 31 2018 10:58
Nice :P And how can I made this work for now?
Denis Lisov
@tanriol
Aug 31 2018 11:00
For now you can work it around with something like this
Sylwester Rąpała
@xoac
Aug 31 2018 11:02
I will try it. But take() make self.e None. So I need to insert future every time not just when it resolved?
Denis Lisov
@tanriol
Aug 31 2018 11:02
Exactly.
Either put the same future back or a different one.
Sebastian
@sertel
Aug 31 2018 11:06
Hi! I’m currently exploring Rusts macros and miss the following function known from Clojure/Lisp: macroexpand. Is there anything alike available? I know there is stringify! but it will only turn the expression that calls my macro into a string. But I want it to turn its expansion into a string.
Sylwester Rąpała
@xoac
Aug 31 2018 11:17
@tanriol Thank you
Sebastian
@sertel
Aug 31 2018 12:30
@Virtuos86 Thanks for that pointer but there is no such function. These are essentially just command line argument and none of them is printing even the expanded code.
Oh, I see. You mean the rustc —pretty expanded flag, I guess
Vitaly
@Virtuos86
Aug 31 2018 12:32
@sertel Macros works in compile-time, functions — in runtime. No way
Sebastian
@sertel
Aug 31 2018 12:32
I actually wanted to get hold on the created code as a string in order to be able to build a test case that compares the generated code to the expected one.
Ok, in Rust one can not call functions from macros. In Clojure/Lisp, this is different. So, I guess in Rust -speak I’m looking for a macro that does the same as macroexpand in Clojure/Lisp.
Sadly, when I write stringify!(vec![1,2,3]) it will just print me ”vec ! [ 1 , 2 , 3 ]”
Vitaly
@Virtuos86
Aug 31 2018 12:36
@sertel I think, you need a procedural macro
Sebastian
@sertel
Aug 31 2018 12:37
Why would that help me?
As far as I understand, a procedural macro is similar to a reader macro in Lisp. That means it is being executed even before the Syntactic Macro runs. But then, what would I want to do there or generate there?
Do you have a specific idea?
Zakarum
@omni-viral
Aug 31 2018 13:14
Is this snippet safe?

fn foo<T, U>(t: T) -> U {
    use std::{any::TypeId, mem::transmute};
    if TypeId::of::<T>() == TypeId::of::<U>() {
        unsafe { transmute(t) }
    } else {
        panic!()
    }
}
Tom Cumming
@tomcumming
Aug 31 2018 13:27
is having a _secret: () field the idiomatic way to make a struct constructor private ?
Denis Lisov
@tanriol
Aug 31 2018 13:28
For now it is (if you need all the regular fields to be public, that is).
Vitaly
@Virtuos86
Aug 31 2018 13:29
@sertel yes, you're right
Denis Lisov
@tanriol
Aug 31 2018 13:29
There's an accepted RFC for #[non_exhaustive], but it's not stable.
Lyle Mantooth
@IslandUsurper
Aug 31 2018 14:45
@gitlabaccount624_gitlab, the error says the header has the wrong value, but I don't see any code where you're setting it.
Is .allowed_headers() for request headers or response headers?
I don' t even know what crate that's from, so I have no clue.
Lyle Mantooth
@IslandUsurper
Aug 31 2018 14:52
Just found that page. Sounds like it will.
John
@gitlabaccount624_gitlab
Aug 31 2018 15:17
omg
i'm in the wrong channel
i thought i was in the actix channel
deleting everything..
Denis Lisov
@tanriol
Aug 31 2018 15:18
@gitlabaccount624_gitlab Don't delete messages others have replied to, it's kinda impolite and makes them look strange :-)
John
@gitlabaccount624_gitlab
Aug 31 2018 15:18
well they won't look strange after they read my message
Lyle Mantooth
@IslandUsurper
Aug 31 2018 16:06
Ha. I did think it was strange there was no context around which middleware you were using. ;)
Nicolas Bigaouette
@nbigaouette
Aug 31 2018 17:37
Can I use a { path = "..." } dependency to point to a directory containing a workspace (multiple crates)?
If I point to the workspace root dir, I get an error found a virtual manifest at ... instead of a package manifest
If I point to the exact crate inside the workspace, it works.
I though cargo was smart enough to visit the workspace?
Robert Masen
@FreeMasen
Aug 31 2018 17:39
If there are more than one crate in your workspace how would cargo know which crate you were trying to use?
Nicolas Bigaouette
@nbigaouette
Aug 31 2018 17:40
With the name of the crate serde = { path = ".../serde.git" }?
This works: serde = { git = "https://github.com/serde-rs/serde.git" }. This doesn't work: serde = { path = "../serde" }
Nicolas Bigaouette
@nbigaouette
Aug 31 2018 17:47
Using git = "file://..." does work...