Debian Codegraph @

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

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