Skip to content

Commit

Permalink
Merge pull request #7 from mmstick/extra-derives
Browse files Browse the repository at this point in the history
Add PartialEq, From<T> derives + Fix rustc 1.24.1 builds
  • Loading branch information
matklad authored Jan 14, 2019
2 parents 084ffbd + 4044453 commit b4e99bc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 15 deletions.
14 changes: 14 additions & 0 deletions src/imp_pl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ impl<T> Default for OnceCell<T> {
}
}

impl<T> From<T> for OnceCell<T> {
fn from(value: T) -> Self {
let cell = Self::new();
cell.get_or_init(|| value);
cell
}
}

impl<T: PartialEq> PartialEq for OnceCell<T> {
fn eq(&self, other: &OnceCell<T>) -> bool {
self.get() == other.get()
}
}

impl<T> OnceCell<T> {
/// An empty cell, for initialization in a `const` context.
pub const INIT: OnceCell<T> = OnceCell {
Expand Down
28 changes: 19 additions & 9 deletions src/imp_std.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use std::{
cell::UnsafeCell,
sync::{
Once, ONCE_INIT,
atomic::{AtomicBool, Ordering},
},
};
use std::cell::UnsafeCell;
use std::sync::{Once, ONCE_INIT};
use std::sync::atomic::{AtomicBool, Ordering};

/// A thread-safe cell which can be written to only once.
///
Expand Down Expand Up @@ -37,8 +33,22 @@ pub struct OnceCell<T> {
}

impl<T> Default for OnceCell<T> {
fn default() -> OnceCell<T> {
OnceCell::new()
fn default() -> Self {
Self::new()
}
}

impl<T> From<T> for OnceCell<T> {
fn from(value: T) -> Self {
let cell = Self::new();
cell.get_or_init(|| value);
cell
}
}

impl<T: PartialEq> PartialEq for OnceCell<T> {
fn eq(&self, other: &OnceCell<T>) -> bool {
self.get() == other.get()
}
}

Expand Down
22 changes: 16 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,8 @@ mod imp;

#[macro_use]
pub mod unsync {
use std::{
ops::Deref,
cell::UnsafeCell,
};
use std::ops::Deref;
use std::cell::UnsafeCell;

/// A cell which can be written to only once. Not thread safe.
///
Expand All @@ -205,8 +203,8 @@ pub mod unsync {
}

impl<T> Default for OnceCell<T> {
fn default() -> OnceCell<T> {
OnceCell::new()
fn default() -> Self {
Self::new()
}
}

Expand All @@ -223,6 +221,18 @@ pub mod unsync {
}
}

impl<T: PartialEq> PartialEq for OnceCell<T> {
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
}
}

impl<T> From<T> for OnceCell<T> {
fn from(value: T) -> Self {
OnceCell { inner: UnsafeCell::new(Some(value)) }
}
}

impl<T> OnceCell<T> {
/// An empty cell, for initialization in a `const` context.
pub const INIT: OnceCell<T> = OnceCell { inner: UnsafeCell::new(None) };
Expand Down
21 changes: 21 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,24 @@ fn unsync_clone() {
let c = s.clone();
assert_eq!(c.get().map(String::as_str), Some("hello"));
}

#[test]
fn from_impl() {
assert_eq!(sync::OnceCell::from("value").get(), Some(&"value"));
assert_eq!(unsync::OnceCell::from("value").get(), Some(&"value"));
assert_ne!(sync::OnceCell::from("foo").get(), Some(&"bar"));
assert_ne!(unsync::OnceCell::from("foo").get(), Some(&"bar"));
}

#[test]
fn partialeq_impl() {
assert!(sync::OnceCell::from("value") == sync::OnceCell::from("value"));
assert!(sync::OnceCell::from("foo") != sync::OnceCell::from("bar"));
assert!(unsync::OnceCell::from("value") == unsync::OnceCell::from("value"));
assert!(unsync::OnceCell::from("foo") != unsync::OnceCell::from("bar"));

assert!(sync::OnceCell::<String>::new() == sync::OnceCell::new());
assert!(sync::OnceCell::<String>::new() != sync::OnceCell::from("value".to_owned()));
assert!(unsync::OnceCell::<String>::new() == unsync::OnceCell::new());
assert!(unsync::OnceCell::<String>::new() != unsync::OnceCell::from("value".to_owned()));
}

0 comments on commit b4e99bc

Please sign in to comment.