Skip to content

Commit e3b373a

Browse files
authored
Use some APIs first available in Rust 1.93 (#13188)
Some minor clean-ups/adjustments in a few location using niceties added in 1.93.
1 parent 3ce7fdb commit e3b373a

7 files changed

Lines changed: 43 additions & 50 deletions

File tree

crates/environ/src/graphs/entity_graph.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,18 @@ where
2424
Node: EntityRef + Debug,
2525
{
2626
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27-
struct Edges<'a, Node: EntityRef + Debug>(&'a EntityGraph<Node>);
28-
29-
impl<'a, Node: EntityRef + Debug> Debug for Edges<'a, Node> {
30-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31-
f.debug_map()
32-
.entries(
33-
self.0
34-
.nodes()
35-
.map(|n| (n, self.0.successors(n).collect::<Box<[_]>>())),
36-
)
37-
.finish()
38-
}
39-
}
40-
4127
f.debug_struct("Graph")
42-
.field("edges", &Edges(self))
28+
.field(
29+
"edges",
30+
&fmt::from_fn(|f| {
31+
f.debug_map()
32+
.entries(
33+
self.nodes()
34+
.map(|n| (n, self.successors(n).collect::<Box<[_]>>())),
35+
)
36+
.finish()
37+
}),
38+
)
4339
.finish()
4440
}
4541
}

crates/fiber/src/stackswitch/aarch64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) unsafe extern "C" fn wasmtime_fiber_switch(top_of_stack: *mut u8) {
1717

1818
#[unsafe(naked)]
1919
unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* x0 */) {
20-
naked_asm!(concat!(
20+
naked_asm!(
2121
"
2222
.cfi_startproc
2323
// Save all callee-saved registers on the stack since we're
@@ -57,7 +57,7 @@ unsafe extern "C" fn wasmtime_fiber_switch_(top_of_stack: *mut u8 /* x0 */) {
5757
ret
5858
.cfi_endproc
5959
",
60-
));
60+
);
6161
}
6262

6363
pub(crate) unsafe fn wasmtime_fiber_init(

crates/wasmtime/src/compile/stratify.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,17 @@ pub struct Strata<Node> {
6868

6969
impl<Node: Debug> Debug for Strata<Node> {
7070
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71-
struct Layers<'a, Node>(&'a Strata<Node>);
72-
73-
impl<'a, Node: Debug> Debug for Layers<'a, Node> {
74-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75-
let mut f = f.debug_list();
76-
for layer in self.0.layers() {
77-
f.entry(&layer);
78-
}
79-
f.finish()
80-
}
81-
}
82-
8371
f.debug_struct("Strata")
84-
.field("layers", &Layers(self))
72+
.field(
73+
"layers",
74+
&core::fmt::from_fn(|f| {
75+
let mut f = f.debug_list();
76+
for layer in self.layers() {
77+
f.entry(&layer);
78+
}
79+
f.finish()
80+
}),
81+
)
8582
.finish()
8683
}
8784
}

