Skip to content

Commit 777796b

Browse files
committed
Document and extend Ivar
Documents `Ivar` and adds some common trait implementations for it
1 parent b2ce429 commit 777796b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/librustc/util/ivar.rs

+29
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,19 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::fmt;
1112
use std::cell::Cell;
1213

14+
/// A write-once variable. When constructed, it is empty, and
15+
/// can only be set once.
16+
///
17+
/// Ivars ensure that data that can only be initialised once. A full
18+
/// implementation is used for concurrency and blocks on a read of an
19+
/// unfulfilled value. This implementation is more minimal and panics
20+
/// if you attempt to read the value before it has been set. It is also
21+
/// not `Sync`, but may be extended in the future to be usable as a true
22+
/// concurrency type.
23+
#[derive(PartialEq)]
1324
pub struct Ivar<T:Copy> {
1425
data: Cell<Option<T>>
1526
}
@@ -39,3 +50,21 @@ impl<T:Copy> Ivar<T> {
3950
self.get().unwrap()
4051
}
4152
}
53+
54+
impl<T:fmt::Debug> fmt::Debug for Ivar<T> {
55+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56+
match self.get() {
57+
Some(val) => write!(f, "Ivar {{ {:?} }}", val),
58+
None => f.write_str("Ivar { <unfulfilled> }")
59+
}
60+
}
61+
}
62+
63+
impl<T:Copy> Clone for Ivar<T> {
64+
fn clone(&self) -> Ivar<T> {
65+
match self.get() {
66+
Some(val) => Ivar { data: Cell::new(Some(val)) },
67+
None => Ivar::new()
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)