1use core::borrow::{Borrow, BorrowMut};
4
5#[cfg(any(
6 feature = "portable-atomic",
7 target_has_atomic = "ptr",
8 has_atomic_load_store
9))]
10use crate::spsc;
11
12#[cfg(any(
13 feature = "portable-atomic",
14 all(feature = "mpmc_large", target_has_atomic = "ptr"),
15 all(not(feature = "mpmc_large"), target_has_atomic = "8")
16))]
17use crate::mpmc;
18
19pub(crate) trait SealedStorage {
20 type Buffer<T>: ?Sized + Borrow<[T]> + BorrowMut<[T]>;
21 #[allow(unused)]
23 fn len<T>(this: *const Self::Buffer<T>) -> usize;
24 #[allow(unused)]
26 fn as_ptr<T>(this: *mut Self::Buffer<T>) -> *mut T;
27
28 #[cfg(any(
29 feature = "portable-atomic",
30 all(feature = "mpmc_large", target_has_atomic = "ptr"),
31 all(not(feature = "mpmc_large"), target_has_atomic = "8")
32 ))]
33 fn as_mpmc_view<T>(this: &mpmc::QueueInner<T, Self>) -> &mpmc::QueueView<T>
34 where
35 Self: Storage + Sized;
36 #[cfg(any(
37 feature = "portable-atomic",
38 all(feature = "mpmc_large", target_has_atomic = "ptr"),
39 all(not(feature = "mpmc_large"), target_has_atomic = "8")
40 ))]
41 fn as_mpmc_mut_view<T>(this: &mut mpmc::QueueInner<T, Self>) -> &mut mpmc::QueueView<T>
42 where
43 Self: Storage + Sized;
44
45 #[cfg(any(
46 feature = "portable-atomic",
47 target_has_atomic = "ptr",
48 has_atomic_load_store
49 ))]
50 fn as_queue_view<T>(this: &spsc::QueueInner<T, Self>) -> &spsc::QueueView<T>
52 where
53 Self: Storage + Sized;
54 #[cfg(any(
55 feature = "portable-atomic",
56 target_has_atomic = "ptr",
57 has_atomic_load_store
58 ))]
59 fn as_mut_queue_view<T>(this: &mut spsc::QueueInner<T, Self>) -> &mut spsc::QueueView<T>
61 where
62 Self: Storage + Sized;
63}
64
65#[allow(private_bounds)]
85pub trait Storage: SealedStorage {}
86
87pub enum OwnedStorage<const N: usize> {}
89impl<const N: usize> Storage for OwnedStorage<N> {}
90impl<const N: usize> SealedStorage for OwnedStorage<N> {
91 type Buffer<T> = [T; N];
92 fn len<T>(_: *const Self::Buffer<T>) -> usize {
93 N
94 }
95 fn as_ptr<T>(this: *mut Self::Buffer<T>) -> *mut T {
96 this.cast()
97 }
98 #[cfg(any(
99 feature = "portable-atomic",
100 all(feature = "mpmc_large", target_has_atomic = "ptr"),
101 all(not(feature = "mpmc_large"), target_has_atomic = "8")
102 ))]
103 fn as_mpmc_view<T>(this: &mpmc::Queue<T, N>) -> &mpmc::QueueView<T>
104 where
105 Self: Storage + Sized,
106 {
107 this.as_view_private()
109 }
110 #[cfg(any(
111 feature = "portable-atomic",
112 all(feature = "mpmc_large", target_has_atomic = "ptr"),
113 all(not(feature = "mpmc_large"), target_has_atomic = "8")
114 ))]
115 fn as_mpmc_mut_view<T>(this: &mut mpmc::Queue<T, N>) -> &mut mpmc::QueueView<T>
116 where
117 Self: Storage + Sized,
118 {
119 this.as_view_mut_private()
121 }
122 #[cfg(any(
123 feature = "portable-atomic",
124 target_has_atomic = "ptr",
125 has_atomic_load_store
126 ))]
127 fn as_queue_view<T>(this: &spsc::QueueInner<T, Self>) -> &spsc::QueueView<T>
129 where
130 Self: Storage + Sized,
131 {
132 this.as_view_private()
134 }
135 #[cfg(any(
136 feature = "portable-atomic",
137 target_has_atomic = "ptr",
138 has_atomic_load_store
139 ))]
140 fn as_mut_queue_view<T>(this: &mut spsc::QueueInner<T, Self>) -> &mut spsc::QueueView<T>
142 where
143 Self: Storage + Sized,
144 {
145 this.as_mut_view_private()
147 }
148}
149
150pub enum ViewStorage {}
152impl Storage for ViewStorage {}
153impl SealedStorage for ViewStorage {
154 type Buffer<T> = [T];
155 fn len<T>(this: *const Self::Buffer<T>) -> usize {
156 this.len()
157 }
158
159 fn as_ptr<T>(this: *mut Self::Buffer<T>) -> *mut T {
160 this.cast()
161 }
162
163 #[cfg(any(
164 feature = "portable-atomic",
165 all(feature = "mpmc_large", target_has_atomic = "ptr"),
166 all(not(feature = "mpmc_large"), target_has_atomic = "8")
167 ))]
168 fn as_mpmc_view<T>(this: &mpmc::QueueInner<T, Self>) -> &mpmc::QueueView<T>
169 where
170 Self: Storage + Sized,
171 {
172 this
173 }
174
175 #[cfg(any(
176 feature = "portable-atomic",
177 all(feature = "mpmc_large", target_has_atomic = "ptr"),
178 all(not(feature = "mpmc_large"), target_has_atomic = "8")
179 ))]
180 fn as_mpmc_mut_view<T>(this: &mut mpmc::QueueInner<T, Self>) -> &mut mpmc::QueueView<T>
181 where
182 Self: Storage + Sized,
183 {
184 this
185 }
186
187 #[cfg(any(
188 feature = "portable-atomic",
189 target_has_atomic = "ptr",
190 has_atomic_load_store
191 ))]
192 fn as_queue_view<T>(this: &spsc::QueueInner<T, Self>) -> &spsc::QueueView<T>
194 where
195 Self: Storage + Sized,
196 {
197 this
198 }
199 #[cfg(any(
200 feature = "portable-atomic",
201 target_has_atomic = "ptr",
202 has_atomic_load_store
203 ))]
204 fn as_mut_queue_view<T>(this: &mut spsc::QueueInner<T, Self>) -> &mut spsc::QueueView<T>
206 where
207 Self: Storage + Sized,
208 {
209 this
210 }
211}