diff --git a/src/spsc.rs b/src/spsc.rs index 4016378..f915fbe 100644 --- a/src/spsc.rs +++ b/src/spsc.rs @@ -167,9 +167,6 @@ pub fn new(cap: usize) -> (Producer, Consumer) { /// /// assert_eq!(p.push(10), Ok(())); /// assert_eq!(p.push(20), Err(PushError(20))); -/// -/// assert!(!p.is_empty()); -/// assert!(p.is_full()); /// ``` pub struct Producer { /// The inner representation of the queue. @@ -246,63 +243,6 @@ impl Producer { pub fn capacity(&self) -> usize { self.inner.cap } - - /// Returns `true` if the queue is empty. - /// - /// # Examples - /// - /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new(100); - /// - /// assert!(p.is_empty()); - /// p.push(1).unwrap(); - /// assert!(!p.is_empty()); - /// ``` - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Returns `true` if the queue is full. - /// - /// # Examples - /// - /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new(1); - /// - /// assert!(!p.is_full()); - /// p.push(1).unwrap(); - /// assert!(p.is_full()); - /// ``` - pub fn is_full(&self) -> bool { - self.len() == self.inner.cap - } - - /// Returns the number of elements in the queue. - /// - /// # Examples - /// - /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new(100); - /// assert_eq!(p.len(), 0); - /// - /// p.push(10).unwrap(); - /// assert_eq!(p.len(), 1); - /// - /// p.push(20).unwrap(); - /// assert_eq!(p.len(), 2); - /// ``` - pub fn len(&self) -> usize { - let head = self.inner.head.load(Ordering::Acquire); - let tail = self.tail.get(); - self.head.set(head); - self.inner.distance(head, tail) - } } impl fmt::Debug for Producer { @@ -323,9 +263,6 @@ impl fmt::Debug for Producer { /// /// assert_eq!(c.pop(), Ok(10)); /// assert_eq!(c.pop(), Err(PopError)); -/// -/// assert!(c.is_empty()); -/// assert!(!c.is_full()); /// ``` pub struct Consumer { /// The inner representation of the queue. @@ -401,63 +338,6 @@ impl Consumer { pub fn capacity(&self) -> usize { self.inner.cap } - - /// Returns `true` if the queue is empty. - /// - /// # Examples - /// - /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new(100); - /// - /// assert!(c.is_empty()); - /// p.push(1).unwrap(); - /// assert!(!c.is_empty()); - /// ``` - pub fn is_empty(&self) -> bool { - self.len() == 0 - } - - /// Returns `true` if the queue is full. - /// - /// # Examples - /// - /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new(1); - /// - /// assert!(!c.is_full()); - /// p.push(1).unwrap(); - /// assert!(c.is_full()); - /// ``` - pub fn is_full(&self) -> bool { - self.len() == self.inner.cap - } - - /// Returns the number of elements in the queue. - /// - /// # Examples - /// - /// ``` - /// use crossbeam_queue::spsc; - /// - /// let (p, c) = spsc::new(100); - /// assert_eq!(c.len(), 0); - /// - /// p.push(10).unwrap(); - /// assert_eq!(c.len(), 1); - /// - /// p.push(20).unwrap(); - /// assert_eq!(c.len(), 2); - /// ``` - pub fn len(&self) -> usize { - let head = self.head.get(); - let tail = self.inner.tail.load(Ordering::Acquire); - self.tail.set(tail); - self.inner.distance(head, tail) - } } impl fmt::Debug for Consumer { diff --git a/tests/spsc.rs b/tests/spsc.rs index 4d193ab..727f51e 100644 --- a/tests/spsc.rs +++ b/tests/spsc.rs @@ -35,107 +35,6 @@ fn zero_capacity() { let _ = spsc::new::(0); } -#[test] -fn len_empty_full() { - let (p, c) = spsc::new(2); - - assert_eq!(p.len(), 0); - assert_eq!(c.len(), 0); - assert_eq!(p.is_empty(), true); - assert_eq!(c.is_empty(), true); - assert_eq!(p.is_full(), false); - assert_eq!(c.is_full(), false); - - p.push(()).unwrap(); - - assert_eq!(p.len(), 1); - assert_eq!(c.len(), 1); - assert_eq!(p.is_empty(), false); - assert_eq!(c.is_empty(), false); - assert_eq!(p.is_full(), false); - assert_eq!(c.is_full(), false); - - p.push(()).unwrap(); - - assert_eq!(p.len(), 2); - assert_eq!(c.len(), 2); - assert_eq!(p.is_empty(), false); - assert_eq!(c.is_empty(), false); - assert_eq!(p.is_full(), true); - assert_eq!(c.is_full(), true); - - c.pop().unwrap(); - - assert_eq!(p.len(), 1); - assert_eq!(c.len(), 1); - assert_eq!(p.is_empty(), false); - assert_eq!(c.is_empty(), false); - assert_eq!(p.is_full(), false); - assert_eq!(c.is_full(), false); -} - -#[test] -fn len() { - const COUNT: usize = 25_000; - const CAP: usize = 1000; - - let (p, c) = spsc::new(CAP); - assert_eq!(p.len(), 0); - assert_eq!(c.len(), 0); - - for _ in 0..CAP / 10 { - for i in 0..50 { - p.push(i).unwrap(); - assert_eq!(p.len(), i + 1); - } - - for i in 0..50 { - c.pop().unwrap(); - assert_eq!(c.len(), 50 - i - 1); - } - } - assert_eq!(p.len(), 0); - assert_eq!(c.len(), 0); - - for i in 0..CAP { - p.push(i).unwrap(); - assert_eq!(p.len(), i + 1); - } - - for _ in 0..CAP { - c.pop().unwrap(); - } - assert_eq!(c.len(), 0); - - let p = scope(|s| { - s.spawn(move |_| { - for i in 0..COUNT { - loop { - if let Ok(x) = c.pop() { - assert_eq!(x, i); - break; - } - } - let len = c.len(); - assert!(len <= CAP); - } - }); - - s.spawn(move |_| { - for i in 0..COUNT { - while p.push(i).is_err() {} - let len = p.len(); - assert!(len <= CAP); - } - p - }) - .join() - .unwrap() - }) - .unwrap(); - assert_eq!(p.len(), 0); -} - #[test] fn parallel() { const COUNT: usize = 100_000;