rust - Implementing Default on nested structures causes a stack overflow -
i found unusual behavior of std::default. if create nested structure default setters , try create highest level struct default parameters, causes stack overflow.
this code compiles, when try run throws thread '<main>' has overflowed stack:
use std::default; pub struct { x: i32 } impl default { fn default() -> { { ..default::default() } } } fn main() { let test = { ..default::default() }; } but if set defaults of inherited props, works:
use std::default; pub struct { x: i32 } impl default { fn default() -> { { x: 0 } } } fn main() { let test = { ..default::default() }; } is known issue , should post rust issue tracker? compiler version rustc 1.2.0-nightly (0250ff9a5 2015-06-17).
of course causes stack overflow.
to make clearer, let's change example little:
pub struct { x: i32, y: i32, } impl default { fn default() -> { { x: 1, ..default::default() } } } when a { ..default::default() }, are not saying "create me a , execute default::default() each field". means implementation of default not equivalent to:
a { x: 1, y: default::default() } it is, in fact, this:
let temp: = default::default(); temp.x = 1; temp so yes, causes stack overflow: you've defined default value of a in terms of itself.
Comments
Post a Comment