Skip to content

Commit b19f121

Browse files
committed
Fix MSRV
1 parent e5c7bcf commit b19f121

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Diff for: src/offset/local/tzdata.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
ffi::CStr,
66
fmt::Debug,
77
fs::File,
8-
io::{Error, Read, Result, Seek, SeekFrom},
8+
io::{Error, ErrorKind, Read, Result, Seek, SeekFrom},
99
};
1010

1111
/// Get timezone data from the `tzdata` file of HarmonyOS NEXT.
@@ -34,7 +34,7 @@ fn open_android_tz_data_file() -> Result<File> {
3434
}
3535
}
3636
}
37-
Err(std::io::Error::from(std::io::ErrorKind::NotFound))
37+
Err(Error::from(ErrorKind::NotFound))
3838
}
3939

4040
/// Get timezone data from the `tzdata` file reader of HarmonyOS NEXT.
@@ -77,7 +77,7 @@ impl TzDataHeader {
7777
let mut magic = [0; TZDATA_VERSION_SIZE];
7878
data.read_exact(&mut magic)?;
7979
if !magic.starts_with(b"tzdata") || magic[TZDATA_VERSION_SIZE - 1] != 0 {
80-
return Err(Error::other("invalid tzdata header magic"));
80+
return Err(Error::new(ErrorKind::Other, "invalid tzdata header magic"));
8181
}
8282
let mut version = [0; 5];
8383
version.copy_from_slice(&magic[6..11]);
@@ -118,7 +118,7 @@ impl TzDataIndexes {
118118
indexes: buf
119119
.chunks(INDEX_SIZE)
120120
.filter_map(|chunk| {
121-
if let Ok(name) = CStr::from_bytes_until_nul(&chunk[..SIZEOF_TZNAME]) {
121+
if let Some(name) = from_bytes_until_nul(&chunk[..SIZEOF_TZNAME]) {
122122
let name = name.to_bytes().to_vec().into_boxed_slice();
123123
let offset = u32::from_be_bytes(
124124
chunk[SIZEOF_TZNAME..SIZEOF_TZNAME + 4].try_into().unwrap(),
@@ -155,6 +155,15 @@ impl TzDataIndexes {
155155
}
156156
}
157157

158+
/// Change this `CStr::from_bytes_until_nul` once MSRV was bumped above 1.72.0
159+
fn from_bytes_until_nul(bytes: &[u8]) -> Option<&CStr> {
160+
let nul_pos = bytes.iter().position(|&b| b == 0)?;
161+
// SAFETY:
162+
// 1. nul_pos + 1 <= bytes.len()
163+
// 2. We know there is a nul byte at nul_pos, so this slice (ending at the nul byte) is a well-formed C string.
164+
Some(unsafe { CStr::from_bytes_with_nul_unchecked(&bytes[0..=nul_pos]) })
165+
}
166+
158167
/// Ohos tzdata index entry size: `name + offset + length`
159168
#[cfg(any(test, target_env = "ohos"))]
160169
const SIZEOF_INDEX_OHOS: usize = SIZEOF_TZNAME + 2 * size_of::<u32>();

0 commit comments

Comments
 (0)