1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // RFC 736 (and Issue 21407): functional struct update should respect privacy.
15 // The `foo` module attempts to maintains an invariant that each `S`
16 // has a unique `u64` id.
19 use std::cell::{UnsafeCell};
21 static mut count : UnsafeCell<u64> = UnsafeCell::new(1);
23 pub struct S { pub a: u8, pub b: String, secret_uid: u64 }
25 pub fn make_secrets(a: u8, b: String) -> S {
26 let val = unsafe { let p = count.get(); let val = *p; *p = val + 1; val };
27 println!("creating {}, uid {}", b, val);
28 S { a: a, b: b, secret_uid: val }
33 println!("dropping {}, uid {}", self.b, self.secret_uid);
39 let s_1 = foo::make_secrets(3, format!("ess one"));
40 let s_2 = foo::S { b: format!("ess two"), ..s_1 }; // FRU ...
41 //~^ ERROR field `secret_uid` of struct `foo::S` is private
42 println!("main forged an S named: {}", s_2.b);
43 // at end of scope, ... both s_1 *and* s_2 get dropped. Boom!