Replace `from_first_bytes` with `from_bytes` if possible

This commit is contained in:
Jianfeng Jiang 2026-02-05 11:15:15 +00:00
parent bb37d5fbf7
commit ca6ac52bd7
9 changed files with 40 additions and 40 deletions

View File

@ -110,7 +110,7 @@ impl<L: BlockLog> CryptoChain<L> {
// Read block and get footer.
let mut block_buf = Buf::alloc(1)?;
self.block_log.read(pos, block_buf.as_mut())?;
let footer: Footer = Pod::from_first_bytes(&block_buf.as_slice()[Self::AVAIL_BLOCK_SIZE..]);
let footer: Footer = Pod::from_bytes(&block_buf.as_slice()[Self::AVAIL_BLOCK_SIZE..]);
let payload_len = footer.len as usize;
if payload_len > Self::AVAIL_BLOCK_SIZE || payload_len > buf.len() {
@ -290,7 +290,7 @@ impl<L: BlockLog> LendingIterator for Recovery<L> {
// Deserialize footer.
let footer: Footer =
Pod::from_first_bytes(&self.read_buf.as_slice()[CryptoChain::<L>::AVAIL_BLOCK_SIZE..]);
Pod::from_bytes(&self.read_buf.as_slice()[CryptoChain::<L>::AVAIL_BLOCK_SIZE..]);
let payload_len = footer.len as usize;
if payload_len > CryptoChain::<L>::AVAIL_BLOCK_SIZE {
return None;

View File

@ -739,7 +739,7 @@ impl Superblock {
plain.as_mut_slice(),
)?;
let superblock = Superblock::from_first_bytes(&plain.as_slice()[..Self::SUPERBLOCK_SIZE]);
let superblock = Superblock::from_bytes(&plain.as_slice()[..Self::SUPERBLOCK_SIZE]);
if superblock.magic != MAGIC_NUMBER {
Err(Error::with_msg(InvalidArgs, "open superblock failed"))
} else {
@ -762,7 +762,7 @@ impl Superblock {
}
fn derive_skcipher_key(root_key: &Key) -> SkcipherKey {
SkcipherKey::from_first_bytes(root_key.as_bytes())
SkcipherKey::from_bytes(root_key.as_bytes())
}
}

View File

@ -509,7 +509,7 @@ impl<K: RecordKey<K>, V: RecordValue> SSTable<K, V> {
let mut rbuf = Buf::alloc(1)?;
// Load footer block (last block)
tx_log.read(nblocks - 1, rbuf.as_mut())?;
let meta = FooterMeta::from_first_bytes(&rbuf.as_slice()[BLOCK_SIZE - FOOTER_META_SIZE..]);
let meta = FooterMeta::from_bytes(&rbuf.as_slice()[BLOCK_SIZE - FOOTER_META_SIZE..]);
let mut rbuf = Buf::alloc(meta.index_nblocks as _)?;
tx_log.read(nblocks - meta.index_nblocks as usize, rbuf.as_mut())?;
@ -521,10 +521,9 @@ impl<K: RecordKey<K>, V: RecordValue> SSTable<K, V> {
&rbuf.as_slice()[i * Self::INDEX_ENTRY_SIZE..(i + 1) * Self::INDEX_ENTRY_SIZE];
let pos = BlockId::from_le_bytes(buf[..BID_SIZE].try_into().unwrap());
let first = K::from_first_bytes(&buf[BID_SIZE..BID_SIZE + Self::K_SIZE]);
let last = K::from_first_bytes(
&buf[Self::INDEX_ENTRY_SIZE - Self::K_SIZE..Self::INDEX_ENTRY_SIZE],
);
let first = K::from_bytes(&buf[BID_SIZE..BID_SIZE + Self::K_SIZE]);
let last =
K::from_bytes(&buf[Self::INDEX_ENTRY_SIZE - Self::K_SIZE..Self::INDEX_ENTRY_SIZE]);
tx_log.read(pos, BufMut::try_from(&mut record_block[..]).unwrap())?;
let _ = cache.put(pos, Arc::new(RecordBlock::from_buf(record_block.clone())));
@ -593,7 +592,7 @@ impl<K: RecordKey<K>, V: RecordValue> Iterator for BlockQueryIter<'_, K, V> {
return None;
}
let key = K::from_first_bytes(&buf_slice[offset..offset + k_size]);
let key = K::from_bytes(&buf_slice[offset..offset + k_size]);
offset += k_size;
let flag = RecordFlag::from(buf_slice[offset]);
@ -606,7 +605,7 @@ impl<K: RecordKey<K>, V: RecordValue> Iterator for BlockQueryIter<'_, K, V> {
let value_opt = match flag {
RecordFlag::Synced | RecordFlag::Unsynced => {
let v_opt = if hit_target {
Some(V::from_first_bytes(&buf_slice[offset..offset + v_size]))
Some(V::from_bytes(&buf_slice[offset..offset + v_size]))
} else {
None
};
@ -615,7 +614,7 @@ impl<K: RecordKey<K>, V: RecordValue> Iterator for BlockQueryIter<'_, K, V> {
}
RecordFlag::SyncedAndUnsynced => {
let v_opt = if hit_target {
Some(V::from_first_bytes(
Some(V::from_bytes(
&buf_slice[offset + v_size..offset + 2 * v_size],
))
} else {
@ -650,7 +649,7 @@ impl<K: RecordKey<K>, V: RecordValue> Iterator for BlockScanIter<'_, K, V> {
return None;
}
let key = K::from_first_bytes(&buf_slice[offset..offset + k_size]);
let key = K::from_bytes(&buf_slice[offset..offset + k_size]);
offset += k_size;
let flag = RecordFlag::from(buf_slice[offset]);
@ -661,12 +660,12 @@ impl<K: RecordKey<K>, V: RecordValue> Iterator for BlockScanIter<'_, K, V> {
let v_ex = match flag {
RecordFlag::Synced => {
let v = V::from_first_bytes(&buf_slice[offset..offset + v_size]);
let v = V::from_bytes(&buf_slice[offset..offset + v_size]);
offset += v_size;
ValueEx::Synced(v)
}
RecordFlag::Unsynced => {
let v = V::from_first_bytes(&buf_slice[offset..offset + v_size]);
let v = V::from_bytes(&buf_slice[offset..offset + v_size]);
offset += v_size;
if all_synced {
ValueEx::Synced(v)
@ -680,9 +679,9 @@ impl<K: RecordKey<K>, V: RecordValue> Iterator for BlockScanIter<'_, K, V> {
}
}
RecordFlag::SyncedAndUnsynced => {
let sv = V::from_first_bytes(&buf_slice[offset..offset + v_size]);
let sv = V::from_bytes(&buf_slice[offset..offset + v_size]);
offset += v_size;
let usv = V::from_first_bytes(&buf_slice[offset..offset + v_size]);
let usv = V::from_bytes(&buf_slice[offset..offset + v_size]);
offset += v_size;
if all_synced {
if let Some(listener) = event_listener {

View File

@ -188,10 +188,9 @@ impl<D: BlockSet + 'static> WalAppendTx<D> {
match flag.unwrap() {
WalAppendFlag::Record => {
let record = {
let k = K::from_first_bytes(&buf_slice[offset..offset + k_size]);
let v = V::from_first_bytes(
&buf_slice[offset + k_size..offset + k_size + v_size],
);
let k = K::from_bytes(&buf_slice[offset..offset + k_size]);
let v =
V::from_bytes(&buf_slice[offset + k_size..offset + k_size + v_size]);
offset += k_size + v_size;
(k, v)
};

View File

@ -196,7 +196,7 @@ impl AllocTable {
if diff == AllocDiff::Invalid {
continue;
}
let bid = BlockId::from_first_bytes(&buf_slice[offset..offset + BID_SIZE]);
let bid = BlockId::from_bytes(&buf_slice[offset..offset + BID_SIZE]);
offset += BID_SIZE;
match diff {
AllocDiff::Alloc => bitmap.set(bid, false),

View File

@ -338,7 +338,7 @@ impl Debug for SocketDevice {
fn read_header_and_body(buffer: &[u8]) -> Result<(VirtioVsockHdr, &[u8]), SocketError> {
// Shouldn't panic, because we know `RX_BUFFER_SIZE > size_of::<VirtioVsockHdr>()`.
let header = VirtioVsockHdr::from_first_bytes(&buffer[..VIRTIO_VSOCK_HDR_LEN]);
let header = VirtioVsockHdr::from_bytes(&buffer[..VIRTIO_VSOCK_HDR_LEN]);
let body_length = header.len() as usize;
// This could fail if the device returns an unreasonably long body length.

View File

@ -91,40 +91,36 @@ impl TryFrom<RawExfatDentry> for ExfatDentry {
#[expect(clippy::match_overlapping_arm)]
// FIXME: `EXFAT_STREAM` and `0xC0..=0xFF` overlap. Is the overlapping case expected?
match dentry.dentry_type {
EXFAT_FILE => Ok(ExfatDentry::File(ExfatFileDentry::from_first_bytes(
EXFAT_FILE => Ok(ExfatDentry::File(ExfatFileDentry::from_bytes(dentry_bytes))),
EXFAT_STREAM => Ok(ExfatDentry::Stream(ExfatStreamDentry::from_bytes(
dentry_bytes,
))),
EXFAT_STREAM => Ok(ExfatDentry::Stream(ExfatStreamDentry::from_first_bytes(
EXFAT_NAME => Ok(ExfatDentry::Name(ExfatNameDentry::from_bytes(dentry_bytes))),
EXFAT_BITMAP => Ok(ExfatDentry::Bitmap(ExfatBitmapDentry::from_bytes(
dentry_bytes,
))),
EXFAT_NAME => Ok(ExfatDentry::Name(ExfatNameDentry::from_first_bytes(
EXFAT_UPCASE => Ok(ExfatDentry::Upcase(ExfatUpcaseDentry::from_bytes(
dentry_bytes,
))),
EXFAT_BITMAP => Ok(ExfatDentry::Bitmap(ExfatBitmapDentry::from_first_bytes(
EXFAT_VENDOR_EXT => Ok(ExfatDentry::VendorExt(ExfatVendorExtDentry::from_bytes(
dentry_bytes,
))),
EXFAT_UPCASE => Ok(ExfatDentry::Upcase(ExfatUpcaseDentry::from_first_bytes(
dentry_bytes,
))),
EXFAT_VENDOR_EXT => Ok(ExfatDentry::VendorExt(
ExfatVendorExtDentry::from_first_bytes(dentry_bytes),
)),
EXFAT_VENDOR_ALLOC => Ok(ExfatDentry::VendorAlloc(
ExfatVendorAllocDentry::from_first_bytes(dentry_bytes),
ExfatVendorAllocDentry::from_bytes(dentry_bytes),
)),
EXFAT_UNUSED => Ok(ExfatDentry::UnUsed),
// Deleted
0x01..0x80 => Ok(ExfatDentry::Deleted(ExfatDeletedDentry::from_first_bytes(
0x01..0x80 => Ok(ExfatDentry::Deleted(ExfatDeletedDentry::from_bytes(
dentry_bytes,
))),
// Primary
0x80..0xC0 => Ok(ExfatDentry::GenericPrimary(
ExfatGenericPrimaryDentry::from_first_bytes(dentry_bytes),
ExfatGenericPrimaryDentry::from_bytes(dentry_bytes),
)),
// Secondary
0xC0..=0xFF => Ok(ExfatDentry::GenericSecondary(
ExfatGenericSecondaryDentry::from_first_bytes(dentry_bytes),
ExfatGenericSecondaryDentry::from_bytes(dentry_bytes),
)),
}
}
@ -494,8 +490,7 @@ impl Iterator for ExfatDentryIterator<'_> {
}
// The result is always OK.
let dentry_result =
ExfatDentry::try_from(RawExfatDentry::from_first_bytes(&dentry_buf)).unwrap();
let dentry_result = ExfatDentry::try_from(RawExfatDentry::from_bytes(&dentry_buf)).unwrap();
self.entry += 1;
if let Some(s) = self.size {

View File

@ -1147,7 +1147,7 @@ impl ExfatInode {
for i in 0..num_dentry {
let buf_offset = DENTRY_SIZE * i;
// Delete cluster chain if needed.
let dentry = ExfatDentry::try_from(RawExfatDentry::from_first_bytes(
let dentry = ExfatDentry::try_from(RawExfatDentry::from_bytes(
&buf[buf_offset..buf_offset + DENTRY_SIZE],
))?;
self.inner

View File

@ -1,5 +1,12 @@
// SPDX-License-Identifier: MPL-2.0
//! Aligned array helpers for Pod types.
//!
//! This module provides type-level utilities
//! for creating arrays with specific alignment requirements.
//! It's primarily used internally to support Pod unions
//! that need to maintain precise memory layouts with guaranteed alignment.
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};
/// A transparent wrapper around `[u8; N]` with guaranteed 1-byte alignment.