Debian Codegraph @

third_party/rust/derive_more/tests/deref_mut.rs

raw
1 #![cfg_attr(not(feature = "std"), no_std)]
2 #![allow(dead_code, unused_imports)]
3
4 #[cfg(not(feature = "std"))]
5 extern crate alloc;
6
7 #[cfg(not(feature = "std"))]
8 use alloc::{boxed::Box, format, vec, vec::Vec};
9
10 use derive_more::DerefMut;
11
12 #[derive(DerefMut)]
13 #[deref_mut(forward)]
14 struct MyBoxedInt(Box<i32>);
15 // Deref implementation is needed for DerefMut
16 impl ::core::ops::Deref for MyBoxedInt {
17 type Target = <Box<i32> as ::core::ops::Deref>::Target;
18 #[inline]
19 fn deref(&self) -> &Self::Target {
20 <Box<i32> as ::core::ops::Deref>::deref(&self.0)
21 }
22 }
23
24 #[derive(DerefMut)]
25 struct NumRef<'a> {
26 #[deref_mut(forward)]
27 num: &'a mut i32,
28 }
29 // Deref implementation is needed for DerefMut
30 impl<'a> ::core::ops::Deref for NumRef<'a> {
31 type Target = <&'a mut i32 as ::core::ops::Deref>::Target;
32 #[inline]
33 fn deref(&self) -> &Self::Target {
34 <&'a mut i32 as ::core::ops::Deref>::deref(&self.num)
35 }
36 }
37
38 #[derive(DerefMut)]
39 #[deref_mut(forward)]
40 struct NumRef2<'a> {
41 num: &'a mut i32,
42 #[deref_mut(ignore)]
43 useless: bool,
44 }
45
46 // Deref implementation is needed for DerefMut
47 impl<'a> ::core::ops::Deref for NumRef2<'a> {
48 type Target = <&'a mut i32 as ::core::ops::Deref>::Target;
49 #[inline]
50 fn deref(&self) -> &Self::Target {
51 <&'a mut i32 as ::core::ops::Deref>::deref(&self.num)
52 }
53 }
54
55 #[derive(DerefMut)]
56 struct MyInt(i32);
57
58 // Deref implementation is needed for DerefMutToInner
59 impl ::core::ops::Deref for MyInt {
60 type Target = i32;
61 #[inline]
62 fn deref(&self) -> &Self::Target {
63 &self.0
64 }
65 }
66
67 #[derive(DerefMut)]
68 struct Point1D {
69 x: i32,
70 }
71
72 // Deref implementation is needed for DerefMutToInner
73 impl ::core::ops::Deref for Point1D {
74 type Target = i32;
75 #[inline]
76 fn deref(&self) -> &Self::Target {
77 &self.x
78 }
79 }
80
81 #[derive(DerefMut)]
82 struct CoolVec {
83 cool: bool,
84 #[deref_mut]
85 vec: Vec<i32>,
86 }
87 impl ::core::ops::Deref for CoolVec {
88 type Target = Vec<i32>;
89 #[inline]
90 fn deref(&self) -> &Self::Target {
91 &self.vec
92 }
93 }
94
95 #[derive(DerefMut)]
96 struct GenericVec<T>(Vec<T>);
97
98 impl<T> ::core::ops::Deref for GenericVec<T> {
99 type Target = Vec<T>;
100 #[inline]
101 fn deref(&self) -> &Self::Target {
102 &self.0
103 }
104 }
105
106 #[test]
107 fn deref_mut_generic() {
108 let mut gv = GenericVec::<i32>(vec![42]);
109 assert!(gv.get_mut(0).is_some());
110 }
111
112 #[derive(DerefMut)]
113 struct GenericBox<T>(#[deref_mut(forward)] Box<T>);
114
115 impl<T> ::core::ops::Deref for GenericBox<T>
116 where
117 Box<T>: ::core::ops::Deref,
118 {
119 type Target = <Box<T> as ::core::ops::Deref>::Target;
120 #[inline]
121 fn deref(&self) -> &Self::Target {
122 <Box<T> as ::core::ops::Deref>::deref(&self.0)
123 }
124 }
125
126 #[test]
127 fn deref_mut_generic_forward() {
128 let mut boxed = GenericBox(Box::new(1i32));
129 *boxed = 3;
130 assert_eq!(*boxed, 3i32);
131 }