crates/wasmtime/src/runtime/component/func/typed.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ macro_rules! integers {
11571157
fn linear_lift_from_memory(_cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {
11581158
debug_assert!(matches!(ty, InterfaceType::$ty));
11591159
debug_assert!((bytes.as_ptr() as usize) % Self::SIZE32 == 0);
1160-
Ok($primitive::from_le_bytes(bytes.try_into().unwrap()))
1160+
Ok($primitive::from_le_bytes(*bytes.as_array().unwrap()))
11611161
}
11621162

11631163
fn linear_lift_into_from_memory(
@@ -1257,8 +1257,9 @@ macro_rules! floats {
12571257
// into a memcpy on little-endian platforms.
12581258
// TODO use `as_chunks` when https://github.com/rust-lang/rust/issues/74985
12591259
// is stabilized
1260-
for (dst, src) in iter::zip(dst.chunks_exact_mut(Self::SIZE32), items) {
1261-
let dst: &mut [u8; Self::SIZE32] = dst.try_into().unwrap();
1260+
let (dst, rest) = dst.as_chunks_mut::<{Self::SIZE32}>();
1261+
debug_assert!(rest.is_empty());
1262+
for (dst, src) in iter::zip(dst, items) {
12621263
*dst = src.to_le_bytes();
12631264
}
12641265
Ok(())
@@ -1276,7 +1277,7 @@ macro_rules! floats {
12761277
fn linear_lift_from_memory(_cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {
12771278
debug_assert!(matches!(ty, InterfaceType::$ty));
12781279
debug_assert!((bytes.as_ptr() as usize) % Self::SIZE32 == 0);
1279-
Ok($float::from_le_bytes(bytes.try_into().unwrap()))
1280+
Ok($float::from_le_bytes(*bytes.as_array().unwrap()))
12801281
}
12811282

12821283
fn linear_lift_list_from_memory(cx: &mut LiftContext<'_>, list: &WasmList<Self>) -> Result<Vec<Self>> where Self: Sized {
@@ -1295,7 +1296,7 @@ macro_rules! floats {
12951296
Ok(
12961297
bytes
12971298
.chunks_exact(Self::SIZE32)
1298-
.map(|i| $float::from_le_bytes(i.try_into().unwrap()))
1299+
.map(|i| $float::from_le_bytes(*i.as_array().unwrap()))
12991300
.collect()
13001301
)
13011302
}
@@ -1435,7 +1436,7 @@ unsafe impl Lift for char {
14351436
) -> Result<Self> {
14361437
debug_assert!(matches!(ty, InterfaceType::Char));
14371438
debug_assert!((bytes.as_ptr() as usize) % Self::SIZE32 == 0);
1438-
let bits = u32::from_le_bytes(bytes.try_into().unwrap());
1439+
let bits = u32::from_le_bytes(*bytes.as_array().unwrap());
14391440
Ok(char::try_from(bits)?)
14401441
}
14411442
}
@@ -1454,8 +1455,8 @@ fn lift_pointer_pair_from_flat(
14541455
fn lift_pointer_pair_from_memory(cx: &mut LiftContext<'_>, bytes: &[u8]) -> Result<(usize, usize)> {
14551456
// FIXME(#4311): needs memory64 treatment
14561457
let _ = cx; // this will be needed for memory64 in the future
1457-
let ptr = u32::from_le_bytes(bytes[..4].try_into().unwrap());
1458-
let len = u32::from_le_bytes(bytes[4..].try_into().unwrap());
1458+
let ptr = u32::from_le_bytes(*bytes[..4].as_array().unwrap());
1459+
let len = u32::from_le_bytes(*bytes[4..].as_array().unwrap());
14591460
Ok((usize::try_from(ptr)?, usize::try_from(len)?))
14601461
}
14611462

@@ -1769,14 +1770,13 @@ impl WasmStr {
17691770

17701771
fn decode_utf16<'a>(&self, memory: &'a [u8], len: usize) -> Result<Cow<'a, str>> {
17711772
// See notes in `decode_utf8` for why this is panicking indexing.
1772-
let memory = &memory[self.ptr..][..len * 2];
1773-
Ok(core::char::decode_utf16(
1774-
memory
1775-
.chunks(2)
1776-
.map(|chunk| u16::from_le_bytes(chunk.try_into().unwrap())),
1773+
let (chunks, rest) = &memory[self.ptr..][..len * 2].as_chunks::<2>();
1774+
debug_assert!(rest.is_empty());
1775+
Ok(
1776+
core::char::decode_utf16(chunks.iter().map(|chunk| u16::from_le_bytes(*chunk)))
1777+
.collect::<Result<String, _>>()?
1778+
.into(),
17771779
)
1778-
.collect::<Result<String, _>>()?
1779-
.into())
17801780
}
17811781

17821782
fn decode_latin1<'a>(&self, memory: &'a [u8]) -> Result<Cow<'a, str>> {

crates/wasmtime/src/runtime/component/values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -947,8 +947,8 @@ fn lift_flat_pointer_pair(
947947
}
948948

949949
fn load_flat_pointer_pair(bytes: &[u8]) -> (usize, usize) {
950-
let ptr = u32::from_le_bytes(bytes[..4].try_into().unwrap()) as usize;
951-
let len = u32::from_le_bytes(bytes[4..].try_into().unwrap()) as usize;
950+
let ptr = u32::from_le_bytes(*bytes[..4].as_array().unwrap()) as usize;
951+
let len = u32::from_le_bytes(*bytes[4..].as_array().unwrap()) as usize;
952952
(ptr, len)
953953
}
954954

crates/wasmtime/src/runtime/vm/gc/enabled/data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl VMGcObjectData {
135135
let offset = usize::try_from(offset).unwrap();
136136
let end = offset.checked_add(N).unwrap();
137137
let bytes = self.data.get(offset..end).expect("out of bounds field");
138-
T::read_le(bytes.try_into().unwrap())
138+
T::read_le(bytes.as_array().unwrap())
139139
}
140140

141141
/// Read a POD field out of this object.
@@ -159,7 +159,7 @@ impl VMGcObjectData {
159159
self.data.as_mut().len(),
160160
),
161161
};
162-
val.write_le(into.try_into().unwrap());
162+
val.write_le(into.as_mut_array().unwrap());
163163
}
164164

165165
/// Get a slice of this object's data.

crates/wasmtime/src/runtime/vm/gc/enabled/drc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl DrcHeap {
324324
debug_assert!(
325325
field_end <= object_start + usize::try_from(object_size).unwrap()
326326
);
327-
let raw: [u8; 4] = heap[field_start..field_end].try_into().unwrap();
327+
let raw = *heap[field_start..field_end].as_array().unwrap();
328328
let raw = u32::from_le_bytes(raw);
329329

330330
if let Some(child) = VMGcRef::from_raw_u32(raw)

0 commit comments

Comments
 (0)