Skip to content

Commit 512bec5

Browse files
committed
Bound the contained type with sync.
Lazy<Cell<T>> would be a bad idea.
1 parent 63b6be8 commit 512bec5

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Lazy initialization."
55
keywords = ["lazy", "initialization"]
66
license = "Apache-2.0/MIT"
77
repository = "https://github.com/khuey/lazy-init"
8-
version = "0.1.0"
8+
version = "0.1.1"
99

1010
[dev-dependencies]
1111
scoped-pool = "1.0.0"

src/lib.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,20 @@ enum ThisOrThat<T, U> {
2222

2323
/// `LazyTransform<T, U>` is a synchronized holder type, that holds a value of
2424
/// type T until it is lazily converted into a value of type U.
25-
pub struct LazyTransform<T, U> {
25+
pub struct LazyTransform<T, U>
26+
where T: Sync,
27+
U: Sync
28+
{
2629
initialized: AtomicBool,
2730
lock: Mutex<()>,
2831
value: UnsafeCell<Option<ThisOrThat<T, U>>>,
2932
}
3033

3134
// Implementation details.
32-
impl<T, U> LazyTransform<T, U> {
35+
impl<T, U> LazyTransform<T, U>
36+
where T: Sync,
37+
U: Sync
38+
{
3339
fn extract<'a>(&'a self) -> Option<&'a U> {
3440
// Make sure we're initialized first!
3541
match unsafe { (*self.value.get()).as_ref() } {
@@ -41,7 +47,10 @@ impl<T, U> LazyTransform<T, U> {
4147
}
4248

4349
// Public API.
44-
impl<T, U> LazyTransform<T, U> {
50+
impl<T, U> LazyTransform<T, U>
51+
where T: Sync,
52+
U: Sync
53+
{
4554
/// Construct a new, untransformed `LazyTransform<T, U>` with an argument of
4655
/// type T.
4756
pub fn new(t: T) -> LazyTransform<T, U> {
@@ -103,15 +112,23 @@ impl<T, U> LazyTransform<T, U> {
103112
}
104113
}
105114

106-
unsafe impl<T, U> Sync for LazyTransform<T, U> {}
115+
unsafe impl<T, U> Sync for LazyTransform<T, U>
116+
where T: Sync,
117+
U: Sync
118+
{
119+
}
107120

108121
/// `Lazy<T>` is a lazily initialized synchronized holder type. You can think
109122
/// of it as a LazyTransform where the initial type doesn't exist.
110-
pub struct Lazy<T> {
123+
pub struct Lazy<T>
124+
where T: Sync
125+
{
111126
inner: LazyTransform<(), T>,
112127
}
113128

114-
impl<T> Lazy<T> {
129+
impl<T> Lazy<T>
130+
where T: Sync
131+
{
115132
/// Construct a new, uninitialized `Lazy<T>`.
116133
pub fn new() -> Lazy<T> {
117134
Lazy { inner: LazyTransform::new(()) }

0 commit comments

Comments
 (0)