1#![allow(unknown_lints)] #![allow(renamed_and_removed_lints)] #![allow(forgetting_copy_types)]
9#![allow(clippy::forget_copy)]
10#![allow(clippy::forget_non_drop)]
11
12use crate::flexzerovec::FlexZeroVec;
13use crate::map::ZeroMapBorrowed;
14use crate::map::ZeroMapKV;
15use crate::map2d::ZeroMap2dBorrowed;
16use crate::ule::*;
17use crate::{VarZeroVec, ZeroMap, ZeroMap2d, ZeroVec};
18use core::{mem, ptr};
19use yoke::*;
20
21unsafe impl<'a, T: 'static + AsULE + ?Sized> Yokeable<'a> for ZeroVec<'static, T> {
24 type Output = ZeroVec<'a, T>;
25 #[inline]
26 fn transform(&'a self) -> &'a Self::Output {
27 self
28 }
29 #[inline]
30 fn transform_owned(self) -> Self::Output {
31 self
32 }
33 #[inline]
34 unsafe fn make(from: Self::Output) -> Self {
35 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
36 let from = mem::ManuallyDrop::new(from);
37 let ptr: *const Self = (&*from as *const Self::Output).cast();
38 ptr::read(ptr)
39 }
40 #[inline]
41 fn transform_mut<F>(&'a mut self, f: F)
42 where
43 F: 'static + for<'b> FnOnce(&'b mut Self::Output),
44 {
45 unsafe { f(mem::transmute::<&mut Self, &mut Self::Output>(self)) }
46 }
47}
48
49unsafe impl<'a, T: 'static + VarULE + ?Sized> Yokeable<'a> for VarZeroVec<'static, T> {
52 type Output = VarZeroVec<'a, T>;
53 #[inline]
54 fn transform(&'a self) -> &'a Self::Output {
55 self
56 }
57 #[inline]
58 fn transform_owned(self) -> Self::Output {
59 self
60 }
61 #[inline]
62 unsafe fn make(from: Self::Output) -> Self {
63 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
64 let from = mem::ManuallyDrop::new(from);
65 let ptr: *const Self = (&*from as *const Self::Output).cast();
66 ptr::read(ptr)
67 }
68 #[inline]
69 fn transform_mut<F>(&'a mut self, f: F)
70 where
71 F: 'static + for<'b> FnOnce(&'b mut Self::Output),
72 {
73 unsafe { f(mem::transmute::<&mut Self, &mut Self::Output>(self)) }
74 }
75}
76
77unsafe impl<'a> Yokeable<'a> for FlexZeroVec<'static> {
80 type Output = FlexZeroVec<'a>;
81 #[inline]
82 fn transform(&'a self) -> &'a Self::Output {
83 self
84 }
85 #[inline]
86 fn transform_owned(self) -> Self::Output {
87 self
88 }
89 #[inline]
90 unsafe fn make(from: Self::Output) -> Self {
91 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
92 let from = mem::ManuallyDrop::new(from);
93 let ptr: *const Self = (&*from as *const Self::Output).cast();
94 ptr::read(ptr)
95 }
96 #[inline]
97 fn transform_mut<F>(&'a mut self, f: F)
98 where
99 F: 'static + for<'b> FnOnce(&'b mut Self::Output),
100 {
101 unsafe { f(mem::transmute::<&mut Self, &mut Self::Output>(self)) }
102 }
103}
104
105#[allow(clippy::transmute_ptr_to_ptr)]
107unsafe impl<'a, K, V> Yokeable<'a> for ZeroMap<'static, K, V>
108where
109 K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
110 V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
111 <K as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
112 <V as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
113{
114 type Output = ZeroMap<'a, K, V>;
115 #[inline]
116 fn transform(&'a self) -> &'a Self::Output {
117 unsafe {
118 mem::transmute::<&Self, &Self::Output>(self)
122 }
123 }
124 #[inline]
125 fn transform_owned(self) -> Self::Output {
126 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
127 unsafe {
128 let this = mem::ManuallyDrop::new(self);
131 let ptr: *const Self::Output = (&*this as *const Self).cast();
132 ptr::read(ptr)
133 }
134 }
135 #[inline]
136 unsafe fn make(from: Self::Output) -> Self {
137 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
138 let from = mem::ManuallyDrop::new(from);
139 let ptr: *const Self = (&*from as *const Self::Output).cast();
140 ptr::read(ptr)
141 }
142 #[inline]
143 fn transform_mut<F>(&'a mut self, f: F)
144 where
145 F: 'static + for<'b> FnOnce(&'b mut Self::Output),
146 {
147 unsafe { f(mem::transmute::<&mut Self, &mut Self::Output>(self)) }
148 }
149}
150
151#[allow(clippy::transmute_ptr_to_ptr)]
153unsafe impl<'a, K, V> Yokeable<'a> for ZeroMapBorrowed<'static, K, V>
154where
155 K: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
156 V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
157 &'static <K as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
158 &'static <V as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
159{
160 type Output = ZeroMapBorrowed<'a, K, V>;
161 #[inline]
162 fn transform(&'a self) -> &'a Self::Output {
163 unsafe {
164 mem::transmute::<&Self, &Self::Output>(self)
168 }
169 }
170 #[inline]
171 fn transform_owned(self) -> Self::Output {
172 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
173 unsafe {
174 let this = mem::ManuallyDrop::new(self);
177 let ptr: *const Self::Output = (&*this as *const Self).cast();
178 ptr::read(ptr)
179 }
180 }
181 #[inline]
182 unsafe fn make(from: Self::Output) -> Self {
183 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
184 let from = mem::ManuallyDrop::new(from);
185 let ptr: *const Self = (&*from as *const Self::Output).cast();
186 ptr::read(ptr)
187 }
188 #[inline]
189 fn transform_mut<F>(&'a mut self, f: F)
190 where
191 F: 'static + for<'b> FnOnce(&'b mut Self::Output),
192 {
193 unsafe { f(mem::transmute::<&mut Self, &mut Self::Output>(self)) }
194 }
195}
196
197#[allow(clippy::transmute_ptr_to_ptr)]
199unsafe impl<'a, K0, K1, V> Yokeable<'a> for ZeroMap2d<'static, K0, K1, V>
200where
201 K0: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
202 K1: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
203 V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
204 <K0 as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
205 <K1 as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
206 <V as ZeroMapKV<'static>>::Container: for<'b> Yokeable<'b>,
207{
208 type Output = ZeroMap2d<'a, K0, K1, V>;
209 #[inline]
210 fn transform(&'a self) -> &'a Self::Output {
211 unsafe {
212 mem::transmute::<&Self, &Self::Output>(self)
216 }
217 }
218 #[inline]
219 fn transform_owned(self) -> Self::Output {
220 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
221 unsafe {
222 let this = mem::ManuallyDrop::new(self);
225 let ptr: *const Self::Output = (&*this as *const Self).cast();
226 ptr::read(ptr)
227 }
228 }
229 #[inline]
230 unsafe fn make(from: Self::Output) -> Self {
231 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
232 let from = mem::ManuallyDrop::new(from);
233 let ptr: *const Self = (&*from as *const Self::Output).cast();
234 ptr::read(ptr)
235 }
236 #[inline]
237 fn transform_mut<F>(&'a mut self, f: F)
238 where
239 F: 'static + for<'b> FnOnce(&'b mut Self::Output),
240 {
241 unsafe { f(mem::transmute::<&mut Self, &mut Self::Output>(self)) }
242 }
243}
244
245#[allow(clippy::transmute_ptr_to_ptr)]
247unsafe impl<'a, K0, K1, V> Yokeable<'a> for ZeroMap2dBorrowed<'static, K0, K1, V>
248where
249 K0: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
250 K1: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
251 V: 'static + for<'b> ZeroMapKV<'b> + ?Sized,
252 &'static <K0 as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
253 &'static <K1 as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
254 &'static <V as ZeroMapKV<'static>>::Slice: for<'b> Yokeable<'b>,
255{
256 type Output = ZeroMap2dBorrowed<'a, K0, K1, V>;
257 #[inline]
258 fn transform(&'a self) -> &'a Self::Output {
259 unsafe {
260 mem::transmute::<&Self, &Self::Output>(self)
264 }
265 }
266 #[inline]
267 fn transform_owned(self) -> Self::Output {
268 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
269 unsafe {
270 let this = mem::ManuallyDrop::new(self);
273 let ptr: *const Self::Output = (&*this as *const Self).cast();
274 ptr::read(ptr)
275 }
276 }
277 #[inline]
278 unsafe fn make(from: Self::Output) -> Self {
279 debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>());
280 let from = mem::ManuallyDrop::new(from);
281 let ptr: *const Self = (&*from as *const Self::Output).cast();
282 ptr::read(ptr)
283 }
284 #[inline]
285 fn transform_mut<F>(&'a mut self, f: F)
286 where
287 F: 'static + for<'b> FnOnce(&'b mut Self::Output),
288 {
289 unsafe { f(mem::transmute::<&mut Self, &mut Self::Output>(self)) }
290 }
291}
292
293#[cfg(test)]
294#[allow(non_camel_case_types, non_snake_case)]
295mod test {
296 use super::*;
297 use crate::{vecs::FlexZeroSlice, VarZeroSlice, ZeroSlice};
298 use databake::*;
299
300 #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
304 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
305 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
306 struct DeriveTest_ZeroVec<'data> {
307 #[cfg_attr(feature = "serde", serde(borrow))]
308 _data: ZeroVec<'data, u16>,
309 }
310
311 #[test]
312 #[ignore] fn bake_ZeroVec() {
314 test_bake!(
315 DeriveTest_ZeroVec<'static>,
316 crate::yoke_impls::test::DeriveTest_ZeroVec {
317 _data: crate::ZeroVec::new(),
318 },
319 zerovec,
320 );
321 }
322
323 #[derive(yoke::Yokeable)]
324 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
325 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
326 struct DeriveTest_ZeroSlice<'data> {
327 #[cfg_attr(feature = "serde", serde(borrow))]
328 _data: &'data ZeroSlice<u16>,
329 }
330
331 #[test]
332 fn bake_ZeroSlice() {
333 test_bake!(
334 DeriveTest_ZeroSlice<'static>,
335 crate::yoke_impls::test::DeriveTest_ZeroSlice {
336 _data: crate::ZeroSlice::new_empty(),
337 },
338 zerovec,
339 );
340 }
341
342 #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
343 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
344 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
345 struct DeriveTest_FlexZeroVec<'data> {
346 #[cfg_attr(feature = "serde", serde(borrow))]
347 _data: FlexZeroVec<'data>,
348 }
349
350 #[test]
351 fn bake_FlexZeroVec() {
352 test_bake!(
353 DeriveTest_FlexZeroVec<'static>,
354 crate::yoke_impls::test::DeriveTest_FlexZeroVec {
355 _data: crate::vecs::FlexZeroVec::new(),
356 },
357 zerovec,
358 );
359 }
360
361 #[derive(yoke::Yokeable)]
362 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
363 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
364 struct DeriveTest_FlexZeroSlice<'data> {
365 #[cfg_attr(feature = "serde", serde(borrow))]
366 _data: &'data FlexZeroSlice,
367 }
368
369 #[test]
370 fn bake_FlexZeroSlice() {
371 test_bake!(
372 DeriveTest_FlexZeroSlice<'static>,
373 crate::yoke_impls::test::DeriveTest_FlexZeroSlice {
374 _data: unsafe { crate::vecs::FlexZeroSlice::from_byte_slice_unchecked(b"\x01\0") },
375 },
376 zerovec,
377 );
378 }
379
380 #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
381 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
382 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
383 struct DeriveTest_VarZeroVec<'data> {
384 #[cfg_attr(feature = "serde", serde(borrow))]
385 _data: VarZeroVec<'data, str>,
386 }
387
388 #[test]
389 fn bake_VarZeroVec() {
390 test_bake!(
391 DeriveTest_VarZeroVec<'static>,
392 crate::yoke_impls::test::DeriveTest_VarZeroVec {
393 _data: crate::VarZeroVec::new(),
394 },
395 zerovec,
396 );
397 }
398
399 #[derive(yoke::Yokeable)]
400 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
401 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
402 struct DeriveTest_VarZeroSlice<'data> {
403 #[cfg_attr(feature = "serde", serde(borrow))]
404 _data: &'data VarZeroSlice<str>,
405 }
406
407 #[test]
408 fn bake_VarZeroSlice() {
409 test_bake!(
410 DeriveTest_VarZeroSlice<'static>,
411 crate::yoke_impls::test::DeriveTest_VarZeroSlice {
412 _data: crate::VarZeroSlice::new_empty()
413 },
414 zerovec,
415 );
416 }
417
418 #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
419 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
420 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
421 #[yoke(prove_covariance_manually)]
422 struct DeriveTest_ZeroMap<'data> {
423 #[cfg_attr(feature = "serde", serde(borrow))]
424 _data: ZeroMap<'data, [u8], str>,
425 }
426
427 #[test]
428 fn bake_ZeroMap() {
429 test_bake!(
430 DeriveTest_ZeroMap<'static>,
431 crate::yoke_impls::test::DeriveTest_ZeroMap {
432 _data: unsafe {
433 #[allow(unused_unsafe)]
434 crate::ZeroMap::from_parts_unchecked(
435 crate::VarZeroVec::new(),
436 crate::VarZeroVec::new(),
437 )
438 },
439 },
440 zerovec,
441 );
442 }
443
444 #[derive(yoke::Yokeable)]
445 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
446 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
447 #[yoke(prove_covariance_manually)]
448 struct DeriveTest_ZeroMapBorrowed<'data> {
449 #[cfg_attr(feature = "serde", serde(borrow))]
450 _data: ZeroMapBorrowed<'data, [u8], str>,
451 }
452
453 #[test]
454 fn bake_ZeroMapBorrowed() {
455 test_bake!(
456 DeriveTest_ZeroMapBorrowed<'static>,
457 crate::yoke_impls::test::DeriveTest_ZeroMapBorrowed {
458 _data: unsafe {
459 #[allow(unused_unsafe)]
460 crate::maps::ZeroMapBorrowed::from_parts_unchecked(
461 crate::VarZeroSlice::new_empty(),
462 crate::VarZeroSlice::new_empty(),
463 )
464 },
465 },
466 zerovec,
467 );
468 }
469
470 #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
471 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
472 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
473 #[yoke(prove_covariance_manually)]
474 struct DeriveTest_ZeroMapWithULE<'data> {
475 #[cfg_attr(feature = "serde", serde(borrow))]
476 _data: ZeroMap<'data, ZeroSlice<u32>, str>,
477 }
478
479 #[test]
480 fn bake_ZeroMapWithULE() {
481 test_bake!(
482 DeriveTest_ZeroMapWithULE<'static>,
483 crate::yoke_impls::test::DeriveTest_ZeroMapWithULE {
484 _data: unsafe {
485 #[allow(unused_unsafe)]
486 crate::ZeroMap::from_parts_unchecked(
487 crate::VarZeroVec::new(),
488 crate::VarZeroVec::new(),
489 )
490 },
491 },
492 zerovec,
493 );
494 }
495
496 #[derive(yoke::Yokeable, zerofrom::ZeroFrom)]
497 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
498 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
499 #[yoke(prove_covariance_manually)]
500 struct DeriveTest_ZeroMap2d<'data> {
501 #[cfg_attr(feature = "serde", serde(borrow))]
502 _data: ZeroMap2d<'data, u16, u16, str>,
503 }
504
505 #[test]
506 fn bake_ZeroMap2d() {
507 test_bake!(
508 DeriveTest_ZeroMap2d<'static>,
509 crate::yoke_impls::test::DeriveTest_ZeroMap2d {
510 _data: unsafe {
511 #[allow(unused_unsafe)]
512 crate::ZeroMap2d::from_parts_unchecked(
513 crate::ZeroVec::new(),
514 crate::ZeroVec::new(),
515 crate::ZeroVec::new(),
516 crate::VarZeroVec::new(),
517 )
518 },
519 },
520 zerovec,
521 );
522 }
523
524 #[derive(yoke::Yokeable)]
525 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
526 #[cfg_attr(feature = "databake", derive(databake::Bake), databake(path = zerovec::yoke_impls::test))]
527 #[yoke(prove_covariance_manually)]
528 struct DeriveTest_ZeroMap2dBorrowed<'data> {
529 #[cfg_attr(feature = "serde", serde(borrow))]
530 _data: ZeroMap2dBorrowed<'data, u16, u16, str>,
531 }
532
533 #[test]
534 fn bake_ZeroMap2dBorrowed() {
535 test_bake!(
536 DeriveTest_ZeroMap2dBorrowed<'static>,
537 crate::yoke_impls::test::DeriveTest_ZeroMap2dBorrowed {
538 _data: unsafe {
539 #[allow(unused_unsafe)]
540 crate::maps::ZeroMap2dBorrowed::from_parts_unchecked(
541 crate::ZeroSlice::new_empty(),
542 crate::ZeroSlice::new_empty(),
543 crate::ZeroSlice::new_empty(),
544 crate::VarZeroSlice::new_empty(),
545 )
546 },
547 },
548 zerovec,
549 );
550 }
551}