Skip to content

Commit e3a2120

Browse files
committed
Fix no_std issues
1 parent 189da0d commit e3a2120

File tree

1 file changed

+6
-35
lines changed

1 file changed

+6
-35
lines changed

src/bytearray.rs

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use core::ops::{Deref, DerefMut};
99
use serde::de::{Deserialize, Deserializer, Error, SeqAccess, Visitor};
1010
use serde::ser::{Serialize, Serializer};
1111

12+
const ERROR_STRING: &'static str = "Expected a fixed amount of bytes, got a different amount";
13+
1214
/// Wrapper around `[u8; N]` to serialize and deserialize efficiently.
1315
///
1416
/// ```
@@ -189,21 +191,15 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor<N> {
189191
let mut idx = 0;
190192
while let Some(b) = visitor.next_element()? {
191193
if idx >= N {
192-
return Err(V::Error::custom(format!(
193-
"Expected [u8; {0}], got more than {0} elements",
194-
N
195-
)));
194+
return Err(V::Error::custom(ERROR_STRING));
196195
}
197196

198197
bytes[idx] = b;
199198
idx += 1;
200199
}
201200

202201
if idx != N {
203-
return Err(V::Error::custom(format!(
204-
"Expected [u8; {}], got [u8; {}]",
205-
N, idx
206-
)));
202+
return Err(V::Error::custom(ERROR_STRING));
207203
}
208204

209205
Ok(ByteArray::from(bytes))
@@ -214,20 +210,7 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor<N> {
214210
E: Error,
215211
{
216212
Ok(ByteArray {
217-
bytes: v
218-
.try_into()
219-
.map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?,
220-
})
221-
}
222-
223-
fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<ByteArray<N>, E>
224-
where
225-
E: Error,
226-
{
227-
Ok(ByteArray {
228-
bytes: v
229-
.try_into()
230-
.map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?,
213+
bytes: v.try_into().map_err(|_| E::custom(ERROR_STRING))?,
231214
})
232215
}
233216

@@ -239,19 +222,7 @@ impl<'de, const N: usize> Visitor<'de> for ByteArrayVisitor<N> {
239222
bytes: v
240223
.as_bytes()
241224
.try_into()
242-
.map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?,
243-
})
244-
}
245-
246-
fn visit_string<E>(self, v: String) -> Result<ByteArray<N>, E>
247-
where
248-
E: Error,
249-
{
250-
Ok(ByteArray {
251-
bytes: v
252-
.as_bytes()
253-
.try_into()
254-
.map_err(|_| E::custom(format!("Expected [u8; {}]", N)))?,
225+
.map_err(|_| E::custom(ERROR_STRING))?,
255226
})
256227
}
257228
}

0 commit comments

Comments
 (0)