Debian Codegraph @

debian/missing-sources/derive_more/tests/as_mut.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![cfg_attr(nightly, feature(never_type))]
3 #![allow(clippy::unnecessary_mut_passed)] // testing correct signatures rather than actual code
4 #![allow(dead_code)] // some code is tested for type checking only
5
6 #[cfg(not(feature = "std"))]
7 extern crate alloc;
8
9 #[cfg(not(feature = "std"))]
10 use alloc::{borrow::ToOwned, collections::VecDeque, string::String, vec, vec::Vec};
11
12 #[cfg(feature = "std")]
13 use std::collections::VecDeque;
14
15 use core::{marker::PhantomData, ptr};
16
17 use derive_more::AsMut;
18
19 struct Helper(i32, f64, bool);
20
21 impl AsMut<i32> for Helper {
22 fn as_mut(&mut self) -> &mut i32 {
23 &mut self.0
24 }
25 }
26
27 impl AsMut<f64> for Helper {
28 fn as_mut(&mut self) -> &mut f64 {
29 &mut self.1
30 }
31 }
32
33 impl AsMut<bool> for Helper {
34 fn as_mut(&mut self) -> &mut bool {
35 &mut self.2
36 }
37 }
38
39 struct LifetimeHelper<'a>(i32, PhantomData<&'a ()>);
40
41 impl LifetimeHelper<'static> {
42 fn new(val: i32) -> Self {
43 Self(val, PhantomData)
44 }
45 }
46
47 impl AsMut<i32> for LifetimeHelper<'static> {
48 fn as_mut(&mut self) -> &mut i32 {
49 &mut self.0
50 }
51 }
52
53 struct ConstParamHelper<const N: usize>([i32; N]);
54
55 impl AsMut<[i32]> for ConstParamHelper<0> {
56 fn as_mut(&mut self) -> &mut [i32] {
57 self.0.as_mut()
58 }
59 }
60
61 trait Some {
62 type Assoc;
63 }
64
65 impl Some for () {
66 type Assoc = Helper;
67 }
68
69 impl Some for bool {
70 type Assoc = String;
71 }
72
73 impl Some for u8 {
74 type Assoc = i32;
75 }
76
77 impl Some for str {
78 type Assoc = u8;
79 }
80
81 mod single_field {
82 use super::*;
83
84 mod tuple {
85 use super::*;
86
87 #[test]
88 fn nothing() {
89 #[derive(AsMut)]
90 struct Nothing(String);
91
92 let mut item = Nothing("test".to_owned());
93
94 assert!(ptr::eq(item.as_mut(), &mut item.0));
95 }
96
97 #[test]
98 fn forward() {
99 #[derive(AsMut)]
100 #[as_mut(forward)]
101 struct Forward(String);
102
103 let mut item = Forward("test".to_owned());
104
105 let rf: &mut str = item.as_mut();
106 assert!(ptr::eq(rf, item.0.as_mut()));
107 }
108
109 #[test]
110 fn field() {
111 #[derive(AsMut)]
112 struct Field(#[as_mut] String);
113
114 let mut item = Field("test".to_owned());
115
116 assert!(ptr::eq(item.as_mut(), &mut item.0));
117 }
118
119 #[test]
120 fn field_forward() {
121 #[derive(AsMut)]
122 struct FieldForward(#[as_mut(forward)] String);
123
124 let mut item = FieldForward("test".to_owned());
125
126 let rf: &mut str = item.as_mut();
127 assert!(ptr::eq(rf, item.0.as_mut()));
128 }
129
130 #[test]
131 fn types() {
132 #[derive(AsMut)]
133 #[as_mut(i32, f64)]
134 struct Types(Helper);
135
136 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
137 // to the field type, by producing a trait implementations conflict error during
138 // compilation, if it does.
139 impl AsMut<bool> for Types {
140 fn as_mut(&mut self) -> &mut bool {
141 self.0.as_mut()
142 }
143 }
144
145 // Asserts that the macro expansion doesn't generate an `AsMut` impl for the field type,
146 // by producing a trait implementations conflict error during compilation, if it does.
147 impl AsMut<Helper> for Types {
148 fn as_mut(&mut self) -> &mut Helper {
149 &mut self.0
150 }
151 }
152
153 let mut item = Types(Helper(1, 2.0, false));
154
155 let rf: &mut i32 = item.as_mut();
156 assert!(ptr::eq(rf, item.0.as_mut()));
157
158 let rf: &mut f64 = item.as_mut();
159 assert!(ptr::eq(rf, item.0.as_mut()));
160 }
161
162 #[test]
163 fn types_with_inner() {
164 #[derive(AsMut)]
165 #[as_mut(i32, Helper)]
166 struct TypesWithInner(Helper);
167
168 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
169 // to the field type, by producing a trait implementations conflict error during
170 // compilation, if it does.
171 impl AsMut<bool> for TypesWithInner {
172 fn as_mut(&mut self) -> &mut bool {
173 self.0.as_mut()
174 }
175 }
176
177 let mut item = TypesWithInner(Helper(1, 2.0, false));
178
179 let rf: &mut i32 = item.as_mut();
180 assert!(ptr::eq(rf, item.0.as_mut()));
181
182 let rf: &mut Helper = item.as_mut();
183 assert!(ptr::eq(rf, &mut item.0));
184 }
185
186 #[test]
187 fn types_with_renamed_inner() {
188 type RenamedFoo = Helper;
189
190 #[derive(AsMut)]
191 #[as_mut(i32, RenamedFoo)]
192 struct TypesWithRenamedInner(Helper);
193
194 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
195 // to the field type, by producing a trait implementations conflict error during
196 // compilation, if it does.
197 impl AsMut<bool> for TypesWithRenamedInner {
198 fn as_mut(&mut self) -> &mut bool {
199 self.0.as_mut()
200 }
201 }
202
203 let mut item = TypesWithRenamedInner(Helper(1, 2.0, false));
204
205 let rf: &mut i32 = item.as_mut();
206 assert!(ptr::eq(rf, item.0.as_mut()));
207
208 let rf: &mut Helper = item.as_mut();
209 assert!(ptr::eq(rf, &mut item.0));
210 }
211
212 #[test]
213 fn field_types() {
214 #[derive(AsMut)]
215 struct FieldTypes(#[as_mut(i32, f64)] Helper);
216
217 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
218 // to the field type, by producing a trait implementations conflict error during
219 // compilation, if it does.
220 impl AsMut<bool> for FieldTypes {
221 fn as_mut(&mut self) -> &mut bool {
222 self.0.as_mut()
223 }
224 }
225
226 // Asserts that the macro expansion doesn't generate an `AsMut` impl for the field type,
227 // by producing a trait implementations conflict error during compilation, if it does.
228 impl AsMut<Helper> for FieldTypes {
229 fn as_mut(&mut self) -> &mut Helper {
230 &mut self.0
231 }
232 }
233
234 let mut item = FieldTypes(Helper(1, 2.0, false));
235
236 let rf: &mut i32 = item.as_mut();
237 assert!(ptr::eq(rf, item.0.as_mut()));
238
239 let rf: &mut f64 = item.as_mut();
240 assert!(ptr::eq(rf, item.0.as_mut()));
241 }
242
243 #[test]
244 fn field_types_with_inner() {
245 #[derive(AsMut)]
246 struct FieldTypesWithInner(#[as_mut(i32, Helper)] Helper);
247
248 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
249 // to the field type, by producing a trait implementations conflict error during
250 // compilation, if it does.
251 impl AsMut<bool> for FieldTypesWithInner {
252 fn as_mut(&mut self) -> &mut bool {
253 self.0.as_mut()
254 }
255 }
256
257 let mut item = FieldTypesWithInner(Helper(1, 2.0, false));
258
259 let rf: &mut i32 = item.as_mut();
260 assert!(ptr::eq(rf, item.0.as_mut()));
261
262 let rf: &mut Helper = item.as_mut();
263 assert!(ptr::eq(rf, &mut item.0));
264 }
265
266 #[test]
267 fn field_types_with_renamed_inner() {
268 type RenamedFoo = Helper;
269
270 #[derive(AsMut)]
271 struct FieldTypesWithRenamedInner(#[as_mut(i32, RenamedFoo)] Helper);
272
273 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding to
274 // the field type, by producing a trait implementations conflict error during compilation,
275 // if it does.
276 impl AsMut<bool> for FieldTypesWithRenamedInner {
277 fn as_mut(&mut self) -> &mut bool {
278 self.0.as_mut()
279 }
280 }
281
282 let mut item = FieldTypesWithRenamedInner(Helper(1, 2.0, false));
283
284 let rf: &mut i32 = item.as_mut();
285 assert!(ptr::eq(rf, item.0.as_mut()));
286
287 let rf: &mut Helper = item.as_mut();
288 assert!(ptr::eq(rf, &mut item.0));
289 }
290
291 mod generic {
292 use super::*;
293
294 #[test]
295 fn nothing() {
296 #[derive(AsMut)]
297 struct Nothing<T>(T);
298
299 let mut item = Nothing("test".to_owned());
300
301 assert!(ptr::eq(item.as_mut(), &mut item.0));
302 }
303
304 #[test]
305 fn nothing_assoc() {
306 #[derive(AsMut)]
307 struct Nothing<T: Some>(T::Assoc);
308
309 let mut item = Nothing::<bool>("test".to_owned());
310
311 assert!(ptr::eq(item.as_mut(), &mut item.0));
312 }
313
314 #[test]
315 fn forward() {
316 #[derive(AsMut)]
317 #[as_mut(forward)]
318 struct Forward<T>(T);
319
320 let mut item = Forward("test".to_owned());
321
322 let rf: &mut str = item.as_mut();
323 assert!(ptr::eq(rf, item.0.as_mut()));
324 }
325
326 #[test]
327 fn forward_assoc() {
328 #[derive(AsMut)]
329 #[as_mut(forward)]
330 struct Forward<T: Some>(T::Assoc);
331
332 let mut item = Forward::<bool>("test".to_owned());
333
334 let rf: &mut str = item.as_mut();
335 assert!(ptr::eq(rf, item.0.as_mut()));
336 }
337
338 #[test]
339 fn field() {
340 #[derive(AsMut)]
341 struct Field<T>(#[as_mut] T);
342
343 let mut item = Field("test".to_owned());
344
345 assert!(ptr::eq(item.as_mut(), &mut item.0));
346 }
347
348 #[test]
349 fn field_forward() {
350 #[derive(AsMut)]
351 struct FieldForward<T>(#[as_mut(forward)] T);
352
353 let mut item = FieldForward("test".to_owned());
354
355 let rf: &mut str = item.as_mut();
356 assert!(ptr::eq(rf, item.0.as_mut()));
357 }
358
359 #[test]
360 fn field_forward_assoc() {
361 #[derive(AsMut)]
362 struct FieldForward<T: Some>(#[as_mut(forward)] T::Assoc);
363
364 let mut item = FieldForward::<bool>("test".to_owned());
365
366 let rf: &mut str = item.as_mut();
367 assert!(ptr::eq(rf, item.0.as_mut()));
368 }
369
370 #[test]
371 fn types() {
372 #[derive(AsMut)]
373 #[as_mut(i32, f64)]
374 struct Types<T>(T);
375
376 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl
377 // forwarding to the field type, by producing a trait implementations conflict error
378 // during compilation, if it does.
379 impl<T: AsMut<bool>> AsMut<bool> for Types<T> {
380 fn as_mut(&mut self) -> &mut bool {
381 self.0.as_mut()
382 }
383 }
384
385 let mut item = Types(Helper(1, 2.0, false));
386
387 let rf: &mut i32 = item.as_mut();
388 assert!(ptr::eq(rf, item.0.as_mut()));
389
390 let rf: &mut f64 = item.as_mut();
391 assert!(ptr::eq(rf, item.0.as_mut()));
392 }
393
394 #[test]
395 fn types_assoc() {
396 #[derive(AsMut)]
397 #[as_mut(i32, f64)]
398 struct Types<T: Some>(T::Assoc);
399
400 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl
401 // forwarding to the field type, by producing a trait implementations conflict error
402 // during compilation, if it does.
403 impl<T: Some> AsMut<bool> for Types<T>
404 where
405 T::Assoc: AsMut<bool>,
406 {
407 fn as_mut(&mut self) -> &mut bool {
408 self.0.as_mut()
409 }
410 }
411
412 let mut item = Types::<()>(Helper(1, 2.0, false));
413
414 let rf: &mut i32 = item.as_mut();
415 assert!(ptr::eq(rf, item.0.as_mut()));
416
417 let rf: &mut f64 = item.as_mut();
418 assert!(ptr::eq(rf, item.0.as_mut()));
419 }
420
421 #[test]
422 fn types_inner() {
423 #[derive(AsMut)]
424 #[as_mut(Vec<T>)]
425 struct TypesInner<T>(Vec<T>);
426
427 let mut item = TypesInner(vec![1i32]);
428
429 assert!(ptr::eq(item.as_mut(), &mut item.0));
430 }
431
432 #[test]
433 fn types_inner_assoc() {
434 #[derive(AsMut)]
435 #[as_mut(Vec<T::Assoc>)]
436 struct TypesInner<T: Some>(Vec<T::Assoc>);
437
438 let mut item = TypesInner::<u8>(vec![1i32]);
439
440 assert!(ptr::eq(item.as_mut(), &mut item.0));
441 }
442
443 #[test]
444 fn field_types() {
445 #[derive(AsMut)]
446 struct FieldTypes<T>(#[as_mut(i32, f64)] T);
447
448 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
449 // to the field type, by producing a trait implementations conflict error during
450 // compilation, if it does.
451 impl<T: AsMut<bool>> AsMut<bool> for FieldTypes<T> {
452 fn as_mut(&mut self) -> &mut bool {
453 self.0.as_mut()
454 }
455 }
456
457 let mut item = FieldTypes(Helper(1, 2.0, false));
458
459 let rf: &mut i32 = item.as_mut();
460 assert!(ptr::eq(rf, item.0.as_mut()));
461
462 let rf: &mut f64 = item.as_mut();
463 assert!(ptr::eq(rf, item.0.as_mut()));
464 }
465
466 #[test]
467 fn field_types_assoc() {
468 #[derive(AsMut)]
469 struct FieldTypes<T: Some>(#[as_mut(i32, f64)] T::Assoc);
470
471 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
472 // to the field type, by producing a trait implementations conflict error during
473 // compilation, if it does.
474 impl<T: Some> AsMut<bool> for FieldTypes<T>
475 where
476 T::Assoc: AsMut<bool>,
477 {
478 fn as_mut(&mut self) -> &mut bool {
479 self.0.as_mut()
480 }
481 }
482
483 let mut item = FieldTypes::<()>(Helper(1, 2.0, false));
484
485 let rf: &mut i32 = item.as_mut();
486 assert!(ptr::eq(rf, item.0.as_mut()));
487
488 let rf: &mut f64 = item.as_mut();
489 assert!(ptr::eq(rf, item.0.as_mut()));
490 }
491
492 #[test]
493 fn field_types_inner() {
494 #[derive(AsMut)]
495 struct FieldTypesInner<T>(#[as_mut(Vec<T>)] Vec<T>);
496
497 let mut item = FieldTypesInner(vec![1i32]);
498
499 assert!(ptr::eq(item.as_mut(), &mut item.0));
500 }
501
502 #[test]
503 fn field_types_inner_assoc() {
504 #[derive(AsMut)]
505 struct FieldTypesInner<T: Some>(
506 #[as_mut(Vec<<T as Some>::Assoc>)] Vec<<T as Some>::Assoc>,
507 );
508
509 let mut item = FieldTypesInner::<u8>(vec![1i32]);
510
511 assert!(ptr::eq(item.as_mut(), &mut item.0));
512 }
513
514 #[test]
515 fn lifetime() {
516 #[derive(AsMut)]
517 #[as_mut(i32)]
518 struct Lifetime<'a>(LifetimeHelper<'a>);
519
520 let mut item = Lifetime(LifetimeHelper::new(0));
521
522 assert!(ptr::eq(item.as_mut(), item.0.as_mut()));
523 }
524
525 #[test]
526 fn field_lifetime() {
527 #[derive(AsMut)]
528 struct FieldLifetime<'a>(#[as_mut(i32)] LifetimeHelper<'a>);
529
530 let mut item = FieldLifetime(LifetimeHelper::new(0));
531
532 assert!(ptr::eq(item.as_mut(), item.0.as_mut()));
533 }
534
535 #[test]
536 fn const_param() {
537 #[derive(AsMut)]
538 #[as_mut([i32])]
539 struct ConstParam<const N: usize>(ConstParamHelper<N>);
540
541 let mut item = ConstParam(ConstParamHelper([]));
542
543 assert!(ptr::eq(item.as_mut(), item.0.as_mut()));
544 }
545
546 #[test]
547 fn field_const_param() {
548 #[derive(AsMut)]
549 struct FieldConstParam<const N: usize>(
550 #[as_mut([i32])] ConstParamHelper<N>,
551 );
552
553 let mut item = FieldConstParam(ConstParamHelper([]));
554
555 assert!(ptr::eq(item.as_mut(), item.0.as_mut()));
556 }
557 }
558
559 #[cfg(nightly)]
560 mod never {
561 use super::*;
562
563 #[derive(AsMut)]
564 struct Nothing(!);
565 }
566
567 mod deprecated {
568 use super::*;
569
570 #[derive(AsMut)]
571 #[deprecated(note = "struct")]
572 struct Deprecated(#[deprecated(note = "field")] i32);
573 }
574 }
575
576 mod named {
577 use super::*;
578
579 #[test]
580 fn nothing() {
581 #[derive(AsMut)]
582 struct Nothing {
583 first: String,
584 }
585
586 let mut item = Nothing {
587 first: "test".to_owned(),
588 };
589
590 assert!(ptr::eq(item.as_mut(), &mut item.first));
591 }
592
593 #[test]
594 fn forward() {
595 #[derive(AsMut)]
596 #[as_mut(forward)]
597 struct Forward {
598 first: String,
599 }
600
601 let mut item = Forward {
602 first: "test".to_owned(),
603 };
604
605 let rf: &mut str = item.as_mut();
606 assert!(ptr::eq(rf, item.first.as_mut()));
607 }
608
609 #[test]
610 fn field() {
611 #[derive(AsMut)]
612 struct Field {
613 #[as_mut]
614 first: String,
615 }
616
617 let mut item = Field {
618 first: "test".to_owned(),
619 };
620
621 assert!(ptr::eq(item.as_mut(), &mut item.first));
622 }
623
624 #[test]
625 fn field_forward() {
626 #[derive(AsMut)]
627 struct FieldForward {
628 #[as_mut(forward)]
629 first: String,
630 }
631
632 let mut item = FieldForward {
633 first: "test".to_owned(),
634 };
635
636 let rf: &mut str = item.as_mut();
637 assert!(ptr::eq(rf, item.first.as_mut()));
638 }
639
640 #[test]
641 fn types() {
642 #[derive(AsMut)]
643 #[as_mut(i32, f64)]
644 struct Types {
645 first: Helper,
646 }
647
648 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
649 // to the field type, by producing a trait implementations conflict error during
650 // compilation, if it does.
651 impl AsMut<bool> for Types {
652 fn as_mut(&mut self) -> &mut bool {
653 self.first.as_mut()
654 }
655 }
656
657 // Asserts that the macro expansion doesn't generate an `AsMut` impl for the field type,
658 // by producing a trait implementations conflict error during compilation, if it does.
659 impl AsMut<Helper> for Types {
660 fn as_mut(&mut self) -> &mut Helper {
661 &mut self.first
662 }
663 }
664
665 let mut item = Types {
666 first: Helper(1, 2.0, false),
667 };
668
669 let rf: &mut i32 = item.as_mut();
670 assert!(ptr::eq(rf, item.first.as_mut()));
671
672 let rf: &mut f64 = item.as_mut();
673 assert!(ptr::eq(rf, item.first.as_mut()));
674 }
675
676 #[test]
677 fn types_with_inner() {
678 #[derive(AsMut)]
679 #[as_mut(i32, Helper)]
680 struct TypesWithInner {
681 first: Helper,
682 }
683
684 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
685 // to the field type, by producing a trait implementations conflict error during
686 // compilation, if it does.
687 impl AsMut<bool> for TypesWithInner {
688 fn as_mut(&mut self) -> &mut bool {
689 self.first.as_mut()
690 }
691 }
692
693 let mut item = TypesWithInner {
694 first: Helper(1, 2.0, false),
695 };
696
697 let rf: &mut i32 = item.as_mut();
698 assert!(ptr::eq(rf, item.first.as_mut()));
699
700 let rf: &mut Helper = item.as_mut();
701 assert!(ptr::eq(rf, &mut item.first));
702 }
703
704 #[test]
705 fn types_with_renamed_inner() {
706 type RenamedFoo = Helper;
707
708 #[derive(AsMut)]
709 #[as_mut(i32, RenamedFoo)]
710 struct TypesWithRenamedInner {
711 first: Helper,
712 }
713
714 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
715 // to the field type, by producing a trait implementations conflict error during
716 // compilation, if it does.
717 impl AsMut<bool> for TypesWithRenamedInner {
718 fn as_mut(&mut self) -> &mut bool {
719 self.first.as_mut()
720 }
721 }
722
723 let mut item = TypesWithRenamedInner {
724 first: Helper(1, 2.0, false),
725 };
726
727 let rf: &mut i32 = item.as_mut();
728 assert!(ptr::eq(rf, item.first.as_mut()));
729
730 let rf: &mut Helper = item.as_mut();
731 assert!(ptr::eq(rf, &mut item.first));
732 }
733
734 #[test]
735 fn field_types() {
736 #[derive(AsMut)]
737 struct FieldTypes {
738 #[as_mut(i32, f64)]
739 first: Helper,
740 }
741
742 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
743 // to the field type, by producing a trait implementations conflict error during
744 // compilation, if it does.
745 impl AsMut<bool> for FieldTypes {
746 fn as_mut(&mut self) -> &mut bool {
747 self.first.as_mut()
748 }
749 }
750
751 // Asserts that the macro expansion doesn't generate an `AsMut` impl for the field type,
752 // by producing a trait implementations conflict error during compilation, if it does.
753 impl AsMut<Helper> for FieldTypes {
754 fn as_mut(&mut self) -> &mut Helper {
755 &mut self.first
756 }
757 }
758
759 let mut item = FieldTypes {
760 first: Helper(1, 2.0, false),
761 };
762
763 let rf: &mut i32 = item.as_mut();
764 assert!(ptr::eq(rf, item.first.as_mut()));
765
766 let rf: &mut f64 = item.as_mut();
767 assert!(ptr::eq(rf, item.first.as_mut()));
768 }
769
770 #[test]
771 fn field_types_with_inner() {
772 #[derive(AsMut)]
773 struct FieldTypesWithInner {
774 #[as_mut(i32, Helper)]
775 first: Helper,
776 }
777
778 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
779 // to the field type, by producing a trait implementations conflict error during
780 // compilation, if it does.
781 impl AsMut<bool> for FieldTypesWithInner {
782 fn as_mut(&mut self) -> &mut bool {
783 self.first.as_mut()
784 }
785 }
786
787 let mut item = FieldTypesWithInner {
788 first: Helper(1, 2.0, false),
789 };
790
791 let rf: &mut i32 = item.as_mut();
792 assert!(ptr::eq(rf, item.first.as_mut()));
793
794 let rf: &mut Helper = item.as_mut();
795 assert!(ptr::eq(rf, &mut item.first));
796 }
797
798 #[test]
799 fn field_types_with_renamed_inner() {
800 type RenamedFoo = Helper;
801
802 #[derive(AsMut)]
803 struct FieldTypesWithRenamedInner {
804 #[as_mut(i32, RenamedFoo)]
805 first: Helper,
806 }
807
808 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl forwarding
809 // to the field type, by producing a trait implementations conflict error during
810 // compilation, if it does.
811 impl AsMut<bool> for FieldTypesWithRenamedInner {
812 fn as_mut(&mut self) -> &mut bool {
813 self.first.as_mut()
814 }
815 }
816
817 let mut item = FieldTypesWithRenamedInner {
818 first: Helper(1, 2.0, false),
819 };
820
821 let rf: &mut i32 = item.as_mut();
822 assert!(ptr::eq(rf, item.first.as_mut()));
823
824 let rf: &mut Helper = item.as_mut();
825 assert!(ptr::eq(rf, &mut item.first));
826 }
827
828 mod generic {
829 use super::*;
830
831 #[test]
832 fn nothing() {
833 #[derive(AsMut)]
834 struct Nothing<T> {
835 first: T,
836 }
837
838 let mut item = Nothing {
839 first: "test".to_owned(),
840 };
841
842 assert!(ptr::eq(item.as_mut(), &mut item.first));
843 }
844
845 #[test]
846 fn nothing_assoc() {
847 #[derive(AsMut)]
848 struct Nothing<T: Some> {
849 first: <T as Some>::Assoc,
850 }
851
852 let mut item = Nothing::<bool> {
853 first: "test".to_owned(),
854 };
855
856 assert!(ptr::eq(item.as_mut(), &mut item.first));
857 }
858
859 #[test]
860 fn struct_forward() {
861 #[derive(AsMut)]
862 #[as_mut(forward)]
863 struct Forward<T> {
864 first: T,
865 }
866
867 let mut item = Forward {
868 first: "test".to_owned(),
869 };
870
871 let rf: &mut str = item.as_mut();
872 assert!(ptr::eq(rf, item.first.as_mut()));
873 }
874
875 #[test]
876 fn struct_forward_assoc() {
877 #[derive(AsMut)]
878 #[as_mut(forward)]
879 struct Forward<T: Some> {
880 first: T::Assoc,
881 }
882
883 let mut item = Forward::<bool> {
884 first: "test".to_owned(),
885 };
886
887 let rf: &mut str = item.as_mut();
888 assert!(ptr::eq(rf, item.first.as_mut()));
889 }
890
891 #[test]
892 fn field() {
893 #[derive(AsMut)]
894 struct Field<T> {
895 #[as_mut]
896 first: T,
897 }
898
899 let mut item = Field {
900 first: "test".to_owned(),
901 };
902
903 assert!(ptr::eq(item.as_mut(), &mut item.first));
904 }
905
906 #[test]
907 fn field_forward() {
908 #[derive(AsMut)]
909 struct FieldForward<T> {
910 #[as_mut(forward)]
911 first: T,
912 }
913
914 let mut item = FieldForward {
915 first: "test".to_owned(),
916 };
917
918 let rf: &mut str = item.as_mut();
919 assert!(ptr::eq(rf, item.first.as_mut()));
920 }
921
922 #[test]
923 fn field_forward_assoc() {
924 #[derive(AsMut)]
925 struct FieldForward<T: Some> {
926 #[as_mut(forward)]
927 first: T::Assoc,
928 }
929
930 let mut item = FieldForward::<bool> {
931 first: "test".to_owned(),
932 };
933
934 let rf: &mut str = item.as_mut();
935 assert!(ptr::eq(rf, item.first.as_mut()));
936 }
937
938 #[test]
939 fn types() {
940 #[derive(AsMut)]
941 #[as_mut(i32, f64)]
942 struct Types<T> {
943 first: T,
944 }
945
946 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl
947 // forwarding to the field type, by producing a trait implementations conflict error
948 // during compilation, if it does.
949 impl<T: AsMut<bool>> AsMut<bool> for Types<T> {
950 fn as_mut(&mut self) -> &mut bool {
951 self.first.as_mut()
952 }
953 }
954
955 let mut item = Types {
956 first: Helper(1, 2.0, false),
957 };
958
959 let rf: &mut i32 = item.as_mut();
960 assert!(ptr::eq(rf, item.first.as_mut()));
961
962 let rf: &mut f64 = item.as_mut();
963 assert!(ptr::eq(rf, item.first.as_mut()));
964 }
965
966 #[test]
967 fn types_assoc() {
968 #[derive(AsMut)]
969 #[as_mut(i32, f64)]
970 struct Types<T: Some> {
971 first: T::Assoc,
972 }
973
974 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl
975 // forwarding to the field type, by producing a trait implementations conflict error
976 // during compilation, if it does.
977 impl<T: Some> AsMut<bool> for Types<T>
978 where
979 T::Assoc: AsMut<bool>,
980 {
981 fn as_mut(&mut self) -> &mut bool {
982 self.first.as_mut()
983 }
984 }
985
986 let mut item = Types::<()> {
987 first: Helper(1, 2.0, false),
988 };
989
990 let rf: &mut i32 = item.as_mut();
991 assert!(ptr::eq(rf, item.first.as_mut()));
992
993 let rf: &mut f64 = item.as_mut();
994 assert!(ptr::eq(rf, item.first.as_mut()));
995 }
996
997 #[test]
998 fn types_inner() {
999 #[derive(AsMut)]
1000 #[as_mut(Vec<T>)]
1001 struct TypesInner<T> {
1002 first: Vec<T>,
1003 }
1004
1005 let mut item = TypesInner { first: vec![1i32] };
1006
1007 assert!(ptr::eq(item.as_mut(), &mut item.first));
1008 }
1009
1010 #[test]
1011 fn types_inner_assoc() {
1012 #[derive(AsMut)]
1013 #[as_mut(Vec<T::Assoc>)]
1014 struct TypesInner<T: Some> {
1015 first: Vec<T::Assoc>,
1016 }
1017
1018 let mut item = TypesInner::<u8> { first: vec![1i32] };
1019
1020 assert!(ptr::eq(item.as_mut(), &mut item.first));
1021 }
1022
1023 #[test]
1024 fn field_types() {
1025 #[derive(AsMut)]
1026 struct FieldTypes<T> {
1027 #[as_mut(i32, f64)]
1028 first: T,
1029 }
1030
1031 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl
1032 // forwarding to the field type, by producing a trait implementations conflict error
1033 // during compilation, if it does.
1034 impl<T: AsMut<bool>> AsMut<bool> for FieldTypes<T> {
1035 fn as_mut(&mut self) -> &mut bool {
1036 self.first.as_mut()
1037 }
1038 }
1039
1040 let mut item = FieldTypes {
1041 first: Helper(1, 2.0, false),
1042 };
1043
1044 let rf: &mut i32 = item.as_mut();
1045 assert!(ptr::eq(rf, item.first.as_mut()));
1046
1047 let rf: &mut f64 = item.as_mut();
1048 assert!(ptr::eq(rf, item.first.as_mut()));
1049 }
1050
1051 #[test]
1052 fn field_types_assoc() {
1053 #[derive(AsMut)]
1054 struct FieldTypes<T: Some> {
1055 #[as_mut(i32, f64)]
1056 first: T::Assoc,
1057 }
1058
1059 // Asserts that the macro expansion doesn't generate a blanket `AsMut` impl
1060 // forwarding to the field type, by producing a trait implementations conflict error
1061 // during compilation, if it does.
1062 impl<T: Some> AsMut<bool> for FieldTypes<T>
1063 where
1064 T::Assoc: AsMut<bool>,
1065 {
1066 fn as_mut(&mut self) -> &mut bool {
1067 self.first.as_mut()
1068 }
1069 }
1070
1071 let mut item = FieldTypes::<()> {
1072 first: Helper(1, 2.0, false),
1073 };
1074
1075 let rf: &mut i32 = item.as_mut();
1076 assert!(ptr::eq(rf, item.first.as_mut()));
1077
1078 let rf: &mut f64 = item.as_mut();
1079 assert!(ptr::eq(rf, item.first.as_mut()));
1080 }
1081
1082 #[test]
1083 fn field_types_inner() {
1084 #[derive(AsMut)]
1085 struct FieldTypesInner<T> {
1086 #[as_mut(Vec<T>)]
1087 first: Vec<T>,
1088 }
1089
1090 let mut item = FieldTypesInner { first: vec![1i32] };
1091
1092 assert!(ptr::eq(item.as_mut(), &mut item.first));
1093 }
1094
1095 #[test]
1096 fn field_types_inner_assoc() {
1097 #[derive(AsMut)]
1098 struct FieldTypesInner<T: Some> {
1099 #[as_mut(Vec<<T as Some>::Assoc>)]
1100 first: Vec<<T as Some>::Assoc>,
1101 }
1102
1103 let mut item = FieldTypesInner::<u8> { first: vec![1i32] };
1104
1105 assert!(ptr::eq(item.as_mut(), &mut item.first));
1106 }
1107
1108 #[test]
1109 fn lifetime() {
1110 #[derive(AsMut)]
1111 #[as_mut(i32)]
1112 struct Lifetime<'a> {
1113 first: LifetimeHelper<'a>,
1114 }
1115
1116 let mut item = Lifetime {
1117 first: LifetimeHelper::new(0),
1118 };
1119
1120 assert!(ptr::eq(item.as_mut(), item.first.as_mut()));
1121 }
1122
1123 #[test]
1124 fn field_lifetime() {
1125 #[derive(AsMut)]
1126 struct FieldLifetime<'a> {
1127 #[as_mut(i32)]
1128 first: LifetimeHelper<'a>,
1129 }
1130
1131 let mut item = FieldLifetime {
1132 first: LifetimeHelper::new(0),
1133 };
1134
1135 assert!(ptr::eq(item.as_mut(), item.first.as_mut()));
1136 }
1137
1138 #[test]
1139 fn const_param() {
1140 #[derive(AsMut)]
1141 #[as_mut([i32])]
1142 struct ConstParam<const N: usize> {
1143 first: ConstParamHelper<N>,
1144 }
1145
1146 let mut item = ConstParam {
1147 first: ConstParamHelper([]),
1148 };
1149
1150 assert!(ptr::eq(item.as_mut(), item.first.as_mut()));
1151 }
1152
1153 #[test]
1154 fn field_const_param() {
1155 #[derive(AsMut)]
1156 struct FieldConstParam<const N: usize> {
1157 #[as_mut([i32])]
1158 first: ConstParamHelper<N>,
1159 }
1160
1161 let mut item = FieldConstParam {
1162 first: ConstParamHelper([]),
1163 };
1164
1165 assert!(ptr::eq(item.as_mut(), item.first.as_mut()));
1166 }
1167 }
1168
1169 #[cfg(nightly)]
1170 mod never {
1171 use super::*;
1172
1173 #[derive(AsMut)]
1174 struct Nothing {
1175 first: !,
1176 }
1177 }
1178
1179 mod deprecated {
1180 use super::*;
1181
1182 #[derive(AsMut)]
1183 #[deprecated(note = "struct")]
1184 struct Deprecated {
1185 #[deprecated(note = "field")]
1186 field: i32,
1187 }
1188 }
1189 }
1190 }
1191
1192 mod multi_field {
1193 use super::*;
1194
1195 mod tuple {
1196 use super::*;
1197
1198 #[test]
1199 fn nothing() {
1200 #[derive(AsMut)]
1201 struct Nothing(String, i32);
1202
1203 let mut item = Nothing("test".to_owned(), 0);
1204
1205 assert!(ptr::eq(item.as_mut(), &mut item.0));
1206 assert!(ptr::eq(item.as_mut(), &mut item.1));
1207 }
1208
1209 #[test]
1210 fn skip() {
1211 #[derive(AsMut)]
1212 struct Skip(String, i32, #[as_mut(skip)] f64);
1213
1214 // Asserts that the macro expansion doesn't generate `AsMut` impl for the skipped field,
1215 // by producing trait implementations conflict error during compilation, if it does.
1216 impl AsMut<f64> for Skip {
1217 fn as_mut(&mut self) -> &mut f64 {
1218 &mut self.2
1219 }
1220 }
1221
1222 let mut item = Skip("test".to_owned(), 0, 0.0);
1223
1224 assert!(ptr::eq(item.as_mut(), &mut item.0));
1225 assert!(ptr::eq(item.as_mut(), &mut item.1));
1226 }
1227
1228 #[test]
1229 fn field() {
1230 #[derive(AsMut)]
1231 struct Field(#[as_mut] String, #[as_mut] i32, f64);
1232
1233 // Asserts that the macro expansion doesn't generate `AsMut` impl for the third field,
1234 // by producing trait implementations conflict error during compilation, if it does.
1235 impl AsMut<f64> for Field {
1236 fn as_mut(&mut self) -> &mut f64 {
1237 &mut self.2
1238 }
1239 }
1240
1241 let mut item = Field("test".to_owned(), 0, 0.0);
1242
1243 assert!(ptr::eq(item.as_mut(), &mut item.0));
1244 assert!(ptr::eq(item.as_mut(), &mut item.1));
1245 }
1246
1247 #[test]
1248 fn field_forward() {
1249 #[derive(AsMut)]
1250 struct FieldForward(#[as_mut(forward)] String, i32);
1251
1252 let mut item = FieldForward("test".to_owned(), 0);
1253
1254 let rf: &mut str = item.as_mut();
1255 assert!(ptr::eq(rf, item.0.as_mut()));
1256 }
1257
1258 #[test]
1259 fn types() {
1260 type RenamedString = String;
1261
1262 #[derive(AsMut)]
1263 struct Types(
1264 #[as_mut(str, RenamedString)] String,
1265 #[as_mut([u8])] Vec<u8>,
1266 );
1267
1268 // Asserts that the macro expansion doesn't generate `AsMut` impl for the field type, by
1269 // producing trait implementations conflict error during compilation, if it does.
1270 impl AsMut<Vec<u8>> for Types {
1271 fn as_mut(&mut self) -> &mut Vec<u8> {
1272 &mut self.1
1273 }
1274 }
1275
1276 let mut item = Types("test".to_owned(), vec![0]);
1277
1278 let rf: &mut str = item.as_mut();
1279 assert!(ptr::eq(rf, item.0.as_mut()));
1280
1281 let rf: &mut String = item.as_mut();
1282 assert!(ptr::eq(rf, &mut item.0));
1283
1284 let rf: &mut [u8] = item.as_mut();
1285 assert!(ptr::eq(rf, item.1.as_mut()));
1286 }
1287
1288 mod generic {
1289 use super::*;
1290
1291 #[test]
1292 fn nothing() {
1293 #[derive(AsMut)]
1294 struct Nothing<T, U>(Vec<T>, VecDeque<U>);
1295
1296 let mut item = Nothing(vec![1], VecDeque::from([2]));
1297
1298 assert!(ptr::eq(item.as_mut(), &mut item.0));
1299 assert!(ptr::eq(item.as_mut(), &mut item.1));
1300 }
1301
1302 #[test]
1303 fn skip() {
1304 #[derive(AsMut)]
1305 struct Skip<T, U, V>(Vec<T>, VecDeque<U>, #[as_mut(skip)] V);
1306
1307 let mut item = Skip(vec![1], VecDeque::from([2]), 0);
1308
1309 assert!(ptr::eq(item.as_mut(), &mut item.0));
1310 assert!(ptr::eq(item.as_mut(), &mut item.1));
1311 }
1312
1313 #[test]
1314 fn field() {
1315 #[derive(AsMut)]
1316 struct Field<T, U, V>(#[as_mut] Vec<T>, #[as_mut] VecDeque<U>, V);
1317
1318 let mut item = Field(vec![1], VecDeque::from([2]), 0);
1319
1320 assert!(ptr::eq(item.as_mut(), &mut item.0));
1321 assert!(ptr::eq(item.as_mut(), &mut item.1));
1322 }
1323
1324 #[test]
1325 fn field_forward() {
1326 #[derive(AsMut)]
1327 struct FieldForward<T, U>(#[as_mut(forward)] T, U);
1328
1329 let mut item = FieldForward("test".to_owned(), 0);
1330
1331 let rf: &mut str = item.as_mut();
1332 assert!(ptr::eq(rf, item.0.as_mut()));
1333 }
1334
1335 #[test]
1336 fn field_forward_assoc() {
1337 #[derive(AsMut)]
1338 struct FieldForward<T: Some, U>(#[as_mut(forward)] T::Assoc, U);
1339
1340 let mut item = FieldForward::<bool, _>("test".to_owned(), 0);
1341
1342 let rf: &mut str = item.as_mut();
1343 assert!(ptr::eq(rf, item.0.as_mut()));
1344 }
1345
1346 #[test]
1347 fn types() {
1348 #[derive(AsMut)]
1349 struct Types<T, U>(#[as_mut(str)] T, #[as_mut([u8])] U);
1350
1351 let mut item = Types("test".to_owned(), vec![0]);
1352
1353 let rf: &mut str = item.as_mut();
1354 assert!(ptr::eq(rf, item.0.as_mut()));
1355
1356 let rf: &mut [u8] = item.as_mut();
1357 assert!(ptr::eq(rf, item.1.as_mut()));
1358 }
1359
1360 #[test]
1361 fn types_assoc() {
1362 #[derive(AsMut)]
1363 struct Types<T: Some, U: Some + ?Sized>(
1364 #[as_mut(str)] <T as Some>::Assoc,
1365 #[as_mut([u8])] Vec<U::Assoc>,
1366 );
1367
1368 let mut item = Types::<bool, str>("test".to_owned(), vec![0]);
1369
1370 let rf: &mut str = item.as_mut();
1371 assert!(ptr::eq(rf, item.0.as_mut()));
1372
1373 let rf: &mut [u8] = item.as_mut();
1374 assert!(ptr::eq(rf, item.1.as_mut()));
1375 }
1376
1377 #[test]
1378 fn types_with_inner() {
1379 #[derive(AsMut)]
1380 struct TypesWithInner<T, U>(
1381 #[as_mut(Vec<T>, [T])] Vec<T>,
1382 #[as_mut(str)] U,
1383 );
1384
1385 let mut item = TypesWithInner(vec![1i32], "a".to_owned());
1386
1387 let rf: &mut Vec<i32> = item.as_mut();
1388 assert!(ptr::eq(rf, &mut item.0));
1389
1390 let rf: &mut [i32] = item.as_mut();
1391 assert!(ptr::eq(rf, item.0.as_mut()));
1392
1393 let rf: &mut str = item.as_mut();
1394 assert!(ptr::eq(rf, item.1.as_mut()));
1395 }
1396
1397 #[test]
1398 fn types_with_inner_assoc() {
1399 #[derive(AsMut)]
1400 struct TypesWithInner<T: Some, U: Some>(
1401 #[as_mut(Vec<T::Assoc>, [T::Assoc])] Vec<T::Assoc>,
1402 #[as_mut(str)] U::Assoc,
1403 );
1404
1405 let mut item = TypesWithInner::<u8, bool>(vec![1i32], "a".to_owned());
1406
1407 let rf: &mut Vec<i32> = item.as_mut();
1408 assert!(ptr::eq(rf, &mut item.0));
1409
1410 let rf: &mut [i32] = item.as_mut();
1411 assert!(ptr::eq(rf, item.0.as_mut()));
1412
1413 let rf: &mut str = item.as_mut();
1414 assert!(ptr::eq(rf, item.1.as_mut()));
1415 }
1416
1417 #[test]
1418 fn field_non_generic() {
1419 #[derive(AsMut)]
1420 struct FieldNonGeneric<T>(#[as_mut([T])] Vec<i32>, T);
1421
1422 let mut item = FieldNonGeneric(vec![], 2i32);
1423
1424 assert!(ptr::eq(item.as_mut(), item.0.as_mut()));
1425 }
1426
1427 #[test]
1428 fn field_non_generic_assoc() {
1429 #[derive(AsMut)]
1430 struct FieldNonGeneric<T: Some>(#[as_mut([T::Assoc])] Vec<i32>, T);
1431
1432 let mut item = FieldNonGeneric::<u8>(vec![], 2u8);
1433
1434 assert!(ptr::eq(item.as_mut(), item.0.as_mut()));
1435 }
1436 }
1437
1438 #[cfg(nightly)]
1439 mod never {
1440 use super::*;
1441
1442 #[derive(AsMut)]
1443 struct Nothing(String, !);
1444 }
1445 }
1446
1447 mod named {
1448 use super::*;
1449
1450 #[test]
1451 fn nothing() {
1452 #[derive(AsMut)]
1453 struct Nothing {
1454 first: String,
1455 second: i32,
1456 }
1457
1458 let mut item = Nothing {
1459 first: "test".to_owned(),
1460 second: 0,
1461 };
1462
1463 assert!(ptr::eq(item.as_mut(), &mut item.first));
1464 assert!(ptr::eq(item.as_mut(), &mut item.second));
1465 }
1466
1467 #[test]
1468 fn skip() {
1469 #[derive(AsMut)]
1470 struct Skip {
1471 first: String,
1472 second: i32,
1473 #[as_mut(skip)]
1474 third: f64,
1475 }
1476
1477 // Asserts that the macro expansion doesn't generate `AsMut` impl for the skipped field,
1478 // by producing trait implementations conflict error during compilation, if it does.
1479 impl AsMut<f64> for Skip {
1480 fn as_mut(&mut self) -> &mut f64 {
1481 &mut self.third
1482 }
1483 }
1484
1485 let mut item = Skip {
1486 first: "test".to_owned(),
1487 second: 0,
1488 third: 0.0,
1489 };
1490
1491 assert!(ptr::eq(item.as_mut(), &mut item.first));
1492 assert!(ptr::eq(item.as_mut(), &mut item.second));
1493 }
1494
1495 #[test]
1496 fn field() {
1497 #[derive(AsMut)]
1498 struct Field {
1499 #[as_mut]
1500 first: String,
1501 #[as_mut]
1502 second: i32,
1503 third: f64,
1504 }
1505
1506 // Asserts that the macro expansion doesn't generate `AsMut` impl for the `third` field,
1507 // by producing trait implementations conflict error during compilation, if it does.
1508 impl AsMut<f64> for Field {
1509 fn as_mut(&mut self) -> &mut f64 {
1510 &mut self.third
1511 }
1512 }
1513
1514 let mut item = Field {
1515 first: "test".to_owned(),
1516 second: 0,
1517 third: 0.0,
1518 };
1519
1520 assert!(ptr::eq(item.as_mut(), &mut item.first));
1521 assert!(ptr::eq(item.as_mut(), &mut item.second));
1522 }
1523
1524 #[test]
1525 fn field_forward() {
1526 #[derive(AsMut)]
1527 struct FieldForward {
1528 #[as_mut(forward)]
1529 first: String,
1530 second: i32,
1531 }
1532
1533 let mut item = FieldForward {
1534 first: "test".to_owned(),
1535 second: 0,
1536 };
1537
1538 let rf: &mut str = item.as_mut();
1539 assert!(ptr::eq(rf, item.first.as_mut()));
1540 }
1541
1542 #[test]
1543 fn types() {
1544 type RenamedString = String;
1545
1546 #[derive(AsMut)]
1547 struct Types {
1548 #[as_mut(str, RenamedString)]
1549 first: String,
1550 #[as_mut([u8])]
1551 second: Vec<u8>,
1552 }
1553
1554 // Asserts that the macro expansion doesn't generate `AsMut` impl for unmentioned type,
1555 // by producing trait implementations conflict error during compilation, if it does.
1556 impl AsMut<Vec<u8>> for Types {
1557 fn as_mut(&mut self) -> &mut Vec<u8> {
1558 &mut self.second
1559 }
1560 }
1561
1562 let mut item = Types {
1563 first: "test".to_owned(),
1564 second: vec![0],
1565 };
1566
1567 let rf: &mut str = item.as_mut();
1568 assert!(ptr::eq(rf, item.first.as_mut()));
1569
1570 let rf: &mut String = item.as_mut();
1571 assert!(ptr::eq(rf, &mut item.first));
1572
1573 let rf: &mut [u8] = item.as_mut();
1574 assert!(ptr::eq(rf, item.second.as_mut()));
1575 }
1576
1577 mod generic {
1578 use super::*;
1579
1580 #[test]
1581 fn nothing() {
1582 #[derive(AsMut)]
1583 struct Nothing<T, U> {
1584 first: Vec<T>,
1585 second: VecDeque<U>,
1586 }
1587
1588 let mut item = Nothing {
1589 first: vec![1],
1590 second: VecDeque::from([2]),
1591 };
1592
1593 assert!(ptr::eq(item.as_mut(), &mut item.first));
1594 assert!(ptr::eq(item.as_mut(), &mut item.second));
1595 }
1596
1597 #[test]
1598 fn skip() {
1599 #[derive(AsMut)]
1600 struct Skip<T, U, V> {
1601 first: Vec<T>,
1602 second: VecDeque<U>,
1603 #[as_mut(skip)]
1604 third: V,
1605 }
1606
1607 let mut item = Skip {
1608 first: vec![1],
1609 second: VecDeque::from([2]),
1610 third: 0,
1611 };
1612
1613 assert!(ptr::eq(item.as_mut(), &mut item.first));
1614 assert!(ptr::eq(item.as_mut(), &mut item.second));
1615 }
1616
1617 #[test]
1618 fn field() {
1619 #[derive(AsMut)]
1620 struct Field<T, U, V> {
1621 #[as_mut]
1622 first: Vec<T>,
1623 #[as_mut]
1624 second: VecDeque<U>,
1625 third: V,
1626 }
1627
1628 let mut item = Field {
1629 first: vec![1],
1630 second: VecDeque::from([2]),
1631 third: 0,
1632 };
1633
1634 assert!(ptr::eq(item.as_mut(), &mut item.first));
1635 assert!(ptr::eq(item.as_mut(), &mut item.second));
1636 }
1637
1638 #[test]
1639 fn field_forward() {
1640 #[derive(AsMut)]
1641 struct FieldForward<T, U> {
1642 #[as_mut(forward)]
1643 first: T,
1644 second: U,
1645 }
1646
1647 let mut item = FieldForward {
1648 first: "test".to_owned(),
1649 second: 0,
1650 };
1651
1652 let rf: &mut str = item.as_mut();
1653 assert!(ptr::eq(rf, item.first.as_mut()));
1654 }
1655
1656 #[test]
1657 fn field_forward_assoc() {
1658 #[derive(AsMut)]
1659 struct FieldForward<T: Some, U> {
1660 #[as_mut(forward)]
1661 first: T::Assoc,
1662 second: U,
1663 }
1664
1665 let mut item = FieldForward::<bool, _> {
1666 first: "test".to_owned(),
1667 second: 0,
1668 };
1669
1670 let rf: &mut str = item.as_mut();
1671 assert!(ptr::eq(rf, item.first.as_mut()));
1672 }
1673
1674 #[test]
1675 fn types() {
1676 #[derive(AsMut)]
1677 struct Types<T, U> {
1678 #[as_mut(str)]
1679 first: T,
1680 #[as_mut([u8])]
1681 second: U,
1682 }
1683
1684 let mut item = Types {
1685 first: "test".to_owned(),
1686 second: vec![0],
1687 };
1688
1689 let rf: &mut str = item.as_mut();
1690 assert!(ptr::eq(rf, item.first.as_mut()));
1691
1692 let rf: &mut [u8] = item.as_mut();
1693 assert!(ptr::eq(rf, item.second.as_mut()));
1694 }
1695
1696 #[test]
1697 fn types_assoc() {
1698 #[derive(AsMut)]
1699 struct Types<T: Some, U: Some + ?Sized> {
1700 #[as_mut(str)]
1701 first: T::Assoc,
1702 #[as_mut([u8])]
1703 second: Vec<<U as Some>::Assoc>,
1704 }
1705
1706 let mut item = Types::<bool, str> {
1707 first: "test".to_owned(),
1708 second: vec![0],
1709 };
1710
1711 let rf: &mut str = item.as_mut();
1712 assert!(ptr::eq(rf, item.first.as_mut()));
1713
1714 let rf: &mut [u8] = item.as_mut();
1715 assert!(ptr::eq(rf, item.second.as_mut()));
1716 }
1717
1718 #[test]
1719 fn types_with_inner() {
1720 #[derive(AsMut)]
1721 struct TypesWithInner<T, U> {
1722 #[as_mut(Vec<T>, [T])]
1723 first: Vec<T>,
1724 #[as_mut(str)]
1725 second: U,
1726 }
1727
1728 let mut item = TypesWithInner {
1729 first: vec![1i32],
1730 second: "a".to_owned(),
1731 };
1732
1733 let rf: &mut Vec<i32> = item.as_mut();
1734 assert!(ptr::eq(rf, &mut item.first));
1735
1736 let rf: &mut [i32] = item.as_mut();
1737 assert!(ptr::eq(rf, item.first.as_mut()));
1738
1739 let rf: &mut str = item.as_mut();
1740 assert!(ptr::eq(rf, item.second.as_mut()));
1741 }
1742
1743 #[test]
1744 fn types_with_inner_assoc() {
1745 #[derive(AsMut)]
1746 struct TypesWithInner<T: Some, U: Some> {
1747 #[as_mut(Vec<<T as Some>::Assoc>, [<T as Some>::Assoc])]
1748 first: Vec<<T as Some>::Assoc>,
1749 #[as_mut(str)]
1750 second: U::Assoc,
1751 }
1752
1753 let mut item = TypesWithInner::<u8, bool> {
1754 first: vec![1i32],
1755 second: "a".to_owned(),
1756 };
1757
1758 let rf: &mut Vec<i32> = item.as_mut();
1759 assert!(ptr::eq(rf, &mut item.first));
1760
1761 let rf: &mut [i32] = item.as_mut();
1762 assert!(ptr::eq(rf, item.first.as_mut()));
1763
1764 let rf: &mut str = item.as_mut();
1765 assert!(ptr::eq(rf, item.second.as_mut()));
1766 }
1767
1768 #[test]
1769 fn field_non_generic() {
1770 #[derive(AsMut)]
1771 struct FieldNonGeneric<T> {
1772 #[as_mut([T])]
1773 first: Vec<i32>,
1774 second: T,
1775 }
1776
1777 let mut item = FieldNonGeneric {
1778 first: vec![],
1779 second: 2i32,
1780 };
1781
1782 assert!(ptr::eq(item.as_mut(), item.first.as_mut()));
1783 }
1784
1785 #[test]
1786 fn field_non_generic_assoc() {
1787 #[derive(AsMut)]
1788 struct FieldNonGeneric<T: Some> {
1789 #[as_mut([<T as Some>::Assoc])]
1790 first: Vec<i32>,
1791 second: T,
1792 }
1793
1794 let mut item = FieldNonGeneric::<u8> {
1795 first: vec![],
1796 second: 2u8,
1797 };
1798
1799 assert!(ptr::eq(item.as_mut(), item.first.as_mut()));
1800 }
1801 }
1802
1803 #[cfg(nightly)]
1804 mod never {
1805 use super::*;
1806
1807 #[derive(AsMut)]
1808 struct Nothing {
1809 first: !,
1810 second: i32,
1811 }
1812 }
1813 }
1814 }