From bf13673b9761af2daccb82b453494038fb218d44 Mon Sep 17 00:00:00 2001 From: Jianfeng Jiang Date: Thu, 5 Feb 2026 07:36:31 +0000 Subject: [PATCH] Replace all usage of `from_bytes` with `from_first_bytes` --- kernel/comps/block/src/partition.rs | 2 +- .../src/layers/1-crypto/crypto_chain.rs | 4 +-- .../comps/mlsdisk/src/layers/3-log/tx_log.rs | 4 +-- .../comps/mlsdisk/src/layers/4-lsm/sstable.rs | 25 ++++++++--------- kernel/comps/mlsdisk/src/layers/4-lsm/wal.rs | 7 ++--- .../mlsdisk/src/layers/5-disk/block_alloc.rs | 2 +- .../comps/virtio/src/device/socket/device.rs | 2 +- kernel/src/fs/exfat/dentry.rs | 27 +++++++++++-------- kernel/src/fs/exfat/inode.rs | 2 +- kernel/src/util/net/addr/family.rs | 6 ++--- ostd/src/arch/x86/kernel/acpi/remapping.rs | 8 +++--- 11 files changed, 48 insertions(+), 41 deletions(-) diff --git a/kernel/comps/block/src/partition.rs b/kernel/comps/block/src/partition.rs index cec09c395..83f365510 100644 --- a/kernel/comps/block/src/partition.rs +++ b/kernel/comps/block/src/partition.rs @@ -229,7 +229,7 @@ fn parse_gpt(device: &Arc) -> Vec> { for j in 0..entries_per_sector { let entry_offset = j * gpt.size_of_partition_entry as usize; - let entry = GptEntry::from_bytes(&buf[entry_offset..entry_offset + entry_size]); + let entry = GptEntry::from_first_bytes(&buf[entry_offset..entry_offset + entry_size]); if entry.is_valid() { partitions.push(Some(PartitionInfo::Gpt(entry))); } else { diff --git a/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_chain.rs b/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_chain.rs index 27ce80f62..ea81a750d 100644 --- a/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_chain.rs +++ b/kernel/comps/mlsdisk/src/layers/1-crypto/crypto_chain.rs @@ -110,7 +110,7 @@ impl CryptoChain { // 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_bytes(&block_buf.as_slice()[Self::AVAIL_BLOCK_SIZE..]); + let footer: Footer = Pod::from_first_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 LendingIterator for Recovery { // Deserialize footer. let footer: Footer = - Pod::from_bytes(&self.read_buf.as_slice()[CryptoChain::::AVAIL_BLOCK_SIZE..]); + Pod::from_first_bytes(&self.read_buf.as_slice()[CryptoChain::::AVAIL_BLOCK_SIZE..]); let payload_len = footer.len as usize; if payload_len > CryptoChain::::AVAIL_BLOCK_SIZE { return None; diff --git a/kernel/comps/mlsdisk/src/layers/3-log/tx_log.rs b/kernel/comps/mlsdisk/src/layers/3-log/tx_log.rs index 211f42c82..c0a6e1fb0 100644 --- a/kernel/comps/mlsdisk/src/layers/3-log/tx_log.rs +++ b/kernel/comps/mlsdisk/src/layers/3-log/tx_log.rs @@ -739,7 +739,7 @@ impl Superblock { plain.as_mut_slice(), )?; - let superblock = Superblock::from_bytes(&plain.as_slice()[..Self::SUPERBLOCK_SIZE]); + let superblock = Superblock::from_first_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_bytes(root_key.as_bytes()) + SkcipherKey::from_first_bytes(root_key.as_bytes()) } } diff --git a/kernel/comps/mlsdisk/src/layers/4-lsm/sstable.rs b/kernel/comps/mlsdisk/src/layers/4-lsm/sstable.rs index f26466781..6d324f3a4 100644 --- a/kernel/comps/mlsdisk/src/layers/4-lsm/sstable.rs +++ b/kernel/comps/mlsdisk/src/layers/4-lsm/sstable.rs @@ -509,7 +509,7 @@ impl, V: RecordValue> SSTable { let mut rbuf = Buf::alloc(1)?; // Load footer block (last block) tx_log.read(nblocks - 1, rbuf.as_mut())?; - let meta = FooterMeta::from_bytes(&rbuf.as_slice()[BLOCK_SIZE - FOOTER_META_SIZE..]); + let meta = FooterMeta::from_first_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,9 +521,10 @@ impl, V: RecordValue> SSTable { &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_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]); + 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], + ); tx_log.read(pos, BufMut::try_from(&mut record_block[..]).unwrap())?; let _ = cache.put(pos, Arc::new(RecordBlock::from_buf(record_block.clone()))); @@ -592,7 +593,7 @@ impl, V: RecordValue> Iterator for BlockQueryIter<'_, K, V> { return None; } - let key = K::from_bytes(&buf_slice[offset..offset + k_size]); + let key = K::from_first_bytes(&buf_slice[offset..offset + k_size]); offset += k_size; let flag = RecordFlag::from(buf_slice[offset]); @@ -605,7 +606,7 @@ impl, 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_bytes(&buf_slice[offset..offset + v_size])) + Some(V::from_first_bytes(&buf_slice[offset..offset + v_size])) } else { None }; @@ -614,7 +615,7 @@ impl, V: RecordValue> Iterator for BlockQueryIter<'_, K, V> { } RecordFlag::SyncedAndUnsynced => { let v_opt = if hit_target { - Some(V::from_bytes( + Some(V::from_first_bytes( &buf_slice[offset + v_size..offset + 2 * v_size], )) } else { @@ -649,7 +650,7 @@ impl, V: RecordValue> Iterator for BlockScanIter<'_, K, V> { return None; } - let key = K::from_bytes(&buf_slice[offset..offset + k_size]); + let key = K::from_first_bytes(&buf_slice[offset..offset + k_size]); offset += k_size; let flag = RecordFlag::from(buf_slice[offset]); @@ -660,12 +661,12 @@ impl, V: RecordValue> Iterator for BlockScanIter<'_, K, V> { let v_ex = match flag { RecordFlag::Synced => { - let v = V::from_bytes(&buf_slice[offset..offset + v_size]); + let v = V::from_first_bytes(&buf_slice[offset..offset + v_size]); offset += v_size; ValueEx::Synced(v) } RecordFlag::Unsynced => { - let v = V::from_bytes(&buf_slice[offset..offset + v_size]); + let v = V::from_first_bytes(&buf_slice[offset..offset + v_size]); offset += v_size; if all_synced { ValueEx::Synced(v) @@ -679,9 +680,9 @@ impl, V: RecordValue> Iterator for BlockScanIter<'_, K, V> { } } RecordFlag::SyncedAndUnsynced => { - let sv = V::from_bytes(&buf_slice[offset..offset + v_size]); + let sv = V::from_first_bytes(&buf_slice[offset..offset + v_size]); offset += v_size; - let usv = V::from_bytes(&buf_slice[offset..offset + v_size]); + let usv = V::from_first_bytes(&buf_slice[offset..offset + v_size]); offset += v_size; if all_synced { if let Some(listener) = event_listener { diff --git a/kernel/comps/mlsdisk/src/layers/4-lsm/wal.rs b/kernel/comps/mlsdisk/src/layers/4-lsm/wal.rs index 65b106d68..49aed1cf3 100644 --- a/kernel/comps/mlsdisk/src/layers/4-lsm/wal.rs +++ b/kernel/comps/mlsdisk/src/layers/4-lsm/wal.rs @@ -188,9 +188,10 @@ impl WalAppendTx { match flag.unwrap() { WalAppendFlag::Record => { let record = { - 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]); + 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], + ); offset += k_size + v_size; (k, v) }; diff --git a/kernel/comps/mlsdisk/src/layers/5-disk/block_alloc.rs b/kernel/comps/mlsdisk/src/layers/5-disk/block_alloc.rs index 9d3da09c8..2471e42ac 100644 --- a/kernel/comps/mlsdisk/src/layers/5-disk/block_alloc.rs +++ b/kernel/comps/mlsdisk/src/layers/5-disk/block_alloc.rs @@ -196,7 +196,7 @@ impl AllocTable { if diff == AllocDiff::Invalid { continue; } - let bid = BlockId::from_bytes(&buf_slice[offset..offset + BID_SIZE]); + let bid = BlockId::from_first_bytes(&buf_slice[offset..offset + BID_SIZE]); offset += BID_SIZE; match diff { AllocDiff::Alloc => bitmap.set(bid, false), diff --git a/kernel/comps/virtio/src/device/socket/device.rs b/kernel/comps/virtio/src/device/socket/device.rs index 963e02842..f3241265f 100644 --- a/kernel/comps/virtio/src/device/socket/device.rs +++ b/kernel/comps/virtio/src/device/socket/device.rs @@ -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::()`. - let header = VirtioVsockHdr::from_bytes(&buffer[..VIRTIO_VSOCK_HDR_LEN]); + let header = VirtioVsockHdr::from_first_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. diff --git a/kernel/src/fs/exfat/dentry.rs b/kernel/src/fs/exfat/dentry.rs index 2d3f0109d..45af89ced 100644 --- a/kernel/src/fs/exfat/dentry.rs +++ b/kernel/src/fs/exfat/dentry.rs @@ -91,36 +91,40 @@ impl TryFrom 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_bytes(dentry_bytes))), - EXFAT_STREAM => Ok(ExfatDentry::Stream(ExfatStreamDentry::from_bytes( + EXFAT_FILE => Ok(ExfatDentry::File(ExfatFileDentry::from_first_bytes( dentry_bytes, ))), - EXFAT_NAME => Ok(ExfatDentry::Name(ExfatNameDentry::from_bytes(dentry_bytes))), - EXFAT_BITMAP => Ok(ExfatDentry::Bitmap(ExfatBitmapDentry::from_bytes( + EXFAT_STREAM => Ok(ExfatDentry::Stream(ExfatStreamDentry::from_first_bytes( dentry_bytes, ))), - EXFAT_UPCASE => Ok(ExfatDentry::Upcase(ExfatUpcaseDentry::from_bytes( + EXFAT_NAME => Ok(ExfatDentry::Name(ExfatNameDentry::from_first_bytes( dentry_bytes, ))), - EXFAT_VENDOR_EXT => Ok(ExfatDentry::VendorExt(ExfatVendorExtDentry::from_bytes( + EXFAT_BITMAP => Ok(ExfatDentry::Bitmap(ExfatBitmapDentry::from_first_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_bytes(dentry_bytes), + ExfatVendorAllocDentry::from_first_bytes(dentry_bytes), )), EXFAT_UNUSED => Ok(ExfatDentry::UnUsed), // Deleted - 0x01..0x80 => Ok(ExfatDentry::Deleted(ExfatDeletedDentry::from_bytes( + 0x01..0x80 => Ok(ExfatDentry::Deleted(ExfatDeletedDentry::from_first_bytes( dentry_bytes, ))), // Primary 0x80..0xC0 => Ok(ExfatDentry::GenericPrimary( - ExfatGenericPrimaryDentry::from_bytes(dentry_bytes), + ExfatGenericPrimaryDentry::from_first_bytes(dentry_bytes), )), // Secondary 0xC0..=0xFF => Ok(ExfatDentry::GenericSecondary( - ExfatGenericSecondaryDentry::from_bytes(dentry_bytes), + ExfatGenericSecondaryDentry::from_first_bytes(dentry_bytes), )), } } @@ -490,7 +494,8 @@ impl Iterator for ExfatDentryIterator<'_> { } // The result is always OK. - let dentry_result = ExfatDentry::try_from(RawExfatDentry::from_bytes(&dentry_buf)).unwrap(); + let dentry_result = + ExfatDentry::try_from(RawExfatDentry::from_first_bytes(&dentry_buf)).unwrap(); self.entry += 1; if let Some(s) = self.size { diff --git a/kernel/src/fs/exfat/inode.rs b/kernel/src/fs/exfat/inode.rs index b7d6e2874..5c609ad1f 100644 --- a/kernel/src/fs/exfat/inode.rs +++ b/kernel/src/fs/exfat/inode.rs @@ -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_bytes( + let dentry = ExfatDentry::try_from(RawExfatDentry::from_first_bytes( &buf[buf_offset..buf_offset + DENTRY_SIZE], ))?; self.inner diff --git a/kernel/src/util/net/addr/family.rs b/kernel/src/util/net/addr/family.rs index a287841a7..b550da1a9 100644 --- a/kernel/src/util/net/addr/family.rs +++ b/kernel/src/util/net/addr/family.rs @@ -152,7 +152,7 @@ pub fn read_socket_addr_from_user(addr: Vaddr, addr_len: usize) -> Result() { return_errno_with_message!(Errno::EINVAL, "the socket address length is too small"); } - let (addr, port) = CSocketAddrInet::from_bytes(storage.as_bytes()).into(); + let (addr, port) = CSocketAddrInet::from_first_bytes(storage.as_bytes()).into(); SocketAddr::IPv4(addr, port) } Ok(CSocketAddrFamily::AF_UNIX) => { @@ -163,14 +163,14 @@ pub fn read_socket_addr_from_user(addr: Vaddr, addr_len: usize) -> Result() { return_errno_with_message!(Errno::EINVAL, "the socket address length is too small"); } - let addr = CSocketAddrNetlink::from_bytes(storage.as_bytes()); + let addr = CSocketAddrNetlink::from_first_bytes(storage.as_bytes()); SocketAddr::Netlink(addr.into()) } Ok(CSocketAddrFamily::AF_VSOCK) => { if addr_len < size_of::() { return_errno_with_message!(Errno::EINVAL, "the socket address length is too small"); } - let addr = CSocketAddrVm::from_bytes(storage.as_bytes()); + let addr = CSocketAddrVm::from_first_bytes(storage.as_bytes()); SocketAddr::Vsock(addr.into()) } _ => { diff --git a/ostd/src/arch/x86/kernel/acpi/remapping.rs b/ostd/src/arch/x86/kernel/acpi/remapping.rs index c7e0f5c77..3dd1a79ed 100644 --- a/ostd/src/arch/x86/kernel/acpi/remapping.rs +++ b/ostd/src/arch/x86/kernel/acpi/remapping.rs @@ -191,7 +191,7 @@ macro_rules! impl_from_bytes { "`].", )] pub fn from_bytes(bytes: &[u8]) -> Self { - let header = $header_struct::from_bytes(bytes); + let header = $header_struct::from_first_bytes(bytes); debug_assert_eq!(header.length as usize, bytes.len()); let mut index = size_of::<$header_struct>(); @@ -226,7 +226,7 @@ impl DeviceScope { /// /// This method may panic if the byte prefix does not represent a valid [`DeviceScope`]. fn from_bytes_prefix(bytes: &[u8]) -> Self { - let header = DeviceScopeHeader::from_bytes(bytes); + let header = DeviceScopeHeader::from_first_bytes(bytes); debug_assert!((header.length as usize) <= bytes.len()); let mut index = size_of::(); @@ -250,7 +250,7 @@ impl Rhsa { /// /// This method may panic if the bytes do not represent a valid [`Rhsa`]. pub fn from_bytes(bytes: &[u8]) -> Self { - let val = ::from_bytes(bytes); + let val = ::from_first_bytes(bytes); debug_assert_eq!(val.length as usize, bytes.len()); val @@ -264,7 +264,7 @@ impl Andd { /// /// This method may panic if the bytes do not represent a valid [`Andd`]. pub fn from_bytes(bytes: &[u8]) -> Self { - let header = AnddHeader::from_bytes(bytes); + let header = AnddHeader::from_first_bytes(bytes); debug_assert_eq!(header.length as usize, bytes.len()); let header_len = size_of::();