@@ -36,9 +36,11 @@ use std::panic::{RefUnwindSafe, UnwindSafe};
3636use std:: sync:: atomic:: { self , AtomicUsize , Ordering } ;
3737
3838use crate :: bounded:: Bounded ;
39+ use crate :: single:: Single ;
3940use crate :: unbounded:: Unbounded ;
4041
4142mod bounded;
43+ mod single;
4244mod unbounded;
4345
4446/// A concurrent queue.
@@ -67,8 +69,9 @@ impl<T> UnwindSafe for ConcurrentQueue<T> {}
6769impl < T > RefUnwindSafe for ConcurrentQueue < T > { }
6870
6971enum Inner < T > {
70- Bounded ( Bounded < T > ) ,
71- Unbounded ( Unbounded < T > ) ,
72+ Single ( Single < T > ) ,
73+ Bounded ( Box < Bounded < T > > ) ,
74+ Unbounded ( Box < Unbounded < T > > ) ,
7275}
7376
7477impl < T > ConcurrentQueue < T > {
@@ -88,7 +91,11 @@ impl<T> ConcurrentQueue<T> {
8891 /// let q = ConcurrentQueue::<i32>::bounded(100);
8992 /// ```
9093 pub fn bounded ( cap : usize ) -> ConcurrentQueue < T > {
91- ConcurrentQueue ( Inner :: Bounded ( Bounded :: new ( cap) ) )
94+ if cap == 1 {
95+ ConcurrentQueue ( Inner :: Single ( Single :: new ( ) ) )
96+ } else {
97+ ConcurrentQueue ( Inner :: Bounded ( Box :: new ( Bounded :: new ( cap) ) ) )
98+ }
9299 }
93100
94101 /// Creates a new unbounded queue.
@@ -101,7 +108,7 @@ impl<T> ConcurrentQueue<T> {
101108 /// let q = ConcurrentQueue::<i32>::unbounded();
102109 /// ```
103110 pub fn unbounded ( ) -> ConcurrentQueue < T > {
104- ConcurrentQueue ( Inner :: Unbounded ( Unbounded :: new ( ) ) )
111+ ConcurrentQueue ( Inner :: Unbounded ( Box :: new ( Unbounded :: new ( ) ) ) )
105112 }
106113
107114 /// Attempts to push an item into the queue.
@@ -135,6 +142,7 @@ impl<T> ConcurrentQueue<T> {
135142 /// ```
136143 pub fn push ( & self , value : T ) -> Result < ( ) , PushError < T > > {
137144 match & self . 0 {
145+ Inner :: Single ( q) => q. push ( value) ,
138146 Inner :: Bounded ( q) => q. push ( value) ,
139147 Inner :: Unbounded ( q) => q. push ( value) ,
140148 }
@@ -167,6 +175,7 @@ impl<T> ConcurrentQueue<T> {
167175 /// ```
168176 pub fn pop ( & self ) -> Result < T , PopError > {
169177 match & self . 0 {
178+ Inner :: Single ( q) => q. pop ( ) ,
170179 Inner :: Bounded ( q) => q. pop ( ) ,
171180 Inner :: Unbounded ( q) => q. pop ( ) ,
172181 }
@@ -187,6 +196,7 @@ impl<T> ConcurrentQueue<T> {
187196 /// ```
188197 pub fn is_empty ( & self ) -> bool {
189198 match & self . 0 {
199+ Inner :: Single ( q) => q. is_empty ( ) ,
190200 Inner :: Bounded ( q) => q. is_empty ( ) ,
191201 Inner :: Unbounded ( q) => q. is_empty ( ) ,
192202 }
@@ -209,6 +219,7 @@ impl<T> ConcurrentQueue<T> {
209219 /// ```
210220 pub fn is_full ( & self ) -> bool {
211221 match & self . 0 {
222+ Inner :: Single ( q) => q. is_full ( ) ,
212223 Inner :: Bounded ( q) => q. is_full ( ) ,
213224 Inner :: Unbounded ( q) => q. is_full ( ) ,
214225 }
@@ -232,6 +243,7 @@ impl<T> ConcurrentQueue<T> {
232243 /// ```
233244 pub fn len ( & self ) -> usize {
234245 match & self . 0 {
246+ Inner :: Single ( q) => q. len ( ) ,
235247 Inner :: Bounded ( q) => q. len ( ) ,
236248 Inner :: Unbounded ( q) => q. len ( ) ,
237249 }
@@ -254,6 +266,7 @@ impl<T> ConcurrentQueue<T> {
254266 /// ```
255267 pub fn capacity ( & self ) -> Option < usize > {
256268 match & self . 0 {
269+ Inner :: Single ( _) => Some ( 1 ) ,
257270 Inner :: Bounded ( q) => Some ( q. capacity ( ) ) ,
258271 Inner :: Unbounded ( _) => None ,
259272 }
@@ -288,6 +301,7 @@ impl<T> ConcurrentQueue<T> {
288301 /// ```
289302 pub fn close ( & self ) -> bool {
290303 match & self . 0 {
304+ Inner :: Single ( q) => q. close ( ) ,
291305 Inner :: Bounded ( q) => q. close ( ) ,
292306 Inner :: Unbounded ( q) => q. close ( ) ,
293307 }
@@ -308,6 +322,7 @@ impl<T> ConcurrentQueue<T> {
308322 /// ```
309323 pub fn is_closed ( & self ) -> bool {
310324 match & self . 0 {
325+ Inner :: Single ( q) => q. is_closed ( ) ,
311326 Inner :: Bounded ( q) => q. is_closed ( ) ,
312327 Inner :: Unbounded ( q) => q. is_closed ( ) ,
313328 }
0 commit comments