1 // RFC 736 (and Issue 21407): functional struct update should respect privacy.
3 // The `foo` module attempts to maintains an invariant that each `S`
4 // has a unique `u64` id.
7 use std
::cell
::{UnsafeCell}
;
9 static mut COUNT
: UnsafeCell
<u64> = UnsafeCell
::new(1);
11 pub struct S { pub a: u8, pub b: String, secret_uid: u64 }
13 pub fn make_secrets(a
: u8, b
: String
) -> S
{
14 let val
= unsafe { let p = COUNT.get(); let val = *p; *p = val + 1; val }
;
15 println
!("creating {}, uid {}", b
, val
);
16 S { a: a, b: b, secret_uid: val }
21 println
!("dropping {}, uid {}", self.b
, self.secret_uid
);
27 let s_1
= foo
::make_secrets(3, format
!("ess one"));
28 let s_2
= foo
::S { b: format!("ess two"), ..s_1 }
; // FRU ...
29 //~^ ERROR field `secret_uid` of struct `S` is private
30 println
!("main forged an S named: {}", s_2
.b
);
31 // at end of scope, ... both s_1 *and* s_2 get dropped. Boom!