wrapping_macro/
wrapping.rs1#[macro_export]
3macro_rules! wrapping {
4 (
5 $( #[$meta:meta] )*
6 $pub:vis struct $name:ident($inner_pub:vis $inner_ty:ty);
7 ) => {
8 $( #[$meta] )*
9 $pub struct $name($inner_pub $inner_ty);
10
11 impl $name {
12 pub fn from_inner(inner: $inner_ty) -> Self {
13 Self(inner)
14 }
15
16 pub fn inner(&self) -> &$inner_ty {
17 &self.0
18 }
19
20 pub fn into_inner(self) -> $inner_ty {
21 self.0
22 }
23 }
24
25 impl ::core::convert::From<$inner_ty> for $name {
26 fn from(v: $inner_ty) -> Self {
27 Self(v)
28 }
29 }
30
31 impl ::core::ops::Deref for $name {
32 type Target = $inner_ty;
33
34 fn deref(&self) -> &Self::Target {
35 &self.0
36 }
37 }
38
39 impl ::core::ops::DerefMut for $name {
40 fn deref_mut(&mut self) -> &mut Self::Target {
41 &mut self.0
42 }
43 }
44 }
45}
46
47#[cfg(feature = "alloc")]
49#[macro_export]
50macro_rules! wrapping_string {
51 (
52 $( #[$meta:meta] )*
53 $pub:vis struct $name:ident($inner_pub:vis $inner_ty:ty);
54 ) => {
55 wrapping_macro::wrapping!{
56 $( #[$meta] )*
57 $pub struct $name($inner_pub $inner_ty);
58 }
59
60 impl ::core::convert::From<&$crate::alloc::string::String> for $name {
61 fn from(v: &$crate::alloc::string::String) -> Self {
62 Self(v.to_owned())
63 }
64 }
65 impl ::core::convert::From<&::core::primitive::str> for $name {
66 fn from(v: &::core::primitive::str) -> Self {
67 Self(v.into())
68 }
69 }
70 impl ::core::convert::From<$crate::alloc::boxed::Box<::core::primitive::str>> for $name {
71 fn from(v: $crate::alloc::boxed::Box<::core::primitive::str>) -> Self {
72 Self(v.to_string())
73 }
74 }
75 impl ::core::convert::From<&$crate::alloc::boxed::Box<::core::primitive::str>> for $name {
76 fn from(v: &$crate::alloc::boxed::Box<::core::primitive::str>) -> Self {
77 Self(v.to_string())
78 }
79 }
80
81 impl ::core::fmt::Display for $name {
82 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
83 write!(f, "{}", self.0)
84 }
85 }
86
87 impl ::core::str::FromStr for $name {
88 type Err = ::core::convert::Infallible;
89
90 fn from_str(s: &::core::primitive::str) -> Result<Self, Self::Err> {
91 Ok(Self(s.into()))
92 }
93 }
94 }
95}
96
97#[cfg(feature = "alloc")]
99#[macro_export]
100macro_rules! wrapping_box_str {
101 (
102 $( #[$meta:meta] )*
103 $pub:vis struct $name:ident($inner_pub:vis $inner_ty:ty);
104 ) => {
105 wrapping_macro::wrapping!{
106 $( #[$meta] )*
107 $pub struct $name($inner_pub $inner_ty);
108 }
109
110 impl ::core::convert::From<$crate::alloc::string::String> for $name {
111 fn from(v: $crate::alloc::string::String) -> Self {
112 Self(v.as_str().into())
113 }
114 }
115 impl ::core::convert::From<&$crate::alloc::string::String> for $name {
116 fn from(v: &$crate::alloc::string::String) -> Self {
117 Self(v.as_str().into())
118 }
119 }
120 impl ::core::convert::From<&::core::primitive::str> for $name {
121 fn from(v: &::core::primitive::str) -> Self {
122 Self(v.into())
123 }
124 }
125 impl ::core::convert::From<&$crate::alloc::boxed::Box<::core::primitive::str>> for $name {
126 fn from(v: &$crate::alloc::boxed::Box<::core::primitive::str>) -> Self {
127 Self(v.to_owned())
128 }
129 }
130
131 impl ::core::fmt::Display for $name {
132 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
133 write!(f, "{}", self.0)
134 }
135 }
136
137 impl ::core::str::FromStr for $name {
138 type Err = ::core::convert::Infallible;
139
140 fn from_str(s: &::core::primitive::str) -> Result<Self, Self::Err> {
141 Ok(Self(s.into()))
142 }
143 }
144 }
145}
146
147#[macro_export]
149macro_rules! wrapping_int {
150 (
151 $( #[$meta:meta] )*
152 $pub:vis struct $name:ident($inner_pub:vis $inner_ty:ty);
153 ) => {
154 wrapping_macro::wrapping!{
155 $( #[$meta] )*
156 $pub struct $name($inner_pub $inner_ty);
157 }
158
159 impl ::core::fmt::Display for $name {
160 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
161 write!(f, "{}", self.0)
162 }
163 }
164
165 impl ::core::str::FromStr for $name {
166 type Err = ::core::num::ParseIntError;
167
168 fn from_str(s: &::core::primitive::str) -> Result<Self, Self::Err> {
169 s.parse::<$inner_ty>().map(Into::into)
170 }
171 }
172 }
173}
174
175#[macro_export]
177macro_rules! wrapping_float {
178 (
179 $( #[$meta:meta] )*
180 $pub:vis struct $name:ident($inner_pub:vis $inner_ty:ty);
181 ) => {
182 wrapping_macro::wrapping!{
183 $( #[$meta] )*
184 $pub struct $name($inner_pub $inner_ty);
185 }
186
187 impl ::core::fmt::Display for $name {
188 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
189 write!(f, "{}", self.0)
190 }
191 }
192
193 impl ::core::str::FromStr for $name {
194 type Err = ::core::num::ParseFloatError;
195
196 fn from_str(s: &::core::primitive::str) -> Result<Self, Self::Err> {
197 s.parse::<$inner_ty>().map(Into::into)
198 }
199 }
200 }
201}