Skip to content

Commit 151345f

Browse files
authored
use try_into with entire byte slice (#158)
1 parent 169ca8b commit 151345f

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

rust/src/lib.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern crate quickcheck;
1414
extern crate quickcheck_macros;
1515
extern crate hex;
1616

17+
use std::convert::TryInto;
1718
use std::io::{BufRead, Seek, Write};
1819

1920
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
@@ -858,15 +859,15 @@ impl Ipv4 {
858859
Self::new_impl(data).map_err(|e| JsError::from_str(&e.to_string()))
859860
}
860861

861-
pub (crate) fn new_impl(data: Vec<u8>) -> Result<Ipv4, DeserializeError> {
862-
use std::convert::TryInto;
863-
data[..4]
864-
.try_into()
865-
.map(Self)
866-
.map_err(|_e| {
867-
let cbor_error = cbor_event::Error::WrongLen(4, cbor_event::Len::Len(data.len() as u64), "Ipv4 address length");
868-
DeserializeError::new("Ipv4", DeserializeFailure::CBOR(cbor_error))
869-
})
862+
pub(crate) fn new_impl(data: Vec<u8>) -> Result<Ipv4, DeserializeError> {
863+
data.as_slice().try_into().map(Self).map_err(|_e| {
864+
let cbor_error = cbor_event::Error::WrongLen(
865+
4,
866+
cbor_event::Len::Len(data.len() as u64),
867+
"Ipv4 address length",
868+
);
869+
DeserializeError::new("Ipv4", DeserializeFailure::CBOR(cbor_error))
870+
})
870871
}
871872

872873
pub fn ip(&self) -> Vec<u8> {
@@ -886,15 +887,15 @@ impl Ipv6 {
886887
Self::new_impl(data).map_err(|e| JsError::from_str(&e.to_string()))
887888
}
888889

889-
pub (crate) fn new_impl(data: Vec<u8>) -> Result<Ipv6, DeserializeError> {
890-
use std::convert::TryInto;
891-
data[..16]
892-
.try_into()
893-
.map(Self)
894-
.map_err(|_e| {
895-
let cbor_error = cbor_event::Error::WrongLen(16, cbor_event::Len::Len(data.len() as u64), "Ipv6 address length");
896-
DeserializeError::new("Ipv6", DeserializeFailure::CBOR(cbor_error))
897-
})
890+
pub(crate) fn new_impl(data: Vec<u8>) -> Result<Ipv6, DeserializeError> {
891+
data.as_slice().try_into().map(Self).map_err(|_e| {
892+
let cbor_error = cbor_event::Error::WrongLen(
893+
16,
894+
cbor_event::Len::Len(data.len() as u64),
895+
"Ipv6 address length",
896+
);
897+
DeserializeError::new("Ipv6", DeserializeFailure::CBOR(cbor_error))
898+
})
898899
}
899900

900901
pub fn ip(&self) -> Vec<u8> {

0 commit comments

Comments
 (0)