These are chat archives for rust-lang/rust

28th
Apr 2018
Josh
@joshlemer
Apr 28 2018 00:06

Hey I was wondering, why does A need to have statically known size here?

struct Foo<T>  {
    t: Box<T>
}

trait A {}

fn f(arg: Foo<A>) -> () {} // compiler complains

the error returned is

  --> src/main.rs:29:1
   |
29 | fn f(arg: Foo<A>) -> () {}
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^ `A + 'static` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `A + 'static`
note: required by `Foo`
  --> src/main.rs:23:1
   |
23 | struct Foo<T>  {
   | ^^^^^^^^^^^^^
Hans W. Uhlig
@huhlig
Apr 28 2018 00:07
stack space in f
Josh
@joshlemer
Apr 28 2018 00:07
I thought that it shouldn't care about the size of A since it is in a box?
Hans W. Uhlig
@huhlig
Apr 28 2018 00:07
hmm..
Josh
@joshlemer
Apr 28 2018 00:07
so therefor size of A should have no effect on size of Foo<A>
Hans W. Uhlig
@huhlig
Apr 28 2018 00:09
I think it needs to be Foo<T> where T: A
There is a reason, I’m just mentally blanking on it
Josh
@joshlemer
Apr 28 2018 00:10
yeah that would be for static disppatch right, but if I wanna use dynamic dispatch / trait objects right?
Hans W. Uhlig
@huhlig
Apr 28 2018 00:10
I believe so
Vyacheslav Kim (Kane)
@kanekv
Apr 28 2018 04:30
you need &Foo<T> probably
Guillaume P.
@TeXitoi
Apr 28 2018 07:20
@joshlemer by default, T is sized, you can opt it explicitly:
pub struct Foo<T: ?Sized>  {
    t: Box<T>
}

pub trait A {}

pub fn f(_: Foo<A>) -> () {}
Dylan DPC
@Dylan-DPC
Apr 28 2018 08:45
How do i specify the associated type Error when writing my own implementation for deserialization?
Denis Lisov
@tanriol
Apr 28 2018 09:00
@Dylan-DPC Do you mean writing your own serialization format? The error type is a property of the data format, not the type being deserialized.
Kelly Thomas Kline
@kellytk
Apr 28 2018 10:18
If you've used Yew for a web project how was your experience?
prataprc
@prataprc
Apr 28 2018 13:44
dabbling with Iterator. looks like map on &[i32].iter() takes &T as argument while take_while takes &&T.
Is that correct ?
Geoff Shannon
@RadicalZephyr
Apr 28 2018 13:47
@prataprc I think what's happening is that iter creates an iterator over &i32, and then the take_while fn is passed a &Item, thus it gets passed &&i32
prataprc
@prataprc
Apr 28 2018 13:48
Is there a reason why take_while is passing it as &Item ?
Geoff Shannon
@RadicalZephyr
Apr 28 2018 13:48
you could unwrap one level by using into_iter() to make an owning iterator. i.e. an iterator over i32
I presume because the take_while fn is a conditional. If it passed it's arg not as a ref, then the checked values would not be in the collection anymore
they would get moved into the conditional function. But when you use take_while you almost always want those values to be actually used for something later. Thus the conditional has to get a reference
prataprc
@prataprc
Apr 28 2018 15:47
@RadicalZephyr Yeah make sense.