These are chat archives for rust-lang/rust

27th
Apr 2018
David Lewis
@davidarmstronglewis
Apr 27 2018 05:59
Does anyone have any experience writing libraries to work with restful apis? I'm writing one for a third party service, but want the types I define to be usable with whatever http client the user wants to use - along with an implementation I'll provide in case the user just wants a "batteries included" experience. The http crate looks like a great place to start, maybe an Endpoint build that implements Into for an Http Request - not sure if I can use that generic type with other crates like hyper or reqwest however.
prataprc
@prataprc
Apr 27 2018 09:11
https://is.gd/qblJHJ in this code, isy allocated in stack or heap ? If in stack, I suppose this allocation should happen in runtime isn't it ?
Michal 'vorner' Vaner
@vorner
Apr 27 2018 09:25
It's on stack. And if it's on stack, it isn't called allocation, the process of just using the stack frame memory is much simpler than that (each function knows how much stack space its variables take, so the stack pointer is bumped by that number on entry to that function and decreased on exit).
But the change of the stack pointer does happen at runtime.
prataprc
@prataprc
Apr 27 2018 09:27
Thanks for pointing out.
If I change let y = [ x*2; 20]; to let y = [20; x*2];
Compiler cribs: error[E0435]: attempt to use a non-constant value in a constant
Michal 'vorner' Vaner
@vorner
Apr 27 2018 09:28
Yes, you can use only compile-time known sizes for arrays. If you want to compute the size at runtime, you need Vec, which allocates its data on the heap.
prataprc
@prataprc
Apr 27 2018 09:28
while the initial value can be expression, length must be a constant. Make sense.
@vorner thanks.
Michal 'vorner' Vaner
@vorner
Apr 27 2018 09:29
There are some pushes to allow the later too, but these are still in the process of discussion.
prataprc
@prataprc
Apr 27 2018 09:30
Oh vow! I that is cool. Hope it comes through.
Michal 'vorner' Vaner
@vorner
Apr 27 2018 09:31
Probably eventually, but it's being discussed/the discussion is not very active for quite a long time… so maybe some more time will be needed. It's like most people think it is a good idea, but there's nobody to actively push it forward I think (I might be wrong there, it's just my impression)
prataprc
@prataprc
Apr 27 2018 09:32
okay. are there any links ?
Michal 'vorner' Vaner
@vorner
Apr 27 2018 09:33
I don't have them at hand, but searching for things like DST (Dynamically Sized Types) might yield some results.
prataprc
@prataprc
Apr 27 2018 09:33
Sure will do.
Dylan DPC
@Dylan-DPC
Apr 27 2018 11:34
suppose i have a struct Foo<T,U> is there a way i can specify U without having it on one of the members?
Michal 'vorner' Vaner
@vorner
Apr 27 2018 11:35
No. But you can use something like _u: PhantomData<U> to work around that.
Dylan DPC
@Dylan-DPC
Apr 27 2018 11:38
uhmm okay. yeah that's what the error says as well. Thanks
but i need U as a type later, so it won't affect that right?
oh ok got it now
Paul Masurel
@fulmicoton
Apr 27 2018 12:14
yes it's ok. And it has no cost whatsoever.
Michal 'vorner' Vaner
@vorner
Apr 27 2018 12:17
Actually, it has some effect (not on the performance or size), to some properties of the type. The phantom data pretends that there's some U actually inside ‒ so, for example, if U is not Sync, the whole data structure won't be either. That's often what you want if you have reasons to make it generic over U, though.
Paul Masurel
@fulmicoton
Apr 27 2018 12:30
oh interesting
Dylan DPC
@Dylan-DPC
Apr 27 2018 12:41
its ok . as usual whenever i get stuck with such problems, I then realise I may not need it and end up rewriting it in another way :D
Hayden Jones
@wrq
Apr 27 2018 14:18
is there a simple way to get a pdf of the rust book second edition?
I want to read it, but I'm gonna be offline for a while
Hayden Jones
@wrq
Apr 27 2018 14:44
so no, then. that's slightly incredible, that nobody has thought to slap a link to it somewhere on rust-lang/book
Michal 'vorner' Vaner
@vorner
Apr 27 2018 15:55
I had some success rendering it myself with mdbook-pdf
Actually, no, mdbook-epub (but you can take that one offline too)
prataprc
@prataprc
Apr 27 2018 17:32
There is a print icon on web version
I could save it as a PDF.
But there is no table of contents.
Dmitry Golubets
@DGolubets
Apr 27 2018 19:27
Hi guys, I'm bit confused implementing extension trait for my custom future:
pub trait IntoEzFuture<'a, T> {
    fn into_ezfuture(self) -> EzFuture<'a, T>;
}

pub trait IntoEzFuture2<'a, T> {
    fn into_ezfuture(self) -> EzFuture<'a, T>;
}

impl<'a, F, T> IntoEzFuture<'a, T> for F
    where F: Future<Item=T, Error=Error> + 'a {
    fn into_ezfuture(self) -> EzFuture<'a, T> {
        EzFuture(Box::new(self))
    }
}

impl<'a, T: 'a> IntoEzFuture2<'a, T> for Result<T, Error> {
    fn into_ezfuture(self) -> EzFuture<'a, T> {
        EzFuture(Box::new(self.into_future()))
    }
}
As you see I want to be able to convert any kind of future to my type
and a Result
I though I could implement one trait for both
but then it complains about conflicting implementation
2 traits kinda works, but it feels wrong
how would you do it?
Hayden Jones
@wrq
Apr 27 2018 19:44
I used rustup-doc, settling for reading it on my laptop for now. Will investigate other suggestions later, thank you
diegor8
@diegor8
Apr 27 2018 20:20
Hello , is this place appropriate to ask whether a rust feature can be replicated in c++ ?
Hans W. Uhlig
@huhlig
Apr 27 2018 20:20
which one?
diegor8
@diegor8
Apr 27 2018 20:21
( automatic implementation ) of class constructor
Hans W. Uhlig
@huhlig
Apr 27 2018 20:21
uhh… rust doesn’t have constructors
diegor8
@diegor8
Apr 27 2018 20:22
The equivalent
Hans W. Uhlig
@huhlig
Apr 27 2018 20:22
so… im not sure how you would replicate it in c++
diegor8
@diegor8
Apr 27 2018 20:22
You can add a macro above functions in rust
Hans W. Uhlig
@huhlig
Apr 27 2018 20:22
you mean derive
diegor8
@diegor8
Apr 27 2018 20:22
Yes
Is there some way class constructors can be implemented ?
Automatically
Getters and setters can be ( kind of )
diegor8
@diegor8
Apr 27 2018 20:24
The way I
Hans W. Uhlig
@huhlig
Apr 27 2018 20:24
derive isn’t really automatic constructors
its procedural code generation
Michal 'vorner' Vaner
@vorner
Apr 27 2018 20:25
As far as I know C++, quite anything can be created with template dark magic, but don't ask me how (I don't know and even if I did, I wouldn't probably want to admit it)
Hans W. Uhlig
@huhlig
Apr 27 2018 20:25
templates and macros
it just won’t be clean
however if you’re looking for a automatically generated constructor the default constructor should do what you want
diegor8
@diegor8
Apr 27 2018 20:30
So default constructors do let you set the values to the attributes of a struct
Like the new obtained by derive ?
Hans W. Uhlig
@huhlig
Apr 27 2018 21:12
again, derive is just a procedural macro
what specifically are you talking about
#[derive(Default)]?