Uses try_into for slice to array conversions

pull/284/head
Tpt 2 years ago committed by Thomas Tanon
parent 9dc8dce69c
commit 59aea75a30
  1. 6
      lib/src/storage/mod.rs
  2. 13
      lib/src/xsd/date_time.rs
  3. 8
      lib/src/xsd/duration.rs

@ -215,9 +215,9 @@ impl Storage {
fn ensure_version(&self) -> Result<u64, StorageError> {
Ok(
if let Some(version) = self.db.get(&self.default_cf, b"oxversion")? {
let mut buffer = [0; 8];
buffer.copy_from_slice(&version);
u64::from_be_bytes(buffer)
u64::from_be_bytes(version.as_ref().try_into().map_err(|e| {
CorruptionError::new(format!("Error while parsing the version key: {}", e))
})?)
} else {
self.update_version(LATEST_STORAGE_VERSION)?;
LATEST_STORAGE_VERSION

@ -1181,17 +1181,14 @@ impl Timestamp {
}
fn from_be_bytes(bytes: [u8; 18]) -> Self {
let mut value = [0; 16];
value.copy_from_slice(&bytes[0..16]);
let mut timezone_offset = [0; 2];
timezone_offset.copy_from_slice(&bytes[16..18]);
Self {
value: Decimal::from_be_bytes(value),
timezone_offset: if timezone_offset == [u8::MAX; 2] {
value: Decimal::from_be_bytes(bytes[0..16].try_into().unwrap()),
timezone_offset: if bytes[16..18] == [u8::MAX; 2] {
None
} else {
Some(TimezoneOffset::from_be_bytes(timezone_offset))
Some(TimezoneOffset::from_be_bytes(
bytes[16..18].try_into().unwrap(),
))
},
}
}

@ -25,13 +25,9 @@ impl Duration {
}
pub fn from_be_bytes(bytes: [u8; 24]) -> Self {
let mut months = [0; 8];
months.copy_from_slice(&bytes[0..8]);
let mut seconds = [8; 16];
seconds.copy_from_slice(&bytes[8..24]);
Self {
year_month: YearMonthDuration::from_be_bytes(months),
day_time: DayTimeDuration::from_be_bytes(seconds),
year_month: YearMonthDuration::from_be_bytes(bytes[0..8].try_into().unwrap()),
day_time: DayTimeDuration::from_be_bytes(bytes[8..24].try_into().unwrap()),
}
}

Loading…
Cancel
Save