A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Add support for lowering directly from `Bytes`. (#13366)
Dan Gohman31931c2bde20
diff --git a/.github/workflows/main.yml b/.github/workflows/main.ymlindex 3102d9e25a..aff0052edd 100644--- a/.github/workflows/main.yml+++ b/.github/workflows/main.yml@@ -1289,7 +1289,7 @@ jobs:strategy:matrix:include:- - crate: "wasmtime --features component-model-async-bytes"+ - crate: "wasmtime --features component-model-bytes,component-model-async"- crate: "wasmtime-cli"- crate: "wasmtime-environ --all-features"- crate: "pulley-interpreter --all-features"diff --git a/Cargo.toml b/Cargo.tomlindex a2d54abc4e..f1ca40f175 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -97,8 +97,6 @@ pin-project-lite = { workspace = true, optional = true }rustix = { workspace = true, features = ["mm", "process"] }[dev-dependencies]-# depend again on wasmtime to activate its default features for tests-wasmtime = { workspace = true, features = ['default', 'anyhow', 'winch', 'pulley', 'all-arch', 'call-hook', 'memory-protection-keys', 'component-model-async', 'component-model-async-bytes', 'gc-copying'] }env_logger = { workspace = true }log = { workspace = true }filecheck = { workspace = true }@@ -142,6 +140,22 @@ rand = { workspace = true }# `wasmtime::Error`.anyhow-for-testing = { workspace = true }+# depend again on wasmtime to activate its default features for tests+[dev-dependencies.wasmtime]+workspace = true+features = [+ 'default',+ 'anyhow',+ 'winch',+ 'pulley',+ 'all-arch',+ 'call-hook',+ 'memory-protection-keys',+ 'component-model-async',+ 'component-model-bytes',+ 'gc-copying'+]+[target.'cfg(windows)'.dev-dependencies]windows-sys = { workspace = true, features = ["Win32_System_Memory"] }diff --git a/crates/wasi/Cargo.toml b/crates/wasi/Cargo.tomlindex 829a330794..29baebac6b 100644--- a/crates/wasi/Cargo.toml+++ b/crates/wasi/Cargo.toml@@ -72,7 +72,7 @@ p1 = ["dep:wiggle", "p2"]p2 = ["wasmtime/component-model", "wasmtime/async"]p3 = ["wasmtime/component-model-async",- "wasmtime/component-model-async-bytes",+ "wasmtime/component-model-bytes",][[test]]diff --git a/crates/wasmtime/Cargo.toml b/crates/wasmtime/Cargo.tomlindex f8768fd53f..b56304c229 100644--- a/crates/wasmtime/Cargo.toml+++ b/crates/wasmtime/Cargo.toml@@ -434,9 +434,10 @@ component-model-async = ["dep:futures",]-# Enables support for `stream` interop with the `bytes` crate.-component-model-async-bytes = [- "component-model-async",+# Enables support and interop with the `Bytes` and `BytesMut` types contained+# within the `bytes` crate. This enables lift/lower impls, for example, as well+# as stream-related impls for `component-model-async` if that's enabled.+component-model-bytes = ["dep:bytes",]diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex eec6cf54c3..c009f03a89 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -830,7 +830,7 @@ where}}-#[cfg(feature = "component-model-async-bytes")]+#[cfg(feature = "component-model-bytes")]impl<D> StreamProducer<D> for bytes::Bytes {type Item = u8;type Buffer = Self;@@ -847,7 +847,7 @@ impl<D> StreamProducer<D> for bytes::Bytes {}}-#[cfg(feature = "component-model-async-bytes")]+#[cfg(feature = "component-model-bytes")]impl<D> StreamProducer<D> for bytes::BytesMut {type Item = u8;type Buffer = Self;diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rsindex 534942ab39..e65fbfc28b 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams/buffers.rs@@ -1,5 +1,5 @@use crate::prelude::*;-#[cfg(feature = "component-model-async-bytes")]+#[cfg(feature = "component-model-bytes")]use bytes::{Bytes, BytesMut};use core::mem::{self, MaybeUninit};use core::slice;@@ -374,7 +374,7 @@ impl<T: Send + Sync + 'static> ReadBuffer<T> for Vec<T> {// SAFETY: the `take` implementation below guarantees that the `fun` closure is// provided with fully initialized items.-#[cfg(feature = "component-model-async-bytes")]+#[cfg(feature = "component-model-bytes")]unsafe impl WriteBuffer<u8> for Bytes {fn remaining(&self) -> &[u8] {self@@ -392,7 +392,7 @@ unsafe impl WriteBuffer<u8> for Bytes {// SAFETY: the `take` implementation below guarantees that the `fun` closure is// provided with fully initialized items.-#[cfg(feature = "component-model-async-bytes")]+#[cfg(feature = "component-model-bytes")]unsafe impl WriteBuffer<u8> for BytesMut {fn remaining(&self) -> &[u8] {self@@ -408,7 +408,7 @@ unsafe impl WriteBuffer<u8> for BytesMut {}}-#[cfg(feature = "component-model-async-bytes")]+#[cfg(feature = "component-model-bytes")]impl ReadBuffer<u8> for BytesMut {fn extend<I: IntoIterator<Item = u8>>(&mut self, iter: I) {Extend::extend(self, iter)@@ -430,7 +430,7 @@ impl ReadBuffer<u8> for BytesMut {}}-#[cfg(feature = "component-model-async-bytes")]+#[cfg(feature = "component-model-bytes")]fn unsafe_byte_slice(slice: &[u8]) -> &[MaybeUninit<u8>] {// SAFETY: it's always safe to interpret a slice of items as a// possibly-initialized slice of items.@@ -465,7 +465,7 @@ mod tests {}#[test]- #[cfg(feature = "component-model-async-bytes")]+ #[cfg(feature = "component-model-bytes")]fn test_cursor_bytes_take() {let mut buf = Bytes::from(&b"123"[..]);let mut dst = Vec::new();@@ -482,7 +482,7 @@ mod tests {}#[test]- #[cfg(feature = "component-model-async-bytes")]+ #[cfg(feature = "component-model-bytes")]fn test_cursor_bytes_mut_take() {let mut buf = BytesMut::from(&b"123"[..]);let mut dst = Vec::new();diff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rsindex f176a299e3..f8eaf40299 100644--- a/crates/wasmtime/src/runtime/component/func/typed.rs+++ b/crates/wasmtime/src/runtime/component/func/typed.rs@@ -962,7 +962,11 @@ pub unsafe trait Lift: Sized + ComponentType {// these wrappers only implement lowering because lifting native Rust types// cannot be done.macro_rules! forward_type_impls {- ($(($($generics:tt)*) $a:ty => $b:ty,)*) => ($(+ ($(+ $(#[$attr:meta])*+ ($($generics:tt)*) $a:ty => $b:ty,+ )*) => ($(+ $(#[$attr])*unsafe impl <$($generics)*> ComponentType for $a {type Lower = <$b as ComponentType>::Lower;@@ -983,10 +987,18 @@ forward_type_impls! {(T: ComponentType + ?Sized) alloc::sync::Arc<T> => T,() String => str,(T: ComponentType) Vec<T> => [T],+ #[cfg(feature = "component-model-bytes")]+ () bytes::Bytes => [u8],+ #[cfg(feature = "component-model-bytes")]+ () bytes::BytesMut => [u8],}macro_rules! forward_lowers {- ($(($($generics:tt)*) $a:ty => $b:ty,)*) => ($(+ ($(+ $(#[$attr:meta])*+ ($($generics:tt)*) $a:ty => $b:ty,+ )*) => ($(+ $(#[$attr])*unsafe impl <$($generics)*> Lower for $a {fn linear_lower_to_flat<U>(&self,@@ -1015,6 +1027,10 @@ forward_lowers! {(T: Lower + ?Sized) alloc::sync::Arc<T> => T,() String => str,(T: Lower) Vec<T> => [T],+ #[cfg(feature = "component-model-bytes")]+ () bytes::Bytes => [u8],+ #[cfg(feature = "component-model-bytes")]+ () bytes::BytesMut => [u8],}macro_rules! forward_string_lifts {@@ -1044,25 +1060,39 @@ forward_string_lifts! {}macro_rules! forward_list_lifts {- ($($a:ty,)*) => ($(- unsafe impl <T: Lift> Lift for $a {+ ($(+ $(#[$attr:meta])*+ ($($generics:tt)*) $a:ty => WasmList<$b:ty> $(( $via:ident $c:ty ))?,+ )*) => ($(+ $(#[$attr])*+ unsafe impl <$($generics)*> Lift for $a {fn linear_lift_from_flat(cx: &mut LiftContext<'_>, ty: InterfaceType, src: &Self::Lower) -> Result<Self> {- let list = <WasmList::<T> as Lift>::linear_lift_from_flat(cx, ty, src)?;- Ok(T::linear_lift_list_from_memory(cx, &list)?.into())+ let list = <WasmList::<$b> as Lift>::linear_lift_from_flat(cx, ty, src)?;+ let vec = <$b>::linear_lift_list_from_memory(cx, &list)?;+ $(let vec = <$c>::from(vec);)?+ Ok(Self::from(vec))}fn linear_lift_from_memory(cx: &mut LiftContext<'_>, ty: InterfaceType, bytes: &[u8]) -> Result<Self> {- let list = <WasmList::<T> as Lift>::linear_lift_from_memory(cx, ty, bytes)?;- Ok(T::linear_lift_list_from_memory(cx, &list)?.into())+ let list = <WasmList::<$b> as Lift>::linear_lift_from_memory(cx, ty, bytes)?;+ let vec = <$b>::linear_lift_list_from_memory(cx, &list)?;+ $(let vec = <$c>::from(vec);)?+ Ok(Self::from(vec))}})*)}forward_list_lifts! {- Box<[T]>,- alloc::sync::Arc<[T]>,- Vec<T>,+ (T: Lift) Box<[T]> => WasmList<T>,+ (T: Lift) alloc::sync::Arc<[T]> => WasmList<T>,+ (T: Lift) Vec<T> => WasmList<T>,+ #[cfg(feature = "component-model-bytes")]+ () bytes::Bytes => WasmList<u8>,+ // Note that `From<Vec<u8>> for BytesMut` is missing from the `bytes` crate+ // and this is the subject of tokio-rs/bytes#615+ #[cfg(feature = "component-model-bytes")]+ () bytes::BytesMut => WasmList<u8> (via bytes::Bytes),}// Macro to help generate `ComponentType` implementations for primitive typesdiff --git a/tests/all/component_model/func.rs b/tests/all/component_model/func.rsindex 819aa99f6f..5873133fcc 100644--- a/tests/all/component_model/func.rs+++ b/tests/all/component_model/func.rs@@ -1,6 +1,7 @@#![cfg(not(miri))]use super::{ApiStyle, REALLOC_AND_FREE};+use bytes::{Bytes, BytesMut};use std::sync::{Arc,atomic::{AtomicBool, Ordering::SeqCst},@@ -107,6 +108,8 @@ fn typecheck() -> Result<()> {assert!(take_two_args.typed::<(i32, &[u8]), (u32,)>(&store).is_err());assert!(take_two_args.typed::<(u32, &[u8]), ()>(&store).is_err());assert!(take_two_args.typed::<(i32, &[u8]), ()>(&store).is_ok());+ assert!(take_two_args.typed::<(i32, Bytes), ()>(&store).is_ok());+ assert!(take_two_args.typed::<(i32, BytesMut), ()>(&store).is_ok());assert!(ret_tuple.typed::<(), ()>(&store).is_err());assert!(ret_tuple.typed::<(), (u8,)>(&store).is_err());assert!(ret_tuple.typed::<(), ((u8, i8),)>(&store).is_ok());@@ -117,6 +120,8 @@ fn typecheck() -> Result<()> {assert!(ret_list_u8.typed::<(), (WasmList<u16>,)>(&store).is_err());assert!(ret_list_u8.typed::<(), (WasmList<i8>,)>(&store).is_err());assert!(ret_list_u8.typed::<(), (WasmList<u8>,)>(&store).is_ok());+ assert!(ret_list_u8.typed::<(), (Bytes,)>(&store).is_ok());+ assert!(ret_list_u8.typed::<(), (BytesMut,)>(&store).is_ok());Ok(())}