Skip to main content

wasmtime/runtime/component/concurrent/
futures_and_streams.rs

1use super::table::{TableDebug, TableId};
2use super::{Event, GlobalErrorContextRefCount, Waitable, WaitableCommon};
3use crate::component::concurrent::{ConcurrentState, CurrentThread, WorkItem, tls};
4use crate::component::func::{self, LiftContext, LowerContext};
5use crate::component::matching::InstanceType;
6use crate::component::resources::{HostResourceIndex, HostResourceTables};
7use crate::component::types;
8use crate::component::values::ErrorContextAny;
9use crate::component::{
10    AsAccessor, ComponentInstanceId, ComponentType, FutureAny, Instance, Lift, Lower,
11    RuntimeInstance, StreamAny, Val, WasmList,
12};
13use crate::prelude::*;
14use crate::store::{StoreOpaque, StoreToken};
15use crate::try_mutex::{TryMutex, TryMutexGuard};
16use crate::vm::VMTransmitLocalState;
17use crate::vm::component::TransmitLocalStateKind;
18use crate::vm::component::{ComponentInstance, HandleCounter, HandleTable, HandleTableInsertion};
19use crate::vm::{AlwaysMut, VMStore};
20use crate::{AsContext, AsContextMut, StoreContextMut, ValRaw};
21use crate::{
22    Error, Result, Trap, bail, bail_bug, ensure,
23    error::{Context as _, format_err},
24};
25use alloc::sync::Arc;
26use buffers::{Extender, SliceBuffer, UntypedWriteBuffer};
27use core::any::{Any, TypeId};
28use core::fmt;
29use core::future;
30use core::iter;
31use core::marker::PhantomData;
32use core::mem::{self, ManuallyDrop, MaybeUninit};
33use core::ops::{Deref, DerefMut};
34use core::pin::Pin;
35use core::task::{Context, Poll, Waker, ready};
36use futures::channel::oneshot;
37use futures::{FutureExt as _, stream};
38use wasmtime_environ::component::{
39    CanonicalAbiInfo, ComponentTypes, InterfaceType, OptionsIndex, RuntimeComponentInstanceIndex,
40    TypeComponentGlobalErrorContextTableIndex, TypeComponentLocalErrorContextTableIndex,
41    TypeFutureTableIndex, TypeStreamTableIndex,
42};
43
44pub use buffers::{ReadBuffer, VecBuffer, WriteBuffer};
45
46mod buffers;
47
48/// Enum for distinguishing between a stream or future in functions that handle
49/// both.
50#[derive(Copy, Clone, Debug)]
51pub enum TransmitKind {
52    Stream,
53    Future,
54}
55
56/// Represents `{stream,future}.{read,write}` results.
57#[derive(Copy, Clone, Debug, PartialEq)]
58pub enum ReturnCode {
59    Blocked,
60    Completed(ItemCount),
61    Dropped(ItemCount),
62    Cancelled(ItemCount),
63}
64
65impl ReturnCode {
66    /// Pack `self` into a single 32-bit integer that may be returned to the
67    /// guest.
68    ///
69    /// This corresponds to `pack_copy_result` in the Component Model spec.
70    pub fn encode(&self) -> u32 {
71        const BLOCKED: u32 = 0xffff_ffff;
72        const COMPLETED: u32 = 0x0;
73        const DROPPED: u32 = 0x1;
74        const CANCELLED: u32 = 0x2;
75        match self {
76            ReturnCode::Blocked => BLOCKED,
77            ReturnCode::Completed(n) => (n.as_u32() << 4) | COMPLETED,
78            ReturnCode::Dropped(n) => (n.as_u32() << 4) | DROPPED,
79            ReturnCode::Cancelled(n) => (n.as_u32() << 4) | CANCELLED,
80        }
81    }
82
83    /// Returns `Self::Completed` with the specified count (or zero if
84    /// `matches!(kind, TransmitKind::Future)`)
85    fn completed(kind: TransmitKind, count: ItemCount) -> Self {
86        Self::Completed(if let TransmitKind::Future = kind {
87            ItemCount::ZERO
88        } else {
89            count
90        })
91    }
92}
93
94/// Representation of how many items are being operated on in a stream read or
95/// write.
96///
97/// The component model requires that stream operations are limited to `1<<28`
98/// items in one go. This type is a newtype wrapper around `u32` with the
99/// invariant that the internal value is limited by this amount.
100#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
101#[repr(transparent)]
102pub struct ItemCount {
103    raw: u32,
104}
105
106impl ItemCount {
107    const MAX: u32 = 1 << 28;
108    const ZERO: ItemCount = ItemCount { raw: 0 };
109
110    /// Creates a new `ItemCount` with the specified count, or a trap if it's
111    /// too large.
112    fn new(count: u32) -> Result<Self, Trap> {
113        if count < Self::MAX {
114            Ok(Self { raw: count })
115        } else {
116            Err(Trap::StreamOpTooBig)
117        }
118    }
119
120    /// Same as `Self::new` but takes a `usize`.
121    fn new_usize(count: usize) -> Result<Self, Trap> {
122        let count = u32::try_from(count).map_err(|_| Trap::StreamOpTooBig)?;
123        Self::new(count)
124    }
125
126    fn as_u32(&self) -> u32 {
127        self.raw
128    }
129
130    fn as_usize(&self) -> usize {
131        usize::try_from(self.raw).unwrap()
132    }
133
134    /// Increments `self` by `amt`, returning a trap if the amount would exceed
135    /// the maximum item count.
136    fn inc(&mut self, amt: usize) -> Result<(), Trap> {
137        let amt = u32::try_from(amt).map_err(|_| Trap::StreamOpTooBig)?;
138        let new_raw = self.raw.checked_add(amt).ok_or(Trap::StreamOpTooBig)?;
139        if new_raw < Self::MAX {
140            self.raw = new_raw;
141            Ok(())
142        } else {
143            Err(Trap::StreamOpTooBig)
144        }
145    }
146
147    /// Helper to add two `ItemCount`s together, fallibly.
148    ///
149    /// It's considered a bug if this overflows, so this is only suitable in
150    /// situations where overflow and/or exceeding the total item count is known
151    /// that it may be possible.
152    fn add(&self, other: ItemCount) -> Result<ItemCount> {
153        match self.raw.checked_add(other.raw) {
154            Some(raw) => Ok(ItemCount::new(raw)?),
155            None => bail_bug!("overflow in `ItemCount::add`"),
156        }
157    }
158
159    /// Same as `add`, but for subtraction.
160    ///
161    /// Like with `add` this is only suitable for situations where the result is
162    /// known to not underflow.
163    fn sub(&self, other: ItemCount) -> Result<ItemCount> {
164        match self.raw.checked_sub(other.raw) {
165            Some(raw) => Ok(ItemCount { raw }),
166            None => bail_bug!("underflow in `ItemCount::sub`"),
167        }
168    }
169}
170
171impl fmt::Display for ItemCount {
172    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173        self.raw.fmt(f)
174    }
175}
176
177impl fmt::Debug for ItemCount {
178    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
179        self.raw.fmt(f)
180    }
181}
182
183impl PartialEq<u32> for ItemCount {
184    fn eq(&self, other: &u32) -> bool {
185        self.raw == *other
186    }
187}
188
189impl PartialOrd<u32> for ItemCount {
190    fn partial_cmp(&self, other: &u32) -> Option<core::cmp::Ordering> {
191        self.raw.partial_cmp(other)
192    }
193}
194
195/// Represents a stream or future type index.
196///
197/// This is useful as a parameter type for functions which operate on either a
198/// future or a stream.
199#[derive(Copy, Clone, Debug)]
200pub enum TransmitIndex {
201    Stream(TypeStreamTableIndex),
202    Future(TypeFutureTableIndex),
203}
204
205impl TransmitIndex {
206    pub fn kind(&self) -> TransmitKind {
207        match self {
208            TransmitIndex::Stream(_) => TransmitKind::Stream,
209            TransmitIndex::Future(_) => TransmitKind::Future,
210        }
211    }
212
213    /// Retrieve the payload type of the specified stream or future, or `None`
214    /// if it has no payload type.
215    fn payload<'a>(&self, types: &'a ComponentTypes) -> Option<&'a InterfaceType> {
216        match self {
217            TransmitIndex::Stream(i) => {
218                let ty = types[*i].ty;
219                types[ty].payload.as_ref()
220            }
221            TransmitIndex::Future(i) => {
222                let ty = types[*i].ty;
223                types[ty].payload.as_ref()
224            }
225        }
226    }
227}
228
229/// Retrieve the host rep and state for the specified guest-visible waitable
230/// handle.
231fn get_mut_by_index_from(
232    handle_table: &mut HandleTable,
233    ty: TransmitIndex,
234    index: u32,
235) -> Result<(u32, &mut VMTransmitLocalState)> {
236    match ty {
237        TransmitIndex::Stream(ty) => handle_table.stream_rep(ty, index),
238        TransmitIndex::Future(ty) => handle_table.future_rep(ty, index),
239    }
240}
241
242/// Lowers items from the `buffer` of a host `{Stream,Future}Producer` `P` into
243/// a guest's linear memory, where `T` is `P::Item`.
244///
245/// `Lower` requires `T::typecheck` to pass for `ty`'s payload, but no check is
246/// done here. `ty` is the type of the read end's entry in the guest's handle
247/// table, so by the invariant on `lower_transmit_to_index`,
248/// `P::Item::typecheck` has already passed for `ty`'s payload.
249fn lower<T: func::Lower + Send + 'static, B: WriteBuffer<T>, U: 'static>(
250    mut store: StoreContextMut<U>,
251    instance: Instance,
252    options: OptionsIndex,
253    ty: TransmitIndex,
254    address: usize,
255    count: usize,
256    buffer: &mut B,
257) -> Result<()> {
258    let count = buffer.remaining().len().min(count);
259
260    let lower = &mut if T::MAY_REQUIRE_REALLOC {
261        LowerContext::new(store.as_context_mut(), options, instance)
262    } else {
263        LowerContext::new_without_realloc(store.as_context_mut(), options, instance)
264    };
265
266    // Note that validation only happens with operations that have a payload.
267    if let Some(ty) = ty.payload(lower.types).copied() {
268        if address % usize::try_from(T::ALIGN32)? != 0 {
269            bail!("read pointer not aligned");
270        }
271        lower
272            .as_slice()
273            .get(address..)
274            .and_then(|b| b.get(..T::SIZE32 * count))
275            .ok_or_else(|| crate::format_err!("read pointer out of bounds of memory"))?;
276
277        T::linear_store_list_to_memory(lower, ty, address, &buffer.remaining()[..count])?;
278    }
279
280    buffer.skip(count);
281
282    Ok(())
283}
284
285fn lift<T: func::Lift + Send + 'static, B: ReadBuffer<T>>(
286    lift: &mut LiftContext<'_>,
287    ty: Option<InterfaceType>,
288    buffer: &mut B,
289    address: usize,
290    count: usize,
291) -> Result<()> {
292    let count = count.min(buffer.remaining_capacity());
293    if T::IS_RUST_UNIT_TYPE {
294        // SAFETY: `T::IS_RUST_UNIT_TYPE` is only true for `()`, a
295        // zero-sized type, so `MaybeUninit::uninit().assume_init()`
296        // is a valid way to populate the zero-sized buffer.
297        buffer.extend(
298            iter::repeat_with(|| unsafe { MaybeUninit::uninit().assume_init() }).take(count),
299        )
300    } else {
301        let ty = match ty {
302            Some(ty) => ty,
303            None => bail_bug!("type required for non-unit lift"),
304        };
305        if address % usize::try_from(T::ALIGN32)? != 0 {
306            bail!("write pointer not aligned");
307        }
308        lift.memory()
309            .get(address..)
310            .and_then(|b| b.get(..T::SIZE32 * count))
311            .ok_or_else(|| crate::format_err!("write pointer out of bounds of memory"))?;
312
313        let list = &WasmList::new(address, count, lift, ty)?;
314        T::linear_lift_into_from_memory(lift, list, &mut Extender(buffer))?
315    }
316    Ok(())
317}
318
319/// Represents the state associated with an error context
320#[derive(Debug, PartialEq, Eq, PartialOrd)]
321pub(super) struct ErrorContextState {
322    /// Debug message associated with the error context
323    pub(crate) debug_msg: String,
324}
325
326/// Represents the size and alignment for a "flat" Component Model type,
327/// i.e. one containing no pointers or handles.
328#[derive(Debug, Clone, Copy, PartialEq, Eq)]
329pub(super) struct FlatAbi {
330    pub(super) size: u32,
331    pub(super) align: u32,
332}
333
334struct HostBuffer<'a> {
335    dst: &'a mut Vec<u8>,
336    marked_written: &'a mut usize,
337}
338
339impl HostBuffer<'_> {
340    fn reborrow(&mut self) -> HostBuffer<'_> {
341        HostBuffer {
342            dst: &mut *self.dst,
343            marked_written: &mut *self.marked_written,
344        }
345    }
346}
347
348/// Represents the buffer for a host- or guest-initiated stream read.
349pub struct Destination<'a, T, B> {
350    id: TableId<TransmitState>,
351    buffer: &'a mut B,
352    host_buffer: Option<HostBuffer<'a>>,
353    _phantom: PhantomData<fn() -> T>,
354}
355
356impl<'a, T, B> Destination<'a, T, B> {
357    /// Reborrow `self` so it can be used again later.
358    pub fn reborrow(&mut self) -> Destination<'_, T, B> {
359        Destination {
360            id: self.id,
361            buffer: &mut *self.buffer,
362            host_buffer: self.host_buffer.as_mut().map(|b| b.reborrow()),
363            _phantom: PhantomData,
364        }
365    }
366
367    /// Take the buffer out of `self`, leaving a default-initialized one in its
368    /// place.
369    ///
370    /// This can be useful for reusing the previously-stored buffer's capacity
371    /// instead of allocating a fresh one.
372    pub fn take_buffer(&mut self) -> B
373    where
374        B: Default,
375    {
376        mem::take(self.buffer)
377    }
378
379    /// Store the specified buffer in `self`.
380    ///
381    /// Any items contained in the buffer will be delivered to the reader after
382    /// the `StreamProducer::poll_produce` call to which this `Destination` was
383    /// passed returns (unless overwritten by another call to `set_buffer`).
384    ///
385    /// If items are stored via this buffer _and_ written via a
386    /// `DirectDestination` view of `self`, then the items in the buffer will be
387    /// delivered after the ones written using `DirectDestination`.
388    pub fn set_buffer(&mut self, buffer: B) {
389        *self.buffer = buffer;
390    }
391
392    /// Return the remaining number of items the current read has capacity to
393    /// accept, if known.
394    ///
395    /// This will return `Some(_)` if the reader is a guest; it will return
396    /// `None` if the reader is the host.
397    ///
398    /// Note that this can return `Some(0)`. This means that the guest is
399    /// attempting to perform a zero-length read which typically means that it's
400    /// trying to wait for this stream to be ready-to-read but is not actually
401    /// ready to receive the items yet. The host in this case is allowed to
402    /// either block waiting for readiness or immediately complete the
403    /// operation. The guest is expected to handle both cases. Some more
404    /// discussion about this case can be found in the discussion of ["Stream
405    /// Readiness" in the component-model repo][docs].
406    ///
407    /// [docs]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md#stream-readiness
408    pub fn remaining(&self, mut store: impl AsContextMut) -> Result<Option<usize>> {
409        self.remaining_(store.as_context_mut().0)
410    }
411
412    fn remaining_(&self, store: &mut StoreOpaque) -> Result<Option<usize>> {
413        let transmit = store.concurrent_state_mut()?.get_mut(self.id)?;
414
415        if let &ReadState::GuestReady { count, .. } = &transmit.read {
416            let &WriteState::HostReady { guest_offset, .. } = &transmit.write else {
417                bail_bug!("expected WriteState::HostReady")
418            };
419
420            Ok(Some(count.as_usize() - guest_offset.as_usize()))
421        } else {
422            Ok(None)
423        }
424    }
425}
426
427impl<'a, B> Destination<'a, u8, B> {
428    /// Return a `DirectDestination` view of `self`.
429    ///
430    /// If the reader is a guest, this will provide direct access to the guest's
431    /// read buffer.  If the reader is a host, this will provide access to a
432    /// buffer which will be delivered to the host before any items stored using
433    /// `Destination::set_buffer`.
434    ///
435    /// `capacity` will only be used if the reader is a host, in which case it
436    /// will update the length of the buffer, possibly zero-initializing the new
437    /// elements if the new length is larger than the old length.
438    pub fn as_direct<D>(
439        mut self,
440        store: StoreContextMut<'a, D>,
441        capacity: usize,
442    ) -> DirectDestination<'a, D> {
443        if let Some(buffer) = &mut self.host_buffer {
444            *buffer.marked_written = 0;
445            buffer.dst.resize(capacity, 0);
446        }
447
448        DirectDestination {
449            id: self.id,
450            host_buffer: self.host_buffer,
451            store,
452        }
453    }
454}
455
456/// Represents a read from a `stream<u8>`, providing direct access to the
457/// writer's buffer.
458pub struct DirectDestination<'a, D: 'static> {
459    id: TableId<TransmitState>,
460    host_buffer: Option<HostBuffer<'a>>,
461    store: StoreContextMut<'a, D>,
462}
463
464#[cfg(feature = "std")]
465impl<D: 'static> std::io::Write for DirectDestination<'_, D> {
466    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
467        let rem = self.remaining().map_err(std::io::Error::other)?;
468        let n = rem.len().min(buf.len());
469        rem[..n].copy_from_slice(&buf[..n]);
470        self.mark_written(n);
471        Ok(n)
472    }
473
474    fn flush(&mut self) -> std::io::Result<()> {
475        Ok(())
476    }
477}
478
479impl<D: 'static> DirectDestination<'_, D> {
480    /// Provide direct access to the writer's buffer.
481    pub fn remaining(&mut self) -> Result<&mut [u8]> {
482        if let Some(buffer) = self.host_buffer.as_mut() {
483            return Ok(&mut buffer.dst[*buffer.marked_written..]);
484        }
485        let transmit = self
486            .store
487            .as_context_mut()
488            .0
489            .concurrent_state_mut()?
490            .get_mut(self.id)?;
491
492        let &ReadState::GuestReady {
493            address,
494            count,
495            options,
496            instance,
497            ..
498        } = &transmit.read
499        else {
500            bail_bug!("expected ReadState::GuestReady")
501        };
502
503        let &WriteState::HostReady { guest_offset, .. } = &transmit.write else {
504            bail_bug!("expected WriteState::HostReady")
505        };
506
507        let memory = instance.options_memory_range_mut(
508            self.store.0,
509            options,
510            address + guest_offset.as_usize(),
511            count.as_usize() - guest_offset.as_usize(),
512        );
513        match memory {
514            Some(memory) => Ok(memory),
515            None => bail_bug!("guest buffer unexpectedly out of bounds"),
516        }
517    }
518
519    /// Mark the specified number of bytes as written to the writer's buffer.
520    ///
521    /// # Panics
522    ///
523    /// This will panic if the count is larger than the size of the
524    /// buffer returned by `Self::remaining`.
525    pub fn mark_written(&mut self, count: usize) {
526        // Note that this unwrap should only trigger for bugs in Wasmtime, and
527        // this is modeled here to centralize the `.unwrap()` for this method in
528        // one location.
529        self.mark_written_(count).unwrap()
530    }
531
532    fn mark_written_(&mut self, count: usize) -> Result<()> {
533        if let Some(buffer) = self.host_buffer.as_mut() {
534            // Note that this `.unwrap` is a documented panic condition of
535            // `mark_written`.
536            *buffer.marked_written = buffer.marked_written.checked_add(count).unwrap();
537        } else {
538            let transmit = self
539                .store
540                .as_context_mut()
541                .0
542                .concurrent_state_mut()?
543                .get_mut(self.id)?;
544
545            let ReadState::GuestReady {
546                count: read_count, ..
547            } = &transmit.read
548            else {
549                bail_bug!("expected ReadState::GuestReady")
550            };
551
552            let WriteState::HostReady { guest_offset, .. } = &mut transmit.write else {
553                bail_bug!("expected WriteState::HostReady");
554            };
555
556            if guest_offset.as_usize() + count > read_count.as_usize() {
557                // Note that this `panic` is a documented panic condition of
558                // `mark_written`.
559                panic!(
560                    "write count ({count}) must be less than or equal to read count ({read_count})"
561                )
562            } else {
563                guest_offset.inc(count)?;
564            }
565        }
566        Ok(())
567    }
568}
569
570/// Represents the state of a `Stream{Producer,Consumer}`.
571#[derive(Copy, Clone, Debug)]
572pub enum StreamResult {
573    /// The operation completed normally, and the producer or consumer may be
574    /// able to produce or consume more items, respectively.
575    Completed,
576    /// The operation was interrupted (i.e. it wrapped up early after receiving
577    /// a `finish` parameter value of true in a call to `poll_produce` or
578    /// `poll_consume`), and the producer or consumer may be able to produce or
579    /// consume more items, respectively.
580    Cancelled,
581    /// The operation completed normally, but the producer or consumer will
582    /// _not_ able to produce or consume more items, respectively.
583    Dropped,
584}
585
586/// Represents the host-owned write end of a stream.
587pub trait StreamProducer<D>: Send + 'static {
588    /// The payload type of this stream.
589    type Item;
590
591    /// The `WriteBuffer` type to use when delivering items.
592    type Buffer: WriteBuffer<Self::Item> + Default;
593
594    /// Handle a host- or guest-initiated read by delivering zero or more items
595    /// to the specified destination.
596    ///
597    /// This will be called whenever the reader starts a read.
598    ///
599    /// # Arguments
600    ///
601    /// * `self` - a `Pin`'d version of self to perform Rust-level
602    ///   future-related operations on.
603    /// * `cx` - a Rust-related [`Context`] which is passed to other
604    ///   future-related operations or used to acquire a waker.
605    /// * `store` - the Wasmtime store that this operation is happening within.
606    ///   Used, for example, to consult the state `D` associated with the store.
607    /// * `destination` - the location that items are to be written to.
608    /// * `finish` - a flag indicating whether the host should strive to
609    ///   immediately complete/cancel any pending operation. See below for more
610    ///   details.
611    ///
612    /// # Behavior
613    ///
614    /// If the implementation is able to produce one or more items immediately,
615    /// it should write them to `destination` and return either
616    /// `Poll::Ready(Ok(StreamResult::Completed))` if it expects to produce more
617    /// items, or `Poll::Ready(Ok(StreamResult::Dropped))` if it cannot produce
618    /// any more items.
619    ///
620    /// If the implementation is unable to produce any items immediately, but
621    /// expects to do so later, and `finish` is _false_, it should store the
622    /// waker from `cx` for later and return `Poll::Pending` without writing
623    /// anything to `destination`.  Later, it should alert the waker when either
624    /// the items arrive, the stream has ended, or an error occurs.
625    ///
626    /// If more items are written to `destination` than the reader has immediate
627    /// capacity to accept, they will be retained in memory by the caller and
628    /// used to satisfy future reads, in which case `poll_produce` will only be
629    /// called again once all those items have been delivered.
630    ///
631    /// # Zero-length reads
632    ///
633    /// This function may be called with a zero-length capacity buffer
634    /// (i.e. `Destination::remaining` returns `Some(0)`). This indicates that
635    /// the guest wants to wait to see if an item is ready without actually
636    /// reading the item. For example think of a UNIX `poll` function run on a
637    /// TCP stream, seeing if it's readable without actually reading it.
638    ///
639    /// In this situation the host is allowed to either return immediately or
640    /// wait for readiness. Note that waiting for readiness is not always
641    /// possible. For example it's impossible to test if a Rust-native `Future`
642    /// is ready without actually reading the item. Stream-specific
643    /// optimizations, such as testing if a TCP stream is readable, may be
644    /// possible however.
645    ///
646    /// For a zero-length read, the host is allowed to:
647    ///
648    /// - Return `Poll::Ready(Ok(StreamResult::Completed))` without writing
649    ///   anything if it expects to be able to produce items immediately (i.e.
650    ///   without first returning `Poll::Pending`) the next time `poll_produce`
651    ///   is called with non-zero capacity. This is the best-case scenario of
652    ///   fulfilling the guest's desire -- items aren't read/buffered but the
653    ///   host is saying it's ready when the guest is.
654    ///
655    /// - Return `Poll::Ready(Ok(StreamResult::Completed))` without actually
656    ///   testing for readiness. The guest doesn't know this yet, but the guest
657    ///   will realize that zero-length reads won't work on this stream when a
658    ///   subsequent nonzero read attempt is made which returns `Poll::Pending`
659    ///   here.
660    ///
661    /// - Return `Poll::Pending` if the host has performed necessary async work
662    ///   to wait for this stream to be readable without actually reading
663    ///   anything. This is also a best-case scenario where the host is letting
664    ///   the guest know that nothing is ready yet. Later the zero-length read
665    ///   will complete and then the guest will attempt a nonzero-length read to
666    ///   actually read some bytes.
667    ///
668    /// - Return `Poll::Ready(Ok(StreamResult::Completed))` after calling
669    ///   `Destination::set_buffer` with one more more items. Note, however,
670    ///   that this creates the hazard that the items will never be received by
671    ///   the guest if it decides not to do another non-zero-length read before
672    ///   closing the stream.  Moreover, if `Self::Item` is e.g. a
673    ///   `Resource<_>`, they may end up leaking in that scenario. It is not
674    ///   recommended to do this and it's better to return
675    ///   `StreamResult::Completed` without buffering anything instead.
676    ///
677    /// For more discussion on zero-length reads see the [documentation in the
678    /// component-model repo itself][docs].
679    ///
680    /// [docs]: https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md#stream-readiness
681    ///
682    /// # Return
683    ///
684    /// This function can return a number of possible cases from this function:
685    ///
686    /// * `Poll::Pending` - this operation cannot complete at this time. The
687    ///   Rust-level `Future::poll` contract applies here where a waker should
688    ///   be stored from the `cx` argument and be arranged to receive a
689    ///   notification when this implementation can make progress. For example
690    ///   if you call `Future::poll` on a sub-future, that's enough. If items
691    ///   were written to `destination` then a trap in the guest will be raised.
692    ///
693    ///   Note that implementations should strive to avoid this return value
694    ///   when `finish` is `true`. In such a situation the guest is attempting
695    ///   to, for example, cancel a previous operation. By returning
696    ///   `Poll::Pending` the guest will be blocked during the cancellation
697    ///   request. If `finish` is `true` then `StreamResult::Cancelled` is
698    ///   favored to indicate that no items were read. If a short read happened,
699    ///   however, it's ok to return `StreamResult::Completed` indicating some
700    ///   items were read.
701    ///
702    /// * `Poll::Ok(StreamResult::Completed)` - items, if applicable, were
703    ///   written to the `destination`. Only zero-length reads (see above) may
704    ///   complete without writing anything; if nothing was written for a
705    ///   nonzero-length read, or for any read by the host, then a trap in the
706    ///   guest will be raised.
707    ///
708    /// * `Poll::Ok(StreamResult::Cancelled)` - used when `finish` is `true` and
709    ///   the implementation was able to successfully cancel any async work that
710    ///   a previous read kicked off, if any. The host should not buffer values
711    ///   received after returning `Cancelled` because the guest will not be
712    ///   aware of these values and the guest could close the stream after
713    ///   cancelling a read. Hosts should only return `Cancelled` when there are
714    ///   no more async operations in flight for a previous read.
715    ///
716    ///   If items were written to `destination` then a trap in the guest will
717    ///   be raised. If `finish` is `false` then this return value will raise a
718    ///   trap in the guest.
719    ///
720    /// * `Poll::Ok(StreamResult::Dropped)` - end-of-stream marker, indicating
721    ///   that this producer should not be polled again. Note that items may
722    ///   still be written to `destination`.
723    ///
724    /// # Errors
725    ///
726    /// The implementation may alternatively choose to return `Err(_)` to
727    /// indicate an unrecoverable error. This will cause the guest (if any) to
728    /// trap and render the component instance (if any) unusable. The
729    /// implementation should report errors that _are_ recoverable by other
730    /// means (e.g. by writing to a `future`) and return
731    /// `Poll::Ready(Ok(StreamResult::Dropped))`.
732    fn poll_produce<'a>(
733        self: Pin<&mut Self>,
734        cx: &mut Context<'_>,
735        store: StoreContextMut<'a, D>,
736        destination: Destination<'a, Self::Item, Self::Buffer>,
737        finish: bool,
738    ) -> Poll<Result<StreamResult>>;
739
740    /// Attempt to convert the specified object into a `Box<dyn Any>` which may
741    /// be downcast to the specified type.
742    ///
743    /// `buffer` contains any items which this producer previously passed to
744    /// [`Destination::set_buffer`] but which have not yet been delivered to
745    /// the reader. If the implementation returns `Ok(_)`, it must ensure those
746    /// items are retained by the returned value, since they are otherwise
747    /// lost.
748    ///
749    /// The implementation must ensure that, if it returns `Ok(_)`, a downcast
750    /// to the specified type is guaranteed to succeed.
751    fn try_into(
752        me: Pin<Box<Self>>,
753        buffer: Self::Buffer,
754        _ty: TypeId,
755    ) -> Result<Box<dyn Any>, (Pin<Box<Self>>, Self::Buffer)> {
756        Err((me, buffer))
757    }
758}
759
760impl<T, D> StreamProducer<D> for iter::Empty<T>
761where
762    T: Send + Sync + 'static,
763{
764    type Item = T;
765    type Buffer = Option<Self::Item>;
766
767    fn poll_produce<'a>(
768        self: Pin<&mut Self>,
769        _: &mut Context<'_>,
770        _: StoreContextMut<'a, D>,
771        _: Destination<'a, Self::Item, Self::Buffer>,
772        _: bool,
773    ) -> Poll<Result<StreamResult>> {
774        Poll::Ready(Ok(StreamResult::Dropped))
775    }
776}
777
778impl<T, D> StreamProducer<D> for stream::Empty<T>
779where
780    T: Send + Sync + 'static,
781{
782    type Item = T;
783    type Buffer = Option<Self::Item>;
784
785    fn poll_produce<'a>(
786        self: Pin<&mut Self>,
787        _: &mut Context<'_>,
788        _: StoreContextMut<'a, D>,
789        _: Destination<'a, Self::Item, Self::Buffer>,
790        _: bool,
791    ) -> Poll<Result<StreamResult>> {
792        Poll::Ready(Ok(StreamResult::Dropped))
793    }
794}
795
796impl<T, D> StreamProducer<D> for Vec<T>
797where
798    T: Unpin + Send + Sync + 'static,
799{
800    type Item = T;
801    type Buffer = VecBuffer<T>;
802
803    fn poll_produce<'a>(
804        self: Pin<&mut Self>,
805        _: &mut Context<'_>,
806        _: StoreContextMut<'a, D>,
807        mut dst: Destination<'a, Self::Item, Self::Buffer>,
808        _: bool,
809    ) -> Poll<Result<StreamResult>> {
810        dst.set_buffer(mem::take(self.get_mut()).into());
811        Poll::Ready(Ok(StreamResult::Dropped))
812    }
813}
814
815impl<T, D> StreamProducer<D> for Box<[T]>
816where
817    T: Unpin + Send + Sync + 'static,
818{
819    type Item = T;
820    type Buffer = VecBuffer<T>;
821
822    fn poll_produce<'a>(
823        self: Pin<&mut Self>,
824        _: &mut Context<'_>,
825        _: StoreContextMut<'a, D>,
826        mut dst: Destination<'a, Self::Item, Self::Buffer>,
827        _: bool,
828    ) -> Poll<Result<StreamResult>> {
829        dst.set_buffer(mem::take(self.get_mut()).into_vec().into());
830        Poll::Ready(Ok(StreamResult::Dropped))
831    }
832}
833
834#[cfg(feature = "component-model-bytes")]
835impl<D> StreamProducer<D> for bytes::Bytes {
836    type Item = u8;
837    type Buffer = Self;
838
839    fn poll_produce<'a>(
840        self: Pin<&mut Self>,
841        _: &mut Context<'_>,
842        _store: StoreContextMut<'a, D>,
843        mut dst: Destination<'a, Self::Item, Self::Buffer>,
844        _: bool,
845    ) -> Poll<Result<StreamResult>> {
846        dst.set_buffer(mem::take(self.get_mut()));
847        Poll::Ready(Ok(StreamResult::Dropped))
848    }
849}
850
851#[cfg(feature = "component-model-bytes")]
852impl<D> StreamProducer<D> for bytes::BytesMut {
853    type Item = u8;
854    type Buffer = Self;
855
856    fn poll_produce<'a>(
857        self: Pin<&mut Self>,
858        _: &mut Context<'_>,
859        _store: StoreContextMut<'a, D>,
860        mut dst: Destination<'a, Self::Item, Self::Buffer>,
861        _: bool,
862    ) -> Poll<Result<StreamResult>> {
863        dst.set_buffer(mem::take(self.get_mut()));
864        Poll::Ready(Ok(StreamResult::Dropped))
865    }
866}
867
868/// Represents the buffer for a host- or guest-initiated stream write.
869pub struct Source<'a, T> {
870    id: TableId<TransmitState>,
871    host_buffer: Option<&'a mut dyn WriteBuffer<T>>,
872}
873
874impl<'a, T> Source<'a, T> {
875    /// Reborrow `self` so it can be used again later.
876    pub fn reborrow(&mut self) -> Source<'_, T> {
877        Source {
878            id: self.id,
879            host_buffer: self.host_buffer.as_deref_mut(),
880        }
881    }
882
883    /// Accept zero or more items from the writer.
884    pub fn read<B, S: AsContextMut>(&mut self, mut store: S, buffer: &mut B) -> Result<()>
885    where
886        T: func::Lift + 'static,
887        B: ReadBuffer<T>,
888    {
889        if let Some(input) = &mut self.host_buffer {
890            let count = input.remaining().len().min(buffer.remaining_capacity());
891            buffer.move_from(*input, count);
892        } else {
893            let store = store.as_context_mut();
894            let transmit = store.0.concurrent_state_mut()?.get_mut(self.id)?;
895
896            let &ReadState::HostReady { guest_offset, .. } = &transmit.read else {
897                bail_bug!("expected ReadState::HostReady");
898            };
899
900            let &WriteState::GuestReady {
901                ty,
902                address,
903                count,
904                options,
905                instance,
906                ..
907            } = &transmit.write
908            else {
909                bail_bug!("expected WriteState::GuestReady");
910            };
911
912            let cx = &mut LiftContext::new(&mut **store.0, options, instance)?;
913            let ty = ty.payload(cx.types);
914            let old_remaining = buffer.remaining_capacity();
915            lift::<T, B>(
916                cx,
917                ty.copied(),
918                buffer,
919                address + (T::SIZE32 * guest_offset.as_usize()),
920                count.as_usize() - guest_offset.as_usize(),
921            )?;
922
923            let transmit = store.0.concurrent_state_mut()?.get_mut(self.id)?;
924
925            let ReadState::HostReady { guest_offset, .. } = &mut transmit.read else {
926                bail_bug!("expected ReadState::HostReady");
927            };
928
929            guest_offset.inc(old_remaining - buffer.remaining_capacity())?;
930        }
931
932        Ok(())
933    }
934
935    /// Return the number of items remaining to be read from the current write
936    /// operation.
937    pub fn remaining(&self, mut store: impl AsContextMut) -> Result<usize>
938    where
939        T: 'static,
940    {
941        self.remaining_(store.as_context_mut().0)
942    }
943
944    fn remaining_(&self, store: &mut StoreOpaque) -> Result<usize>
945    where
946        T: 'static,
947    {
948        let transmit = store.concurrent_state_mut()?.get_mut(self.id)?;
949
950        if let &WriteState::GuestReady { count, .. } = &transmit.write {
951            let &ReadState::HostReady { guest_offset, .. } = &transmit.read else {
952                bail_bug!("expected ReadState::HostReady")
953            };
954
955            Ok(count.as_usize() - guest_offset.as_usize())
956        } else if let Some(host_buffer) = &self.host_buffer {
957            Ok(host_buffer.remaining().len())
958        } else {
959            bail_bug!("expected either WriteState::GuestReady or host buffer")
960        }
961    }
962}
963
964impl<'a> Source<'a, u8> {
965    /// Return a `DirectSource` view of `self`.
966    pub fn as_direct<D>(self, store: StoreContextMut<'a, D>) -> DirectSource<'a, D> {
967        DirectSource {
968            id: self.id,
969            host_buffer: self.host_buffer,
970            store,
971        }
972    }
973}
974
975/// Represents a write to a `stream<u8>`, providing direct access to the
976/// writer's buffer.
977pub struct DirectSource<'a, D: 'static> {
978    id: TableId<TransmitState>,
979    host_buffer: Option<&'a mut dyn WriteBuffer<u8>>,
980    store: StoreContextMut<'a, D>,
981}
982
983#[cfg(feature = "std")]
984impl<D: 'static> std::io::Read for DirectSource<'_, D> {
985    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
986        let rem = self.remaining().map_err(std::io::Error::other)?;
987        let n = rem.len().min(buf.len());
988        buf[..n].copy_from_slice(&rem[..n]);
989        self.mark_read(n);
990        Ok(n)
991    }
992}
993
994impl<D: 'static> DirectSource<'_, D> {
995    /// Provide direct access to the writer's buffer.
996    pub fn remaining(&mut self) -> Result<&[u8]> {
997        if let Some(buffer) = self.host_buffer.as_deref_mut() {
998            return Ok(buffer.remaining());
999        }
1000        let transmit = self
1001            .store
1002            .as_context_mut()
1003            .0
1004            .concurrent_state_mut()?
1005            .get_mut(self.id)?;
1006
1007        let &WriteState::GuestReady {
1008            address,
1009            count,
1010            options,
1011            instance,
1012            ..
1013        } = &transmit.write
1014        else {
1015            bail_bug!("expected WriteState::GuestReady")
1016        };
1017
1018        let &ReadState::HostReady { guest_offset, .. } = &transmit.read else {
1019            bail_bug!("expected ReadState::HostReady")
1020        };
1021
1022        let memory = instance
1023            .options_memory(self.store.0, options)
1024            .get(address + guest_offset.as_usize()..)
1025            .and_then(|b| b.get(..(count.as_usize() - guest_offset.as_usize())));
1026        match memory {
1027            Some(memory) => Ok(memory),
1028            None => bail_bug!("guest buffer unexpectedly out of bounds"),
1029        }
1030    }
1031
1032    /// Mark the specified number of bytes as read from the writer's buffer.
1033    ///
1034    /// # Panics
1035    ///
1036    /// This will panic if the count is larger than the size of the buffer
1037    /// returned by `Self::remaining`.
1038    pub fn mark_read(&mut self, count: usize) {
1039        // Note that this unwrap should only trigger for bugs in Wasmtime, and
1040        // this is modeled here to centralize the `.unwrap()` for this method in
1041        // one location.
1042        self.mark_read_(count).unwrap()
1043    }
1044
1045    fn mark_read_(&mut self, count: usize) -> Result<()> {
1046        if let Some(buffer) = self.host_buffer.as_deref_mut() {
1047            buffer.skip(count);
1048            return Ok(());
1049        }
1050
1051        let transmit = self
1052            .store
1053            .as_context_mut()
1054            .0
1055            .concurrent_state_mut()?
1056            .get_mut(self.id)?;
1057
1058        let WriteState::GuestReady {
1059            count: write_count, ..
1060        } = &transmit.write
1061        else {
1062            bail_bug!("expected WriteState::GuestReady");
1063        };
1064
1065        let ReadState::HostReady { guest_offset, .. } = &mut transmit.read else {
1066            bail_bug!("expected ReadState::HostReady");
1067        };
1068
1069        if guest_offset.as_usize() + count > write_count.as_usize() {
1070            // Note that this is a documented panic condition of `mark_read`.
1071            panic!("read count ({count}) must be less than or equal to write count ({write_count})")
1072        } else {
1073            guest_offset.inc(count)?;
1074        }
1075        Ok(())
1076    }
1077}
1078
1079/// Represents the host-owned read end of a stream.
1080pub trait StreamConsumer<D>: Send + 'static {
1081    /// The payload type of this stream.
1082    type Item;
1083
1084    /// Handle a host- or guest-initiated write by accepting zero or more items
1085    /// from the specified source.
1086    ///
1087    /// This will be called whenever the writer starts a write.
1088    ///
1089    /// If the implementation is able to consume one or more items immediately,
1090    /// it should take them from `source` and return either
1091    /// `Poll::Ready(Ok(StreamResult::Completed))` if it expects to be able to consume
1092    /// more items, or `Poll::Ready(Ok(StreamResult::Dropped))` if it cannot
1093    /// accept any more items.  Alternatively, it may return `Poll::Pending` to
1094    /// indicate that the caller should delay sending a `COMPLETED` event to the
1095    /// writer until a later call to this function returns `Poll::Ready(_)`.
1096    /// For more about that, see the `Backpressure` section below.
1097    ///
1098    /// If the implementation cannot consume any items immediately and `finish`
1099    /// is _false_, it should store the waker from `cx` for later and return
1100    /// `Poll::Pending` without writing anything to `destination`.  Later, it
1101    /// should alert the waker when either (1) the items arrive, (2) the stream
1102    /// has ended, or (3) an error occurs.
1103    ///
1104    /// If the implementation cannot consume any items immediately and `finish`
1105    /// is _true_, it should, if possible, return
1106    /// `Poll::Ready(Ok(StreamResult::Cancelled))` immediately without taking
1107    /// anything from `source`.  However, that might not be possible if an
1108    /// earlier call to `poll_consume` kicked off an asynchronous operation
1109    /// which needs to be completed (and possibly interrupted) gracefully, in
1110    /// which case the implementation may return `Poll::Pending` and later alert
1111    /// the waker as described above.  In other words, when `finish` is true,
1112    /// the implementation should prioritize returning a result to the reader
1113    /// (even if no items can be consumed) rather than wait indefinitely for at
1114    /// capacity to free up.
1115    ///
1116    /// In all of the above cases, the implementation may alternatively choose
1117    /// to return `Err(_)` to indicate an unrecoverable error.  This will cause
1118    /// the guest (if any) to trap and render the component instance (if any)
1119    /// unusable.  The implementation should report errors that _are_
1120    /// recoverable by other means (e.g. by writing to a `future`) and return
1121    /// `Poll::Ready(Ok(StreamResult::Dropped))`.
1122    ///
1123    /// Note that the implementation should only return
1124    /// `Poll::Ready(Ok(StreamResult::Cancelled))` without having taken any
1125    /// items from `source` if called with `finish` set to true.  If it does so
1126    /// when `finish` is false, the caller will trap.  Additionally, it should
1127    /// only return `Poll::Ready(Ok(StreamResult::Completed))` after taking at
1128    /// least one item from `source` if there is an item available; otherwise,
1129    /// the caller will trap.  Items taken during earlier calls for the same
1130    /// write which returned `Poll::Pending` count towards this, so it's fine
1131    /// to return `Completed` without taking more items once those have been
1132    /// forwarded (see "Backpressure" below).  If `poll_consume` is called with
1133    /// no items in
1134    /// `source`, it should only return `Poll::Ready(_)` once it is able to
1135    /// accept at least one item during the next call to `poll_consume`.
1136    ///
1137    /// Note that any items which the implementation of this trait takes from
1138    /// `source` become the responsibility of that implementation.  For that
1139    /// reason, an implementation which forwards items to an upstream sink
1140    /// should reserve capacity in that sink before taking items out of
1141    /// `source`, if possible.  Alternatively, it might buffer items which can't
1142    /// be forwarded immediately and send them once capacity is freed up.
1143    ///
1144    /// ## Backpressure
1145    ///
1146    /// As mentioned above, an implementation might choose to return
1147    /// `Poll::Pending` after taking items from `source`, which tells the caller
1148    /// to delay sending a `COMPLETED` event to the writer.  This can be used as
1149    /// a form of backpressure when the items are forwarded to an upstream sink
1150    /// asynchronously.  Note, however, that it's not possible to "put back"
1151    /// items into `source` once they've been taken out, so if the upstream sink
1152    /// is unable to accept all the items, that cannot be communicated to the
1153    /// writer at this level of abstraction.  Just as with application-specific,
1154    /// recoverable errors, information about which items could be forwarded and
1155    /// which could not must be communicated out-of-band, e.g. by writing to an
1156    /// application-specific `future`.
1157    ///
1158    /// Similarly, if the writer cancels the write after items have been taken
1159    /// from `source` but before the items have all been forwarded to an
1160    /// upstream sink, `poll_consume` will be called with `finish` set to true,
1161    /// and the implementation may either:
1162    ///
1163    /// - Interrupt the forwarding process gracefully.  This may be preferable
1164    /// if there is an out-of-band channel for communicating to the writer how
1165    /// many items were forwarded before being interrupted.
1166    ///
1167    /// - Allow the forwarding to complete without interrupting it.  This is
1168    /// usually preferable if there's no out-of-band channel for reporting back
1169    /// to the writer how many items were forwarded.
1170    fn poll_consume(
1171        self: Pin<&mut Self>,
1172        cx: &mut Context<'_>,
1173        store: StoreContextMut<D>,
1174        source: Source<'_, Self::Item>,
1175        finish: bool,
1176    ) -> Poll<Result<StreamResult>>;
1177}
1178
1179/// Represents a host-owned write end of a future.
1180pub trait FutureProducer<D>: Send + 'static {
1181    /// The payload type of this future.
1182    type Item;
1183
1184    /// Handle a host- or guest-initiated read by producing a value.
1185    ///
1186    /// This is equivalent to `StreamProducer::poll_produce`, but with a
1187    /// simplified interface for futures.
1188    ///
1189    /// If `finish` is true, the implementation may return
1190    /// `Poll::Ready(Ok(None))` to indicate the operation was canceled before it
1191    /// could produce a value.  Otherwise, it must either return
1192    /// `Poll::Ready(Ok(Some(_)))`, `Poll::Ready(Err(_))`, or `Poll::Pending`.
1193    fn poll_produce(
1194        self: Pin<&mut Self>,
1195        cx: &mut Context<'_>,
1196        store: StoreContextMut<D>,
1197        finish: bool,
1198    ) -> Poll<Result<Option<Self::Item>>>;
1199}
1200
1201impl<T, E, D, Fut> FutureProducer<D> for Fut
1202where
1203    E: Into<Error>,
1204    Fut: Future<Output = Result<T, E>> + ?Sized + Send + 'static,
1205{
1206    type Item = T;
1207
1208    fn poll_produce<'a>(
1209        self: Pin<&mut Self>,
1210        cx: &mut Context<'_>,
1211        _: StoreContextMut<'a, D>,
1212        finish: bool,
1213    ) -> Poll<Result<Option<T>>> {
1214        match self.poll(cx) {
1215            Poll::Ready(Ok(v)) => Poll::Ready(Ok(Some(v))),
1216            Poll::Ready(Err(err)) => Poll::Ready(Err(err.into())),
1217            Poll::Pending if finish => Poll::Ready(Ok(None)),
1218            Poll::Pending => Poll::Pending,
1219        }
1220    }
1221}
1222
1223/// Represents a host-owned read end of a future.
1224pub trait FutureConsumer<D>: Send + 'static {
1225    /// The payload type of this future.
1226    type Item;
1227
1228    /// Handle a host- or guest-initiated write by consuming a value.
1229    ///
1230    /// This is equivalent to `StreamProducer::poll_produce`, but with a
1231    /// simplified interface for futures.
1232    ///
1233    /// If `finish` is true, the implementation may return `Poll::Ready(Ok(()))`
1234    /// without taking the item from `source`, which indicates the operation was
1235    /// canceled before it could consume the value.  Otherwise, it must either
1236    /// take the item from `source` and return `Poll::Ready(Ok(()))`, or else
1237    /// return `Poll::Ready(Err(_))` or `Poll::Pending` (with or without taking
1238    /// the item).
1239    fn poll_consume(
1240        self: Pin<&mut Self>,
1241        cx: &mut Context<'_>,
1242        store: StoreContextMut<D>,
1243        source: Source<'_, Self::Item>,
1244        finish: bool,
1245    ) -> Poll<Result<()>>;
1246}
1247
1248/// Represents the readable end of a Component Model `future`.
1249///
1250/// Note that `FutureReader` instances must be disposed of using either `pipe`
1251/// or `close`; otherwise the in-store representation will leak and the writer
1252/// end will hang indefinitely.  Consider using [`GuardedFutureReader`] to
1253/// ensure that disposal happens automatically.
1254pub struct FutureReader<T> {
1255    idx: HostResourceIndex,
1256    // It has already been checked that this read end is connected to a
1257    // `FutureProducer` with `Item = T`.
1258    producer_type_match_checked: bool,
1259    _phantom: PhantomData<T>,
1260}
1261
1262impl<T> FutureReader<T> {
1263    /// Create a new future with the specified producer.
1264    ///
1265    /// # Errors
1266    ///
1267    /// Returns an error if the resource table for this store is full or if
1268    /// [`Config::concurrency_support`] is not enabled.
1269    ///
1270    /// [`Config::concurrency_support`]: crate::Config::concurrency_support
1271    pub fn new<S: AsContextMut>(
1272        mut store: S,
1273        producer: impl FutureProducer<S::Data, Item = T>,
1274    ) -> Result<Self>
1275    where
1276        T: func::Lower + func::Lift + Send + Sync + 'static,
1277    {
1278        ensure!(
1279            store.as_context().0.concurrency_support(),
1280            "concurrency support is not enabled"
1281        );
1282
1283        struct Producer<P>(P);
1284
1285        impl<D, T: func::Lower + 'static, P: FutureProducer<D, Item = T>> StreamProducer<D>
1286            for Producer<P>
1287        {
1288            type Item = P::Item;
1289            type Buffer = Option<P::Item>;
1290
1291            fn poll_produce<'a>(
1292                self: Pin<&mut Self>,
1293                cx: &mut Context<'_>,
1294                store: StoreContextMut<D>,
1295                mut destination: Destination<'a, Self::Item, Self::Buffer>,
1296                finish: bool,
1297            ) -> Poll<Result<StreamResult>> {
1298                // SAFETY: This is a standard pin-projection, and we never move
1299                // out of `self`.
1300                let producer = unsafe { self.map_unchecked_mut(|v| &mut v.0) };
1301
1302                Poll::Ready(Ok(
1303                    if let Some(value) = ready!(producer.poll_produce(cx, store, finish))? {
1304                        destination.set_buffer(Some(value));
1305
1306                        // Here we return `StreamResult::Completed` even though
1307                        // we've produced the last item we'll ever produce.
1308                        // That's because the ABI expects
1309                        // `ReturnCode::Completed(1)` rather than
1310                        // `ReturnCode::Dropped(1)`.  In any case, we won't be
1311                        // called again since the future will have resolved.
1312                        StreamResult::Completed
1313                    } else {
1314                        StreamResult::Cancelled
1315                    },
1316                ))
1317            }
1318        }
1319
1320        Ok(Self {
1321            idx: store
1322                .as_context_mut()
1323                .new_transmit(TransmitKind::Future, Producer(producer))?,
1324            producer_type_match_checked: true,
1325            _phantom: PhantomData,
1326        })
1327    }
1328
1329    pub(super) fn new_(idx: HostResourceIndex) -> Self {
1330        Self {
1331            idx,
1332            // It isn't known if this future comes from a `FutureProducer` with
1333            // `Item = T`.
1334            producer_type_match_checked: false,
1335            _phantom: PhantomData,
1336        }
1337    }
1338
1339    pub(super) fn idx(&self) -> HostResourceIndex {
1340        self.idx
1341    }
1342
1343    /// Set the consumer that accepts the result of this future.
1344    ///
1345    /// # Errors
1346    ///
1347    /// Returns an error if this future has already been closed or otherwise
1348    /// transferred (e.g. lowered into a guest), or if it does not belong to
1349    /// `store`.
1350    pub fn pipe<S: AsContextMut>(
1351        self,
1352        mut store: S,
1353        consumer: impl FutureConsumer<S::Data, Item = T> + Unpin,
1354    ) -> Result<()>
1355    where
1356        T: func::Lift + 'static,
1357    {
1358        struct Consumer<C>(C);
1359
1360        impl<D: 'static, T: func::Lift + 'static, C: FutureConsumer<D, Item = T>> StreamConsumer<D>
1361            for Consumer<C>
1362        {
1363            type Item = T;
1364
1365            fn poll_consume(
1366                self: Pin<&mut Self>,
1367                cx: &mut Context<'_>,
1368                mut store: StoreContextMut<D>,
1369                mut source: Source<Self::Item>,
1370                finish: bool,
1371            ) -> Poll<Result<StreamResult>> {
1372                // SAFETY: This is a standard pin-projection, and we never move
1373                // out of `self`.
1374                let consumer = unsafe { self.map_unchecked_mut(|v| &mut v.0) };
1375
1376                ready!(consumer.poll_consume(
1377                    cx,
1378                    store.as_context_mut(),
1379                    source.reborrow(),
1380                    finish
1381                ))?;
1382
1383                Poll::Ready(Ok(if source.remaining(store)? == 0 {
1384                    // Here we return `StreamResult::Completed` even though
1385                    // we've consumed the last item we'll ever consume.  That's
1386                    // because the ABI expects `ReturnCode::Completed(1)` rather
1387                    // than `ReturnCode::Dropped(1)`.  In any case, we won't be
1388                    // called again since the future will have resolved.
1389                    StreamResult::Completed
1390                } else {
1391                    StreamResult::Cancelled
1392                }))
1393            }
1394        }
1395
1396        store
1397            .as_context_mut()
1398            .set_consumer(self.idx, TransmitKind::Future, Consumer(consumer))
1399    }
1400
1401    /// Transfer ownership of the read end of a future from a guest to the host.
1402    fn lift_from_index(cx: &mut LiftContext<'_>, ty: InterfaceType, index: u32) -> Result<Self> {
1403        Ok(Self::new_(lift_index_to_future(cx, ty, index)?))
1404    }
1405
1406    /// Close this `FutureReader`.
1407    ///
1408    /// This will close this half of the future which will signal to a pending
1409    /// write, if any, that the reader side is dropped. If the writer half has
1410    /// not yet written a value then when it attempts to write a value it will
1411    /// see that this end is closed.
1412    ///
1413    /// # Errors
1414    ///
1415    /// Returns an error if this future has already been closed or otherwise
1416    /// transferred (e.g. lowered into a guest), or if it does not belong to
1417    /// `store`.
1418    pub fn close(&mut self, mut store: impl AsContextMut) -> Result<()> {
1419        future_close(store.as_context_mut().0, self.idx)
1420    }
1421
1422    /// Convenience method around [`Self::close`].
1423    pub fn close_with(&mut self, accessor: impl AsAccessor) -> Result<()> {
1424        accessor.as_accessor().with(|access| self.close(access))
1425    }
1426
1427    /// Returns a [`GuardedFutureReader`] which will auto-close this future on
1428    /// drop and clean it up from the store.
1429    ///
1430    /// Note that the `accessor` provided must own this future and is
1431    /// additionally transferred to the `GuardedFutureReader` return value.
1432    pub fn guard<A>(self, accessor: A) -> GuardedFutureReader<T, A>
1433    where
1434        A: AsAccessor,
1435    {
1436        GuardedFutureReader::new(accessor, self)
1437    }
1438
1439    /// Attempts to convert this [`FutureReader<T>`] to a [`FutureAny`].
1440    ///
1441    /// # Errors
1442    ///
1443    /// This function will return an error if `self` does not belong to
1444    /// `store`.
1445    pub fn try_into_future_any(self, store: impl AsContextMut) -> Result<FutureAny>
1446    where
1447        T: ComponentType + 'static,
1448    {
1449        FutureAny::try_from_future_reader(store, self)
1450    }
1451
1452    /// Attempts to convert a [`FutureAny`] into a [`FutureReader<T>`].
1453    ///
1454    /// # Errors
1455    ///
1456    /// This function will fail if `T` doesn't match the type of the value that
1457    /// `future` is sending.
1458    pub fn try_from_future_any(future: FutureAny) -> Result<Self>
1459    where
1460        T: ComponentType + 'static,
1461    {
1462        future.try_into_future_reader()
1463    }
1464}
1465
1466impl<T> fmt::Debug for FutureReader<T> {
1467    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1468        f.debug_struct("FutureReader")
1469            .field("idx", &self.idx)
1470            .finish()
1471    }
1472}
1473
1474pub(super) fn future_close(store: &mut StoreOpaque, idx: HostResourceIndex) -> Result<()> {
1475    let id = store.host_reader_take(idx)?;
1476    store.host_drop_reader(id, TransmitKind::Future)
1477}
1478
1479/// Transfer ownership of the read end of a future from a guest to the host.
1480pub(super) fn lift_index_to_future(
1481    cx: &mut LiftContext<'_>,
1482    ty: InterfaceType,
1483    index: u32,
1484) -> Result<HostResourceIndex> {
1485    match ty {
1486        InterfaceType::Future(src) => {
1487            let id = {
1488                let (state, instance) = cx.concurrent_state_and_instance_mut();
1489                lift_index_to_transmit(instance, state, TransmitIndex::Future(src), index)?
1490            };
1491            cx.host_resource_lower_own(id.rep(), None, None)
1492        }
1493        _ => func::bad_type_info(),
1494    }
1495}
1496
1497/// Checks that the host-owned read end `idx` may be lowered into a guest as a
1498/// future or stream with the payload type `payload`.
1499fn typecheck_lowered_transmit<U>(
1500    idx: HostResourceIndex,
1501    cx: &mut LowerContext<'_, U>,
1502    payload: Option<&InterfaceType>,
1503) -> Result<()> {
1504    let origin = cx.store.0.transmit_origin(idx)?;
1505    let dst = cx.instance_type();
1506    let equivalent = match origin {
1507        TransmitOrigin::Host { typecheck } => {
1508            return typecheck(payload, &dst).context("future or stream payload types differ");
1509        }
1510        TransmitOrigin::GuestFuture(id, ty) => {
1511            let src = InstanceType::new(cx.store.0.component_instance(id));
1512            types::FutureType::from(src.types[ty].ty, &src).equivalent_payload_guest(&dst, payload)
1513        }
1514        TransmitOrigin::GuestStream(id, ty) => {
1515            let src = InstanceType::new(cx.store.0.component_instance(id));
1516            types::StreamType::from(src.types[ty].ty, &src).equivalent_payload_guest(&dst, payload)
1517        }
1518    };
1519    if !equivalent {
1520        bail!("future or stream payload types differ");
1521    }
1522    Ok(())
1523}
1524
1525/// Transfer ownership of the read end of a future from the host to a guest.
1526/// `item_already_typechecked` should only be set to `true` when lowering a
1527/// `FutureReader<T>` which contains a `FutureProducer<Item = T>`.
1528pub(super) fn lower_future_to_index<U>(
1529    idx: HostResourceIndex,
1530    cx: &mut LowerContext<'_, U>,
1531    ty: InterfaceType,
1532    item_already_typechecked: bool,
1533) -> Result<u32> {
1534    match ty {
1535        InterfaceType::Future(dst) => {
1536            // Maintains the invariant on `lower_transmit_to_index`: if this read
1537            // end comes from a host `FutureProducer` `P`, then
1538            // `P::Item::typecheck` must pass for `ty`'s payload.
1539            //
1540            // If `item_already_typechecked` is true, then we're lowering a
1541            // `FutureReader<T>`, which means `FutureReader<T>::typecheck` has
1542            // already passed for `ty` (as required by the `Lower` trait). This
1543            // includes running `T::typecheck` for `ty`'s payload, and
1544            // since `P::Item = T` the invariant is already satisfied.
1545            if !item_already_typechecked {
1546                let types = cx.types;
1547                typecheck_lowered_transmit(idx, cx, types[types[dst].ty].payload.as_ref())?;
1548            }
1549            let id = cx.store.0.host_reader_take(idx)?;
1550            cx.instance_handle()
1551                .lower_transmit_to_index(cx.store.0, TransmitIndex::Future(dst), id)
1552        }
1553        _ => func::bad_type_info(),
1554    }
1555}
1556
1557// SAFETY: This relies on the `ComponentType` implementation for `u32` being
1558// safe and correct since we lift and lower future handles as `u32`s.
1559unsafe impl<T: ComponentType> ComponentType for FutureReader<T> {
1560    const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
1561
1562    type Lower = <u32 as func::ComponentType>::Lower;
1563
1564    fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
1565        match ty {
1566            InterfaceType::Future(ty) => {
1567                let ty = types.types[*ty].ty;
1568                types::typecheck_payload::<T>(types.types[ty].payload.as_ref(), types)
1569            }
1570            other => bail!("expected `future`, found `{}`", func::desc(other)),
1571        }
1572    }
1573}
1574
1575// SAFETY: See the comment on the `ComponentType` `impl` for this type.
1576unsafe impl<T: ComponentType> func::Lower for FutureReader<T> {
1577    fn linear_lower_to_flat<U>(
1578        &self,
1579        cx: &mut LowerContext<'_, U>,
1580        ty: InterfaceType,
1581        dst: &mut MaybeUninit<Self::Lower>,
1582    ) -> Result<()> {
1583        lower_future_to_index(self.idx, cx, ty, self.producer_type_match_checked)?
1584            .linear_lower_to_flat(cx, InterfaceType::U32, dst)
1585    }
1586
1587    fn linear_lower_to_memory<U>(
1588        &self,
1589        cx: &mut LowerContext<'_, U>,
1590        ty: InterfaceType,
1591        offset: usize,
1592    ) -> Result<()> {
1593        lower_future_to_index(self.idx, cx, ty, self.producer_type_match_checked)?
1594            .linear_lower_to_memory(cx, InterfaceType::U32, offset)
1595    }
1596}
1597
1598// SAFETY: See the comment on the `ComponentType` `impl` for this type.
1599unsafe impl<T: ComponentType> func::Lift for FutureReader<T> {
1600    fn linear_lift_from_flat(
1601        cx: &mut LiftContext<'_>,
1602        ty: InterfaceType,
1603        src: &Self::Lower,
1604    ) -> Result<Self> {
1605        let index = u32::linear_lift_from_flat(cx, InterfaceType::U32, src)?;
1606        Self::lift_from_index(cx, ty, index)
1607    }
1608
1609    fn linear_lift_from_memory(
1610        cx: &mut LiftContext<'_>,
1611        ty: InterfaceType,
1612        bytes: &[u8],
1613    ) -> Result<Self> {
1614        let index = u32::linear_lift_from_memory(cx, InterfaceType::U32, bytes)?;
1615        Self::lift_from_index(cx, ty, index)
1616    }
1617}
1618
1619/// A [`FutureReader`] paired with an [`Accessor`].
1620///
1621/// This is an RAII wrapper around [`FutureReader`] that ensures it is closed
1622/// when dropped. This can be created through [`GuardedFutureReader::new`] or
1623/// [`FutureReader::guard`].
1624///
1625/// [`Accessor`]: crate::component::Accessor
1626pub struct GuardedFutureReader<T, A>
1627where
1628    A: AsAccessor,
1629{
1630    // This field is `None` to implement the conversion from this guard back to
1631    // `FutureReader`. When `None` is seen in the destructor it will cause the
1632    // destructor to do nothing.
1633    reader: Option<FutureReader<T>>,
1634    accessor: A,
1635}
1636
1637impl<T, A> GuardedFutureReader<T, A>
1638where
1639    A: AsAccessor,
1640{
1641    /// Create a new `GuardedFutureReader` with the specified `accessor` and `reader`.
1642    ///
1643    /// # Panics
1644    ///
1645    /// Panics if [`Config::concurrency_support`] is not enabled.
1646    ///
1647    /// [`Config::concurrency_support`]: crate::Config::concurrency_support
1648    pub fn new(accessor: A, reader: FutureReader<T>) -> Self {
1649        assert!(
1650            accessor
1651                .as_accessor()
1652                .with(|a| a.as_context().0.concurrency_support())
1653        );
1654        Self {
1655            reader: Some(reader),
1656            accessor,
1657        }
1658    }
1659
1660    /// Extracts the underlying [`FutureReader`] from this guard, returning it
1661    /// back.
1662    pub fn into_future(self) -> FutureReader<T> {
1663        self.into()
1664    }
1665}
1666
1667impl<T, A> From<GuardedFutureReader<T, A>> for FutureReader<T>
1668where
1669    A: AsAccessor,
1670{
1671    fn from(mut guard: GuardedFutureReader<T, A>) -> Self {
1672        guard.reader.take().unwrap()
1673    }
1674}
1675
1676impl<T, A> Drop for GuardedFutureReader<T, A>
1677where
1678    A: AsAccessor,
1679{
1680    fn drop(&mut self) {
1681        if let Some(reader) = &mut self.reader {
1682            // Currently this can only fail if the future is closed twice or
1683            // the handle was cloned/closed elsewhere, all of which the embedder
1684            // is responsible for when using this guard. Hint during
1685            // development that's a bad idea, but otherwise ignore the error in
1686            // release builds.
1687            let result = reader.close_with(&self.accessor);
1688            debug_assert!(result.is_ok());
1689        }
1690    }
1691}
1692
1693/// Represents the readable end of a Component Model `stream`.
1694///
1695/// Note that `StreamReader` instances must be disposed of using `close`;
1696/// otherwise the in-store representation will leak and the writer end will hang
1697/// indefinitely.  Consider using [`GuardedStreamReader`] to ensure that
1698/// disposal happens automatically.
1699pub struct StreamReader<T> {
1700    idx: HostResourceIndex,
1701    // It has already been checked that this read end is connected to a
1702    // `StreamProducer` with `Item = T`.
1703    producer_type_match_checked: bool,
1704    _phantom: PhantomData<T>,
1705}
1706
1707impl<T> StreamReader<T> {
1708    /// Create a new stream with the specified producer.
1709    ///
1710    /// # Errors
1711    ///
1712    /// Returns an error if the resource table for this store is full or if
1713    /// [`Config::concurrency_support`] is not enabled.
1714    ///
1715    /// [`Config::concurrency_support`]: crate::Config::concurrency_support
1716    pub fn new<S: AsContextMut>(
1717        mut store: S,
1718        producer: impl StreamProducer<S::Data, Item = T>,
1719    ) -> Result<Self>
1720    where
1721        T: func::Lower + func::Lift + Send + Sync + 'static,
1722    {
1723        ensure!(
1724            store.as_context().0.concurrency_support(),
1725            "concurrency support is not enabled",
1726        );
1727        Ok(Self {
1728            idx: store
1729                .as_context_mut()
1730                .new_transmit(TransmitKind::Stream, producer)?,
1731            producer_type_match_checked: true,
1732            _phantom: PhantomData,
1733        })
1734    }
1735
1736    pub(super) fn new_(idx: HostResourceIndex) -> Self {
1737        Self {
1738            idx,
1739            // It isn't known if this stream comes from a `StreamProducer` with
1740            // `Item = T`.
1741            producer_type_match_checked: false,
1742            _phantom: PhantomData,
1743        }
1744    }
1745
1746    pub(super) fn idx(&self) -> HostResourceIndex {
1747        self.idx
1748    }
1749
1750    /// Attempt to consume this object by converting it into the specified type.
1751    ///
1752    /// This can be useful for "short-circuiting" host-to-host streams,
1753    /// bypassing the guest entirely.  For example, if a guest task returns a
1754    /// host-created stream and then exits, this function may be used to
1755    /// retrieve the write end, after which the guest instance and store may be
1756    /// disposed of if no longer needed.
1757    ///
1758    /// This will return `Ok(_)` if and only if the following conditions are
1759    /// met:
1760    ///
1761    /// - The stream was created by the host (i.e. not by the guest).
1762    ///
1763    /// - The `StreamProducer::try_into` function returns `Ok(_)` when given the
1764    /// producer provided to `StreamReader::new` when the stream was created,
1765    /// along with `TypeId::of::<V>()`.
1766    ///
1767    /// # Panics
1768    ///
1769    /// Panics if this stream has already been closed, or if this stream doesn't
1770    /// belong to the specified `store`.
1771    pub fn try_into<V: 'static>(mut self, mut store: impl AsContextMut) -> Result<V, Self> {
1772        let store = store.as_context_mut();
1773        let id = store.0.host_reader_rep(self.idx).unwrap();
1774        let state = store.0.concurrent_state_mut_already_forced_current_thread();
1775        let id = state.get_mut(id).unwrap().state;
1776        if let WriteState::HostReady { try_into, .. } = &state.get_mut(id).unwrap().write {
1777            match try_into(TypeId::of::<V>()) {
1778                Some(result) => {
1779                    self.close(store).unwrap();
1780                    Ok(*result.downcast::<V>().unwrap())
1781                }
1782                None => Err(self),
1783            }
1784        } else {
1785            Err(self)
1786        }
1787    }
1788
1789    /// Set the consumer that accepts the items delivered to this stream.
1790    ///
1791    /// # Errors
1792    ///
1793    /// Returns an error if this stream has already been closed or otherwise
1794    /// transferred (e.g. lowered into a guest), or if it does not belong to
1795    /// `store`.
1796    pub fn pipe<S: AsContextMut>(
1797        self,
1798        mut store: S,
1799        consumer: impl StreamConsumer<S::Data, Item = T>,
1800    ) -> Result<()>
1801    where
1802        T: 'static,
1803    {
1804        store
1805            .as_context_mut()
1806            .set_consumer(self.idx, TransmitKind::Stream, consumer)
1807    }
1808
1809    /// Transfer ownership of the read end of a stream from a guest to the host.
1810    fn lift_from_index(cx: &mut LiftContext<'_>, ty: InterfaceType, index: u32) -> Result<Self> {
1811        Ok(Self::new_(lift_index_to_stream(cx, ty, index)?))
1812    }
1813
1814    /// Close this `StreamReader`.
1815    ///
1816    /// This will signal that this portion of the stream is closed causing all
1817    /// future writes to return immediately with "DROPPED".
1818    ///
1819    /// # Errors
1820    ///
1821    /// Returns an error if this stream has already been closed or otherwise
1822    /// transferred (e.g. lowered into a guest), or if it does not belong to
1823    /// `store`.
1824    pub fn close(&mut self, mut store: impl AsContextMut) -> Result<()> {
1825        stream_close(store.as_context_mut().0, self.idx)
1826    }
1827
1828    /// Convenience method around [`Self::close`].
1829    pub fn close_with(&mut self, accessor: impl AsAccessor) -> Result<()> {
1830        accessor.as_accessor().with(|access| self.close(access))
1831    }
1832
1833    /// Returns a [`GuardedStreamReader`] which will auto-close this stream on
1834    /// drop and clean it up from the store.
1835    ///
1836    /// Note that the `accessor` provided must own this future and is
1837    /// additionally transferred to the `GuardedStreamReader` return value.
1838    pub fn guard<A>(self, accessor: A) -> GuardedStreamReader<T, A>
1839    where
1840        A: AsAccessor,
1841    {
1842        GuardedStreamReader::new(accessor, self)
1843    }
1844
1845    /// Attempts to convert this [`StreamReader<T>`] to a [`StreamAny`].
1846    ///
1847    /// # Errors
1848    ///
1849    /// This function will return an error if `self` does not belong to
1850    /// `store`.
1851    pub fn try_into_stream_any(self, store: impl AsContextMut) -> Result<StreamAny>
1852    where
1853        T: ComponentType + 'static,
1854    {
1855        StreamAny::try_from_stream_reader(store, self)
1856    }
1857
1858    /// Attempts to convert a [`StreamAny`] into a [`StreamReader<T>`].
1859    ///
1860    /// # Errors
1861    ///
1862    /// This function will fail if `T` doesn't match the type of the value that
1863    /// `stream` is sending.
1864    pub fn try_from_stream_any(stream: StreamAny) -> Result<Self>
1865    where
1866        T: ComponentType + 'static,
1867    {
1868        stream.try_into_stream_reader()
1869    }
1870}
1871
1872impl<T> fmt::Debug for StreamReader<T> {
1873    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1874        f.debug_struct("StreamReader")
1875            .field("idx", &self.idx)
1876            .finish()
1877    }
1878}
1879
1880pub(super) fn stream_close(store: &mut StoreOpaque, idx: HostResourceIndex) -> Result<()> {
1881    let id = store.host_reader_take(idx)?;
1882    store.host_drop_reader(id, TransmitKind::Stream)
1883}
1884
1885/// Transfer ownership of the read end of a stream from a guest to the host.
1886pub(super) fn lift_index_to_stream(
1887    cx: &mut LiftContext<'_>,
1888    ty: InterfaceType,
1889    index: u32,
1890) -> Result<HostResourceIndex> {
1891    match ty {
1892        InterfaceType::Stream(src) => {
1893            let id = {
1894                let (state, instance) = cx.concurrent_state_and_instance_mut();
1895                lift_index_to_transmit(instance, state, TransmitIndex::Stream(src), index)?
1896            };
1897            cx.host_resource_lower_own(id.rep(), None, None)
1898        }
1899        _ => func::bad_type_info(),
1900    }
1901}
1902
1903/// Transfer ownership of the read end of a stream from the host to a guest.
1904/// `item_already_typechecked` should only be set to `true` when lowering a
1905/// `StreamReader<T>` which contains a `StreamProducer<Item = T>`.
1906pub(super) fn lower_stream_to_index<U>(
1907    idx: HostResourceIndex,
1908    cx: &mut LowerContext<'_, U>,
1909    ty: InterfaceType,
1910    item_already_typechecked: bool,
1911) -> Result<u32> {
1912    match ty {
1913        InterfaceType::Stream(dst) => {
1914            // Maintains the invariant on `lower_transmit_to_index`: if this read
1915            // end comes from a host `StreamProducer` `P`, then
1916            // `P::Item::typecheck` must pass for `ty`'s payload.
1917            //
1918            // If `item_already_typechecked` is true, then we're lowering a
1919            // `StreamReader<T>`, which means `StreamReader<T>::typecheck` has
1920            // already passed for `ty` (as required by the `Lower` trait). This
1921            // includes running `T::typecheck` for `ty`'s payload, and
1922            // since `P::Item = T` the invariant is already satisfied.
1923            if !item_already_typechecked {
1924                let types = cx.types;
1925                typecheck_lowered_transmit(idx, cx, types[types[dst].ty].payload.as_ref())?;
1926            }
1927            let id = cx.store.0.host_reader_take(idx)?;
1928            cx.instance_handle()
1929                .lower_transmit_to_index(cx.store.0, TransmitIndex::Stream(dst), id)
1930        }
1931        _ => func::bad_type_info(),
1932    }
1933}
1934
1935// SAFETY: This relies on the `ComponentType` implementation for `u32` being
1936// safe and correct since we lift and lower stream handles as `u32`s.
1937unsafe impl<T: ComponentType> ComponentType for StreamReader<T> {
1938    const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
1939
1940    type Lower = <u32 as func::ComponentType>::Lower;
1941
1942    fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
1943        match ty {
1944            InterfaceType::Stream(ty) => {
1945                let ty = types.types[*ty].ty;
1946                types::typecheck_payload::<T>(types.types[ty].payload.as_ref(), types)
1947            }
1948            other => bail!("expected `stream`, found `{}`", func::desc(other)),
1949        }
1950    }
1951}
1952
1953// SAFETY: See the comment on the `ComponentType` `impl` for this type.
1954unsafe impl<T: ComponentType> func::Lower for StreamReader<T> {
1955    fn linear_lower_to_flat<U>(
1956        &self,
1957        cx: &mut LowerContext<'_, U>,
1958        ty: InterfaceType,
1959        dst: &mut MaybeUninit<Self::Lower>,
1960    ) -> Result<()> {
1961        lower_stream_to_index(self.idx, cx, ty, self.producer_type_match_checked)?
1962            .linear_lower_to_flat(cx, InterfaceType::U32, dst)
1963    }
1964
1965    fn linear_lower_to_memory<U>(
1966        &self,
1967        cx: &mut LowerContext<'_, U>,
1968        ty: InterfaceType,
1969        offset: usize,
1970    ) -> Result<()> {
1971        lower_stream_to_index(self.idx, cx, ty, self.producer_type_match_checked)?
1972            .linear_lower_to_memory(cx, InterfaceType::U32, offset)
1973    }
1974}
1975
1976// SAFETY: See the comment on the `ComponentType` `impl` for this type.
1977unsafe impl<T: ComponentType> func::Lift for StreamReader<T> {
1978    fn linear_lift_from_flat(
1979        cx: &mut LiftContext<'_>,
1980        ty: InterfaceType,
1981        src: &Self::Lower,
1982    ) -> Result<Self> {
1983        let index = u32::linear_lift_from_flat(cx, InterfaceType::U32, src)?;
1984        Self::lift_from_index(cx, ty, index)
1985    }
1986
1987    fn linear_lift_from_memory(
1988        cx: &mut LiftContext<'_>,
1989        ty: InterfaceType,
1990        bytes: &[u8],
1991    ) -> Result<Self> {
1992        let index = u32::linear_lift_from_memory(cx, InterfaceType::U32, bytes)?;
1993        Self::lift_from_index(cx, ty, index)
1994    }
1995}
1996
1997/// A [`StreamReader`] paired with an [`Accessor`].
1998///
1999/// This is an RAII wrapper around [`StreamReader`] that ensures it is closed
2000/// when dropped. This can be created through [`GuardedStreamReader::new`] or
2001/// [`StreamReader::guard`].
2002///
2003/// [`Accessor`]: crate::component::Accessor
2004pub struct GuardedStreamReader<T, A>
2005where
2006    A: AsAccessor,
2007{
2008    // This field is `None` to implement the conversion from this guard back to
2009    // `StreamReader`. When `None` is seen in the destructor it will cause the
2010    // destructor to do nothing.
2011    reader: Option<StreamReader<T>>,
2012    accessor: A,
2013}
2014
2015impl<T, A> GuardedStreamReader<T, A>
2016where
2017    A: AsAccessor,
2018{
2019    /// Create a new `GuardedStreamReader` with the specified `accessor` and
2020    /// `reader`.
2021    ///
2022    /// # Panics
2023    ///
2024    /// Panics if [`Config::concurrency_support`] is not enabled.
2025    ///
2026    /// [`Config::concurrency_support`]: crate::Config::concurrency_support
2027    pub fn new(accessor: A, reader: StreamReader<T>) -> Self {
2028        assert!(
2029            accessor
2030                .as_accessor()
2031                .with(|a| a.as_context().0.concurrency_support())
2032        );
2033        Self {
2034            reader: Some(reader),
2035            accessor,
2036        }
2037    }
2038
2039    /// Extracts the underlying [`StreamReader`] from this guard, returning it
2040    /// back.
2041    pub fn into_stream(self) -> StreamReader<T> {
2042        self.into()
2043    }
2044}
2045
2046impl<T, A> From<GuardedStreamReader<T, A>> for StreamReader<T>
2047where
2048    A: AsAccessor,
2049{
2050    fn from(mut guard: GuardedStreamReader<T, A>) -> Self {
2051        guard.reader.take().unwrap()
2052    }
2053}
2054
2055impl<T, A> Drop for GuardedStreamReader<T, A>
2056where
2057    A: AsAccessor,
2058{
2059    fn drop(&mut self) {
2060        if let Some(reader) = &mut self.reader {
2061            // See comments in `Drop for GuardedFutureReader` for this assert.
2062            let result = reader.close_with(&self.accessor);
2063            debug_assert!(result.is_ok());
2064        }
2065    }
2066}
2067
2068/// Represents a Component Model `error-context`.
2069pub struct ErrorContext {
2070    rep: u32,
2071}
2072
2073impl ErrorContext {
2074    pub(crate) fn new(rep: u32) -> Self {
2075        Self { rep }
2076    }
2077
2078    /// Convert this `ErrorContext` into a [`Val`].
2079    pub fn into_val(self) -> Val {
2080        Val::ErrorContext(ErrorContextAny(self.rep))
2081    }
2082
2083    /// Attempt to convert the specified [`Val`] to a `ErrorContext`.
2084    pub fn from_val(_: impl AsContextMut, value: &Val) -> Result<Self> {
2085        let Val::ErrorContext(ErrorContextAny(rep)) = value else {
2086            bail!("expected `error-context`; got `{}`", value.desc());
2087        };
2088        Ok(Self::new(*rep))
2089    }
2090
2091    fn lift_from_index(cx: &mut LiftContext<'_>, ty: InterfaceType, index: u32) -> Result<Self> {
2092        match ty {
2093            InterfaceType::ErrorContext(src) => {
2094                let rep = cx
2095                    .instance_mut()
2096                    .table_for_error_context(src)
2097                    .error_context_rep(index)?;
2098
2099                Ok(Self { rep })
2100            }
2101            _ => func::bad_type_info(),
2102        }
2103    }
2104}
2105
2106pub(crate) fn lower_error_context_to_index<U>(
2107    rep: u32,
2108    cx: &mut LowerContext<'_, U>,
2109    ty: InterfaceType,
2110) -> Result<u32> {
2111    match ty {
2112        InterfaceType::ErrorContext(dst) => {
2113            let (instance, counter) = cx
2114                .instance_handle()
2115                .id()
2116                .get_mut_and_handle_counter(cx.store.0);
2117            instance
2118                .error_context_table_insertion(dst, counter)
2119                .error_context(rep)
2120        }
2121        _ => func::bad_type_info(),
2122    }
2123}
2124// SAFETY: This relies on the `ComponentType` implementation for `u32` being
2125// safe and correct since we lift and lower future handles as `u32`s.
2126unsafe impl func::ComponentType for ErrorContext {
2127    const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
2128
2129    type Lower = <u32 as func::ComponentType>::Lower;
2130
2131    fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
2132        match ty {
2133            InterfaceType::ErrorContext(_) => Ok(()),
2134            other => bail!("expected `error`, found `{}`", func::desc(other)),
2135        }
2136    }
2137}
2138
2139// SAFETY: See the comment on the `ComponentType` `impl` for this type.
2140unsafe impl func::Lower for ErrorContext {
2141    fn linear_lower_to_flat<T>(
2142        &self,
2143        cx: &mut LowerContext<'_, T>,
2144        ty: InterfaceType,
2145        dst: &mut MaybeUninit<Self::Lower>,
2146    ) -> Result<()> {
2147        lower_error_context_to_index(self.rep, cx, ty)?.linear_lower_to_flat(
2148            cx,
2149            InterfaceType::U32,
2150            dst,
2151        )
2152    }
2153
2154    fn linear_lower_to_memory<T>(
2155        &self,
2156        cx: &mut LowerContext<'_, T>,
2157        ty: InterfaceType,
2158        offset: usize,
2159    ) -> Result<()> {
2160        lower_error_context_to_index(self.rep, cx, ty)?.linear_lower_to_memory(
2161            cx,
2162            InterfaceType::U32,
2163            offset,
2164        )
2165    }
2166}
2167
2168// SAFETY: See the comment on the `ComponentType` `impl` for this type.
2169unsafe impl func::Lift for ErrorContext {
2170    fn linear_lift_from_flat(
2171        cx: &mut LiftContext<'_>,
2172        ty: InterfaceType,
2173        src: &Self::Lower,
2174    ) -> Result<Self> {
2175        let index = u32::linear_lift_from_flat(cx, InterfaceType::U32, src)?;
2176        Self::lift_from_index(cx, ty, index)
2177    }
2178
2179    fn linear_lift_from_memory(
2180        cx: &mut LiftContext<'_>,
2181        ty: InterfaceType,
2182        bytes: &[u8],
2183    ) -> Result<Self> {
2184        let index = u32::linear_lift_from_memory(cx, InterfaceType::U32, bytes)?;
2185        Self::lift_from_index(cx, ty, index)
2186    }
2187}
2188
2189/// Represents the read or write end of a stream or future.
2190pub(super) struct TransmitHandle {
2191    pub(super) common: WaitableCommon,
2192    /// See `TransmitState`
2193    state: TableId<TransmitState>,
2194}
2195
2196impl TransmitHandle {
2197    fn new(state: TableId<TransmitState>) -> Self {
2198        Self {
2199            common: WaitableCommon::default(),
2200            state,
2201        }
2202    }
2203}
2204
2205impl TableDebug for TransmitHandle {
2206    fn type_name() -> &'static str {
2207        "TransmitHandle"
2208    }
2209}
2210
2211/// Represents the state of a stream or future.
2212struct TransmitState {
2213    /// The write end of the stream or future.
2214    write_handle: TableId<TransmitHandle>,
2215    /// The read end of the stream or future.
2216    read_handle: TableId<TransmitHandle>,
2217    /// See `WriteState`
2218    write: WriteState,
2219    /// See `ReadState`
2220    read: ReadState,
2221    /// Whether further values may be transmitted via this stream or future.
2222    done: bool,
2223    /// The original creator of this stream or future, whose payload type is
2224    /// checked against a guest's whenever the host lowers the read end into
2225    /// that guest, and which `{Future,Stream}Any` use as their initial type.
2226    pub(super) origin: TransmitOrigin,
2227}
2228
2229#[derive(Copy, Clone)]
2230pub(super) enum TransmitOrigin {
2231    Host {
2232        /// Type-checks the producer's items against the guest-supplied payload.
2233        typecheck: fn(Option<&InterfaceType>, &InstanceType<'_>) -> Result<()>,
2234    },
2235    GuestFuture(ComponentInstanceId, TypeFutureTableIndex),
2236    GuestStream(ComponentInstanceId, TypeStreamTableIndex),
2237}
2238
2239impl TransmitState {
2240    fn new(origin: TransmitOrigin) -> Self {
2241        Self {
2242            write_handle: TableId::new(u32::MAX),
2243            read_handle: TableId::new(u32::MAX),
2244            read: ReadState::Open,
2245            write: WriteState::Open,
2246            done: false,
2247            origin,
2248        }
2249    }
2250}
2251
2252impl TableDebug for TransmitState {
2253    fn type_name() -> &'static str {
2254        "TransmitState"
2255    }
2256}
2257
2258impl TransmitOrigin {
2259    fn guest(id: ComponentInstanceId, index: TransmitIndex) -> Self {
2260        match index {
2261            TransmitIndex::Future(ty) => TransmitOrigin::GuestFuture(id, ty),
2262            TransmitIndex::Stream(ty) => TransmitOrigin::GuestStream(id, ty),
2263        }
2264    }
2265}
2266
2267type PollStream = Box<
2268    dyn Fn() -> Pin<Box<dyn Future<Output = Result<StreamResult>> + Send + 'static>> + Send + Sync,
2269>;
2270
2271type TryInto = Box<dyn Fn(TypeId) -> Option<Box<dyn Any>> + Send + Sync>;
2272
2273/// Represents the state of the write end of a stream or future.
2274enum WriteState {
2275    /// The write end is open, but no write is pending.
2276    Open,
2277    /// The write end is owned by a guest task and a write is pending.
2278    GuestReady {
2279        instance: Instance,
2280        caller: RuntimeComponentInstanceIndex,
2281        ty: TransmitIndex,
2282        flat_abi: Option<FlatAbi>,
2283        options: OptionsIndex,
2284        address: usize,
2285        count: ItemCount,
2286        handle: u32,
2287    },
2288    /// The write end is owned by the host, which is ready to produce items.
2289    HostReady {
2290        produce: PollStream,
2291        try_into: TryInto,
2292        guest_offset: ItemCount,
2293        cancel: bool,
2294        cancel_waker: Option<Waker>,
2295    },
2296    /// The write end has been dropped.
2297    Dropped,
2298}
2299
2300impl fmt::Debug for WriteState {
2301    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2302        match self {
2303            Self::Open => f.debug_tuple("Open").finish(),
2304            Self::GuestReady { .. } => f.debug_tuple("GuestReady").finish(),
2305            Self::HostReady { .. } => f.debug_tuple("HostReady").finish(),
2306            Self::Dropped => f.debug_tuple("Dropped").finish(),
2307        }
2308    }
2309}
2310
2311/// Represents the state of the read end of a stream or future.
2312enum ReadState {
2313    /// The read end is open, but no read is pending.
2314    Open,
2315    /// The read end is owned by a guest task and a read is pending.
2316    GuestReady {
2317        ty: TransmitIndex,
2318        caller_instance: RuntimeComponentInstanceIndex,
2319        flat_abi: Option<FlatAbi>,
2320        instance: Instance,
2321        options: OptionsIndex,
2322        address: usize,
2323        count: ItemCount,
2324        handle: u32,
2325    },
2326    /// The read end is owned by a host task, and it is ready to consume items.
2327    HostReady {
2328        consume: PollStream,
2329        guest_offset: ItemCount,
2330        cancel: bool,
2331        cancel_waker: Option<Waker>,
2332    },
2333    /// Both the read and write ends are owned by the host.
2334    HostToHost {
2335        accept: Box<
2336            dyn for<'a> Fn(
2337                    &'a mut UntypedWriteBuffer<'a>,
2338                )
2339                    -> Pin<Box<dyn Future<Output = Result<StreamResult>> + Send + 'a>>
2340                + Send
2341                + Sync,
2342        >,
2343        buffer: Vec<u8>,
2344        limit: usize,
2345    },
2346    /// The read end has been dropped.
2347    Dropped,
2348}
2349
2350impl fmt::Debug for ReadState {
2351    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2352        match self {
2353            Self::Open => f.debug_tuple("Open").finish(),
2354            Self::GuestReady { .. } => f.debug_tuple("GuestReady").finish(),
2355            Self::HostReady { .. } => f.debug_tuple("HostReady").finish(),
2356            Self::HostToHost { .. } => f.debug_tuple("HostToHost").finish(),
2357            Self::Dropped => f.debug_tuple("Dropped").finish(),
2358        }
2359    }
2360}
2361
2362fn return_code(
2363    kind: TransmitKind,
2364    state: StreamResult,
2365    count: ItemCount,
2366    cancel: bool,
2367) -> Result<ReturnCode> {
2368    // The spec says that for streams the priority of return codes is
2369    //
2370    //   DROPPED > CANCELLED > COMPLETED
2371    //
2372    // where for futures it's
2373    //
2374    //   COMPLETED > DROPPED > CANCELLED
2375    //
2376    // which is what's implemented here.
2377    Ok(match (state, kind) {
2378        (StreamResult::Dropped, _) => ReturnCode::Dropped(count),
2379        (StreamResult::Completed, TransmitKind::Stream) if cancel => ReturnCode::Cancelled(count),
2380        (StreamResult::Completed, _) => ReturnCode::completed(kind, count),
2381        (StreamResult::Cancelled, _) => ReturnCode::Cancelled(count),
2382    })
2383}
2384
2385fn settle_host_read(
2386    transmit: &mut TransmitState,
2387    kind: TransmitKind,
2388    state: StreamResult,
2389) -> Result<ReturnCode> {
2390    let ReadState::HostReady {
2391        consume,
2392        guest_offset,
2393        cancel,
2394        ..
2395    } = mem::replace(&mut transmit.read, ReadState::Open)
2396    else {
2397        bail_bug!("expected ReadState::HostReady")
2398    };
2399    let code = return_code(kind, state, guest_offset, cancel)?;
2400    transmit.read = match state {
2401        StreamResult::Dropped => ReadState::Dropped,
2402        StreamResult::Completed | StreamResult::Cancelled => ReadState::HostReady {
2403            consume,
2404            guest_offset: ItemCount::ZERO,
2405            cancel: false,
2406            cancel_waker: None,
2407        },
2408    };
2409    Ok(code)
2410}
2411
2412fn settle_host_write(
2413    transmit: &mut TransmitState,
2414    kind: TransmitKind,
2415    state: StreamResult,
2416) -> Result<ReturnCode> {
2417    let WriteState::HostReady {
2418        produce,
2419        try_into,
2420        guest_offset,
2421        cancel,
2422        ..
2423    } = mem::replace(&mut transmit.write, WriteState::Open)
2424    else {
2425        bail_bug!("expected WriteState::HostReady")
2426    };
2427    let code = return_code(kind, state, guest_offset, cancel)?;
2428    transmit.write = match state {
2429        StreamResult::Dropped => WriteState::Dropped,
2430        StreamResult::Completed | StreamResult::Cancelled => WriteState::HostReady {
2431            produce,
2432            try_into,
2433            guest_offset: ItemCount::ZERO,
2434            cancel: false,
2435            cancel_waker: None,
2436        },
2437    };
2438    Ok(code)
2439}
2440
2441impl StoreOpaque {
2442    fn pipe_from_guest(
2443        &mut self,
2444        kind: TransmitKind,
2445        id: TableId<TransmitState>,
2446        future: Pin<Box<dyn Future<Output = Result<StreamResult>> + Send + 'static>>,
2447    ) {
2448        let future = async move {
2449            let stream_state = future.await?;
2450            tls::get(|store| {
2451                let state = store.concurrent_state_mut()?;
2452                let transmit = state.get_mut(id)?;
2453                let code = settle_host_read(transmit, kind, stream_state)?;
2454                let WriteState::GuestReady { ty, handle, .. } =
2455                    mem::replace(&mut transmit.write, WriteState::Open)
2456                else {
2457                    bail_bug!("expected WriteState::GuestReady")
2458                };
2459                state.send_write_result(ty, id, handle, code)?;
2460                Ok(())
2461            })
2462        };
2463
2464        self.concurrent_state_mut_already_forced_current_thread()
2465            .push_future(future.boxed());
2466    }
2467
2468    fn pipe_to_guest(
2469        &mut self,
2470        kind: TransmitKind,
2471        id: TableId<TransmitState>,
2472        future: Pin<Box<dyn Future<Output = Result<StreamResult>> + Send + 'static>>,
2473    ) {
2474        let future = async move {
2475            let stream_state = future.await?;
2476            tls::get(|store| {
2477                let state = store.concurrent_state_mut()?;
2478                let transmit = state.get_mut(id)?;
2479                let code = settle_host_write(transmit, kind, stream_state)?;
2480                let ReadState::GuestReady { ty, handle, .. } =
2481                    mem::replace(&mut transmit.read, ReadState::Open)
2482                else {
2483                    bail_bug!("expected ReadState::GuestReady")
2484                };
2485                state.send_read_result(ty, id, handle, code)?;
2486                Ok(())
2487            })
2488        };
2489
2490        self.concurrent_state_mut_already_forced_current_thread()
2491            .push_future(future.boxed());
2492    }
2493
2494    /// Drop the read end of a stream or future read from the host.
2495    fn host_drop_reader(&mut self, id: TableId<TransmitHandle>, kind: TransmitKind) -> Result<()> {
2496        let state = self.concurrent_state_mut()?;
2497        Waitable::Transmit(id).join(state, None)?;
2498        let transmit_id = state.get_mut(id)?.state;
2499        let transmit = state
2500            .get_mut(transmit_id)
2501            .with_context(|| format!("error closing reader {transmit_id:?}"))?;
2502        log::trace!(
2503            "host_drop_reader state {transmit_id:?}; read state {:?} write state {:?}",
2504            transmit.read,
2505            transmit.write
2506        );
2507
2508        transmit.read = ReadState::Dropped;
2509
2510        // If the write end is already dropped, it should stay dropped,
2511        // otherwise, it should be opened.
2512        let new_state = if let WriteState::Dropped = &transmit.write {
2513            WriteState::Dropped
2514        } else {
2515            WriteState::Open
2516        };
2517
2518        let write_handle = transmit.write_handle;
2519        let done = transmit.done;
2520
2521        match mem::replace(&mut transmit.write, new_state) {
2522            // If a guest is waiting to write, notify it that the read end has
2523            // been dropped.
2524            WriteState::GuestReady { ty, handle, .. } => {
2525                state.update_event(
2526                    write_handle.rep(),
2527                    match ty {
2528                        TransmitIndex::Future(ty) => Event::FutureWrite {
2529                            code: ReturnCode::Dropped(ItemCount::ZERO),
2530                            pending: Some((ty, handle)),
2531                        },
2532                        TransmitIndex::Stream(ty) => Event::StreamWrite {
2533                            code: ReturnCode::Dropped(ItemCount::ZERO),
2534                            pending: Some((ty, handle)),
2535                        },
2536                    },
2537                )?;
2538            }
2539
2540            // Otherwise notify the idle write end that the read end has been
2541            // dropped, unless it's already done.
2542            WriteState::Open if done => {}
2543            WriteState::Open => {
2544                state.update_event(
2545                    write_handle.rep(),
2546                    match kind {
2547                        TransmitKind::Future => Event::FutureWrite {
2548                            code: ReturnCode::Dropped(ItemCount::ZERO),
2549                            pending: None,
2550                        },
2551                        TransmitKind::Stream => Event::StreamWrite {
2552                            code: ReturnCode::Dropped(ItemCount::ZERO),
2553                            pending: None,
2554                        },
2555                    },
2556                )?;
2557            }
2558
2559            // If the writer has already been dropped, then this cleans out the
2560            // state that the reader is using. If the write is host-owned then
2561            // by cleaning this out we run the host's `Drop` implementation
2562            // which notifies it of this drop.
2563            WriteState::Dropped | WriteState::HostReady { .. } => {
2564                log::trace!("host_drop_reader delete {transmit_id:?}");
2565                state.delete_transmit(transmit_id)?;
2566            }
2567        }
2568        Ok(())
2569    }
2570
2571    /// Drop the write end of a stream or future read from the host.
2572    fn host_drop_writer(
2573        &mut self,
2574        id: TableId<TransmitHandle>,
2575        on_drop_open: Option<fn() -> Result<()>>,
2576    ) -> Result<()> {
2577        let state = self.concurrent_state_mut()?;
2578        Waitable::Transmit(id).join(state, None)?;
2579        let transmit_id = state.get_mut(id)?.state;
2580        let transmit = state
2581            .get_mut(transmit_id)
2582            .with_context(|| format!("error closing writer {transmit_id:?}"))?;
2583        log::trace!(
2584            "host_drop_writer state {transmit_id:?}; read state {:?} writer state {:?}",
2585            transmit.read,
2586            transmit.write
2587        );
2588
2589        // Existing queued transmits must be updated with information for the impending writer closure
2590        match &mut transmit.write {
2591            WriteState::GuestReady { .. } => {
2592                bail_bug!("can't call `host_drop_writer` on a guest-owned writer");
2593            }
2594            WriteState::HostReady { .. } => {}
2595            v @ WriteState::Open => {
2596                if let (Some(on_drop_open), false) = (on_drop_open, transmit.done) {
2597                    on_drop_open()?;
2598                } else {
2599                    *v = WriteState::Dropped;
2600                }
2601            }
2602            WriteState::Dropped => bail_bug!("write state is already dropped"),
2603        }
2604
2605        let transmit = self.concurrent_state_mut()?.get_mut(transmit_id)?;
2606
2607        // If the existing read state is dropped, then there's nothing to read
2608        // and we can keep it that way.
2609        //
2610        // If the read state was any other state, then we must set the new state to open
2611        // to indicate that there *is* data to be read
2612        let new_state = if let ReadState::Dropped = &transmit.read {
2613            ReadState::Dropped
2614        } else {
2615            ReadState::Open
2616        };
2617
2618        let read_handle = transmit.read_handle;
2619        let done = transmit.done;
2620
2621        // Swap in the new read state
2622        match mem::replace(&mut transmit.read, new_state) {
2623            // If the guest was ready to read, then we cannot drop the reader (or writer);
2624            // we must deliver the event, and update the state associated with the handle to
2625            // represent that a read must be performed
2626            ReadState::GuestReady { ty, handle, .. } => {
2627                // Ensure the final read of the guest is queued, with appropriate closure indicator
2628                self.concurrent_state_mut()?.update_event(
2629                    read_handle.rep(),
2630                    match ty {
2631                        TransmitIndex::Future(ty) => Event::FutureRead {
2632                            code: ReturnCode::Dropped(ItemCount::ZERO),
2633                            pending: Some((ty, handle)),
2634                        },
2635                        TransmitIndex::Stream(ty) => Event::StreamRead {
2636                            code: ReturnCode::Dropped(ItemCount::ZERO),
2637                            pending: Some((ty, handle)),
2638                        },
2639                    },
2640                )?;
2641            }
2642
2643            // Notify the read-end of the drop, unless we're already done.
2644            ReadState::Open if done => {}
2645            ReadState::Open => {
2646                self.concurrent_state_mut()?.update_event(
2647                    read_handle.rep(),
2648                    match on_drop_open {
2649                        Some(_) => Event::FutureRead {
2650                            code: ReturnCode::Dropped(ItemCount::ZERO),
2651                            pending: None,
2652                        },
2653                        None => Event::StreamRead {
2654                            code: ReturnCode::Dropped(ItemCount::ZERO),
2655                            pending: None,
2656                        },
2657                    },
2658                )?;
2659            }
2660
2661            // If the read state was already dropped, then we can remove the
2662            // transmit state completely (both writer and reader have been
2663            // dropped). If the read state is host-owned then it's additionally
2664            // deleted here as a notification that the read end has gone away.
2665            // Running the host's `Drop` implementation is what notifies it of
2666            // this event.
2667            ReadState::Dropped | ReadState::HostReady { .. } | ReadState::HostToHost { .. } => {
2668                log::trace!("host_drop_writer delete {transmit_id:?}");
2669                self.concurrent_state_mut()?.delete_transmit(transmit_id)?;
2670            }
2671        }
2672        Ok(())
2673    }
2674
2675    pub(super) fn transmit_origin(&mut self, idx: HostResourceIndex) -> Result<TransmitOrigin> {
2676        let id = self.host_reader_rep(idx)?;
2677        let state = self.concurrent_state_mut()?;
2678        let state_id = state.get_mut(id)?.state;
2679        Ok(state.get_mut(state_id)?.origin)
2680    }
2681
2682    /// Insert `id` within the store, returning a `HostResourceIndex` suitable
2683    /// to re-acquire it via the `host_reader_take` and `host_reader_rep`
2684    /// methods below.
2685    fn host_reader_insert(&mut self, id: TableId<TransmitHandle>) -> Result<HostResourceIndex> {
2686        HostResourceTables::new_host(self)?.host_resource_lower_own(id.rep(), None, None)
2687    }
2688
2689    /// Removes `idx` from the store, returning the internal `id` it was
2690    /// created with.
2691    fn host_reader_take(&mut self, idx: HostResourceIndex) -> Result<TableId<TransmitHandle>> {
2692        let rep = HostResourceTables::new_host(self)?.host_resource_lift_own(idx)?;
2693        Ok(TableId::new(rep))
2694    }
2695
2696    /// Looks up `idx` within the store, returning the internal `id` it was
2697    /// created with.
2698    fn host_reader_rep(&mut self, idx: HostResourceIndex) -> Result<TableId<TransmitHandle>> {
2699        let rep = HostResourceTables::new_host(self)?.host_resource_rep(idx)?;
2700        Ok(TableId::new(rep))
2701    }
2702}
2703
2704struct ProducerState<P, B> {
2705    producer: P,
2706    buffer: B,
2707    dropped: bool,
2708}
2709
2710impl<T> StoreContextMut<'_, T> {
2711    fn new_transmit<P: StreamProducer<T>>(
2712        mut self,
2713        kind: TransmitKind,
2714        producer: P,
2715    ) -> Result<HostResourceIndex>
2716    where
2717        P::Item: func::Lower,
2718    {
2719        let token = StoreToken::new(self.as_context_mut());
2720        let state = self.0.concurrent_state_mut()?;
2721        let (_, read) = state.new_transmit(TransmitOrigin::Host {
2722            typecheck: types::typecheck_payload::<P::Item>,
2723        })?;
2724        let producer = Arc::new(LockedState::new(ProducerState {
2725            producer: Box::pin(producer),
2726            buffer: P::Buffer::default(),
2727            dropped: false,
2728        }));
2729        let id = state.get_mut(read)?.state;
2730        let produce = Box::new({
2731            let producer = producer.clone();
2732            move || {
2733                let producer = producer.clone();
2734                async move {
2735                    let mut state = producer.take()?;
2736                    let ProducerState { producer: mine, buffer, dropped } = &mut *state;
2737
2738                    let (result, cancelled) = if buffer.remaining().is_empty() {
2739                        future::poll_fn(|cx| {
2740                            tls::get(|store| {
2741                                let transmit = store.concurrent_state_mut()?.get_mut(id)?;
2742
2743                                let &WriteState::HostReady { cancel, .. } = &transmit.write else {
2744                                    bail_bug!("expected WriteState::HostReady")
2745                                };
2746
2747                                let mut host_written = 0;
2748                                let mut host_buffer =
2749                                    if let ReadState::HostToHost { buffer, .. } = &mut transmit.read {
2750                                        Some(mem::take(buffer))
2751                                    } else {
2752                                        None
2753                                    };
2754
2755                                let poll = mine.as_mut().poll_produce(
2756                                    cx,
2757                                    token.as_context_mut(store),
2758                                    Destination {
2759                                        id,
2760                                        buffer,
2761                                        host_buffer: host_buffer.as_mut().map(|b| {
2762                                            HostBuffer {
2763                                                dst: b,
2764                                                marked_written: &mut host_written,
2765                                            }
2766                                        }),
2767                                        _phantom: PhantomData,
2768                                    },
2769                                    cancel,
2770                                );
2771
2772                                let transmit = store.concurrent_state_mut()?.get_mut(id)?;
2773
2774                                let host_offset = if let (
2775                                    Some(host_buffer),
2776                                    ReadState::HostToHost { buffer, limit, .. },
2777                                ) = (host_buffer, &mut transmit.read)
2778                                {
2779                                    *limit = host_written;
2780                                    *buffer = host_buffer;
2781                                    *limit
2782                                } else {
2783                                    0
2784                                };
2785
2786                                {
2787                                    let WriteState::HostReady {
2788                                        guest_offset,
2789                                        cancel_waker,
2790                                        ..
2791                                    } = &mut transmit.write
2792                                    else {
2793                                        bail_bug!("expected WriteState::HostReady")
2794                                    };
2795
2796                                    *cancel_waker = if poll.is_pending() {
2797                                        if !buffer.remaining().is_empty()
2798                                            || *guest_offset > 0
2799                                            || host_offset > 0
2800                                        {
2801                                            bail!(
2802                                                "StreamProducer::poll_produce returned Poll::Pending \
2803                                                 after producing at least one item"
2804                                            )
2805                                        }
2806                                        Some(cx.waker().clone())
2807                                    } else {
2808                                        None
2809                                    };
2810                                }
2811
2812                                Ok(poll.map(|v| v.map(|result| (result, cancel))))
2813                            })?
2814                        })
2815                            .await?
2816                    } else {
2817                        (StreamResult::Completed, false)
2818                    };
2819
2820                    let (guest_offset, host_offset, count) = tls::get(|store| {
2821                        let transmit = store.concurrent_state_mut()?.get_mut(id)?;
2822                        let (count, host_offset) = match &transmit.read {
2823                            &ReadState::GuestReady { count, .. } => (count.as_u32(), 0),
2824                            &ReadState::HostToHost { limit, .. } => (1, limit),
2825                            _ => bail_bug!("invalid read state"),
2826                        };
2827                        let guest_offset = match &transmit.write {
2828                            &WriteState::HostReady { guest_offset, .. } => guest_offset,
2829                            _ => bail_bug!("invalid write state"),
2830                        };
2831                        Ok((guest_offset, host_offset, count))
2832                    })?;
2833
2834                    let produced =
2835                        !buffer.remaining().is_empty() || guest_offset > 0 || host_offset > 0;
2836
2837                    match result {
2838                        StreamResult::Completed => {
2839                            // Only zero-length reads may complete without
2840                            // producing anything. Note that host-to-host reads
2841                            // always have a nonzero `count`.
2842                            if count > 0 && !produced {
2843                                bail!(
2844                                    "StreamProducer::poll_produce returned StreamResult::Completed \
2845                                     without producing any items"
2846                                );
2847                            }
2848                        }
2849                        StreamResult::Cancelled => {
2850                            if !cancelled {
2851                                bail!(
2852                                    "StreamProducer::poll_produce returned StreamResult::Cancelled \
2853                                     without being given a `finish` parameter value of true"
2854                                );
2855                            }
2856                            if produced {
2857                                bail!(
2858                                    "StreamProducer::poll_produce returned StreamResult::Cancelled \
2859                                     after producing at least one item"
2860                                );
2861                            }
2862                        }
2863                        StreamResult::Dropped => {
2864                            *dropped = true;
2865                        }
2866                    }
2867
2868                    let write_buffer = !buffer.remaining().is_empty() || host_offset > 0;
2869
2870                    let dropped = *dropped;
2871
2872                    drop(state);
2873
2874                    if write_buffer {
2875                        write(token, id, producer.clone(), kind).await?;
2876                    }
2877
2878                    Ok(if dropped {
2879                        if producer.with(|p| p.buffer.remaining().is_empty())? {
2880                            StreamResult::Dropped
2881                        } else {
2882                            StreamResult::Completed
2883                        }
2884                    } else {
2885                        result
2886                    })
2887                }
2888                .boxed()
2889            }
2890        });
2891        let try_into = Box::new(move |ty| {
2892            let ProducerState {
2893                producer: mine,
2894                buffer,
2895                dropped,
2896            } = producer.try_lock().ok()?.take()?;
2897            match P::try_into(mine, buffer, ty) {
2898                Ok(value) => Some(value),
2899                Err((mine, buffer)) => {
2900                    *producer.try_lock().ok()? = Some(ProducerState {
2901                        producer: mine,
2902                        buffer,
2903                        dropped,
2904                    });
2905                    None
2906                }
2907            }
2908        });
2909        state.get_mut(id)?.write = WriteState::HostReady {
2910            produce,
2911            try_into,
2912            guest_offset: ItemCount::ZERO,
2913            cancel: false,
2914            cancel_waker: None,
2915        };
2916        self.0.host_reader_insert(read)
2917    }
2918
2919    fn set_consumer<C: StreamConsumer<T>>(
2920        mut self,
2921        idx: HostResourceIndex,
2922        kind: TransmitKind,
2923        consumer: C,
2924    ) -> Result<()> {
2925        let token = StoreToken::new(self.as_context_mut());
2926        let id = self.0.host_reader_take(idx)?;
2927        let state = self.0.concurrent_state_mut()?;
2928        let id = state.get_mut(id)?.state;
2929        let transmit = state.get_mut(id)?;
2930        let consumer = Arc::new(LockedState::new(Box::pin(consumer)));
2931        let consume_with_buffer = {
2932            let consumer = consumer.clone();
2933            async move |mut host_buffer: Option<&mut dyn WriteBuffer<C::Item>>| {
2934                let mut mine = consumer.take()?;
2935
2936                let host_buffer_remaining_before =
2937                    host_buffer.as_deref_mut().map(|v| v.remaining().len());
2938
2939                let (result, cancelled) = future::poll_fn(|cx| {
2940                    tls::get(|store| {
2941                        let cancel = match &store.concurrent_state_mut()?.get_mut(id)?.read {
2942                            &ReadState::HostReady { cancel, .. } => cancel,
2943                            ReadState::Open => false,
2944                            _ => bail_bug!("unexpected read state"),
2945                        };
2946
2947                        let poll = mine.as_mut().poll_consume(
2948                            cx,
2949                            token.as_context_mut(store),
2950                            Source {
2951                                id,
2952                                host_buffer: host_buffer.as_deref_mut(),
2953                            },
2954                            cancel,
2955                        );
2956
2957                        if let ReadState::HostReady { cancel_waker, .. } =
2958                            &mut store.concurrent_state_mut()?.get_mut(id)?.read
2959                        {
2960                            *cancel_waker = if poll.is_pending() {
2961                                Some(cx.waker().clone())
2962                            } else {
2963                                None
2964                            };
2965                        }
2966
2967                        Ok(poll.map(|v| v.map(|result| (result, cancel))))
2968                    })?
2969                })
2970                .await?;
2971
2972                let (guest_offset, count) = tls::get(|store| {
2973                    let transmit = store.concurrent_state_mut()?.get_mut(id)?;
2974                    Ok((
2975                        match &transmit.read {
2976                            &ReadState::HostReady { guest_offset, .. } => guest_offset,
2977                            ReadState::Open => ItemCount::ZERO,
2978                            _ => bail_bug!("invalid read state"),
2979                        },
2980                        match &transmit.write {
2981                            WriteState::GuestReady { count, .. } => count.as_usize(),
2982                            WriteState::HostReady { .. } => match host_buffer_remaining_before {
2983                                Some(n) => n,
2984                                None => bail_bug!("host_buffer_remaining_before should be set"),
2985                            },
2986                            _ => bail_bug!("invalid write state"),
2987                        },
2988                    ))
2989                })?;
2990
2991                match result {
2992                    StreamResult::Completed => {
2993                        // Progress for a host writer is tracked by its buffer
2994                        // shrinking, whereas for a guest writer it's tracked
2995                        // in `guest_offset`.
2996                        let consumed = match (host_buffer_remaining_before, host_buffer) {
2997                            (Some(before), Some(after)) => before != after.remaining().len(),
2998                            _ => guest_offset > 0,
2999                        };
3000                        if count > 0 && !consumed {
3001                            bail!(
3002                                "StreamConsumer::poll_consume returned StreamResult::Completed \
3003                                 without consuming any items"
3004                            );
3005                        }
3006
3007                        if let TransmitKind::Future = kind {
3008                            tls::get(|store| {
3009                                store.concurrent_state_mut()?.get_mut(id)?.done = true;
3010                                crate::error::Ok(())
3011                            })?;
3012                        }
3013                    }
3014                    StreamResult::Cancelled => {
3015                        if !cancelled {
3016                            bail!(
3017                                "StreamConsumer::poll_consume returned StreamResult::Cancelled \
3018                                 without being given a `finish` parameter value of true"
3019                            );
3020                        }
3021                    }
3022                    StreamResult::Dropped => {}
3023                }
3024
3025                Ok(result)
3026            }
3027        };
3028        let consume = {
3029            let consume = consume_with_buffer.clone();
3030            Box::new(move || {
3031                let consume = consume.clone();
3032                async move { consume(None).await }.boxed()
3033            })
3034        };
3035
3036        match &transmit.write {
3037            WriteState::Open => {
3038                transmit.read = ReadState::HostReady {
3039                    consume,
3040                    guest_offset: ItemCount::ZERO,
3041                    cancel: false,
3042                    cancel_waker: None,
3043                };
3044            }
3045            &WriteState::GuestReady { .. } => {
3046                // If the guest writer has a pending event, then don't start
3047                // another read event here even if there's some partial data
3048                // available. Wait for that to be delivered and wait for the
3049                // writer to do something else before a read is triggered.
3050                let write_handle = transmit.write_handle;
3051                let write_has_event = Waitable::Transmit(write_handle)
3052                    .common(state)?
3053                    .event
3054                    .is_some();
3055                let future = if write_has_event {
3056                    None
3057                } else {
3058                    Some(consume())
3059                };
3060                state.get_mut(id)?.read = ReadState::HostReady {
3061                    consume,
3062                    guest_offset: ItemCount::ZERO,
3063                    cancel: false,
3064                    cancel_waker: None,
3065                };
3066                if let Some(future) = future {
3067                    self.0.pipe_from_guest(kind, id, future);
3068                }
3069            }
3070            WriteState::HostReady { .. } => {
3071                let WriteState::HostReady { produce, .. } = mem::replace(
3072                    &mut transmit.write,
3073                    WriteState::HostReady {
3074                        produce: Box::new(|| {
3075                            Box::pin(async { bail_bug!("unexpected invocation of `produce`") })
3076                        }),
3077                        try_into: Box::new(|_| None),
3078                        guest_offset: ItemCount::ZERO,
3079                        cancel: false,
3080                        cancel_waker: None,
3081                    },
3082                ) else {
3083                    bail_bug!("expected WriteState::HostReady")
3084                };
3085
3086                transmit.read = ReadState::HostToHost {
3087                    accept: Box::new(move |input| {
3088                        let consume = consume_with_buffer.clone();
3089                        async move { consume(Some(input.get_mut::<C::Item>()?)).await }.boxed()
3090                    }),
3091                    buffer: Vec::new(),
3092                    limit: 0,
3093                };
3094
3095                let future = async move {
3096                    loop {
3097                        if tls::get(|store| {
3098                            crate::error::Ok(matches!(
3099                                store.concurrent_state_mut()?.get_mut(id)?.read,
3100                                ReadState::Dropped
3101                            ))
3102                        })? {
3103                            break Ok(());
3104                        }
3105
3106                        match produce().await? {
3107                            StreamResult::Completed | StreamResult::Cancelled => {}
3108                            StreamResult::Dropped => break Ok(()),
3109                        }
3110
3111                        if let TransmitKind::Future = kind {
3112                            break Ok(());
3113                        }
3114                    }
3115                }
3116                .map(move |result| {
3117                    tls::get(|store| store.concurrent_state_mut()?.delete_transmit(id))?;
3118                    result
3119                });
3120
3121                state.push_future(Box::pin(future));
3122            }
3123            WriteState::Dropped => {
3124                let reader = transmit.read_handle;
3125                self.0.host_drop_reader(reader, kind)?;
3126            }
3127        }
3128        Ok(())
3129    }
3130}
3131
3132async fn write<D: 'static, P: Send + 'static, T: func::Lower + 'static, B: WriteBuffer<T>>(
3133    token: StoreToken<D>,
3134    id: TableId<TransmitState>,
3135    producer: Arc<LockedState<ProducerState<P, B>>>,
3136    kind: TransmitKind,
3137) -> Result<()> {
3138    let (read, guest_offset) = tls::get(|store| {
3139        let transmit = store.concurrent_state_mut()?.get_mut(id)?;
3140
3141        let guest_offset = if let &WriteState::HostReady { guest_offset, .. } = &transmit.write {
3142            Some(guest_offset)
3143        } else {
3144            None
3145        };
3146
3147        crate::error::Ok((
3148            mem::replace(&mut transmit.read, ReadState::Open),
3149            guest_offset,
3150        ))
3151    })?;
3152
3153    match read {
3154        ReadState::GuestReady {
3155            ty,
3156            flat_abi,
3157            options,
3158            address,
3159            count,
3160            handle,
3161            instance,
3162            caller_instance,
3163        } => {
3164            let guest_offset = match guest_offset {
3165                Some(i) => i,
3166                None => bail_bug!("guest_offset should be present if ready"),
3167            };
3168
3169            if let TransmitKind::Future = kind {
3170                tls::get(|store| {
3171                    store.concurrent_state_mut()?.get_mut(id)?.done = true;
3172                    crate::error::Ok(())
3173                })?;
3174            }
3175
3176            let old_remaining = producer.with(|p| p.buffer.remaining().len())?;
3177            let accept = {
3178                let producer = producer.clone();
3179                move |mut store: StoreContextMut<D>| {
3180                    let mut state = producer.take()?;
3181                    lower::<T, B, D>(
3182                        store.as_context_mut(),
3183                        instance,
3184                        options,
3185                        ty,
3186                        address + (T::SIZE32 * guest_offset.as_usize()),
3187                        count.as_usize() - guest_offset.as_usize(),
3188                        &mut state.buffer,
3189                    )?;
3190                    crate::error::Ok(())
3191                }
3192            };
3193
3194            if guest_offset < count {
3195                if T::MAY_REQUIRE_REALLOC {
3196                    // For payloads which may require a realloc call, use a
3197                    // oneshot::channel and background task.  This is
3198                    // necessary because calling the guest while there are
3199                    // host embedder frames on the stack is unsound.
3200                    let (tx, rx) = oneshot::channel();
3201                    tls::get(move |store| {
3202                        store
3203                            .concurrent_state_mut()?
3204                            .push_high_priority(WorkItem::WorkerFunction(AlwaysMut::new(
3205                                Box::new(move |store| {
3206                                    _ = tx.send(accept(token.as_context_mut(store))?);
3207                                    Ok(())
3208                                }),
3209                            )));
3210                        crate::error::Ok(())
3211                    })?;
3212                    match rx.await {
3213                        Ok(r) => r,
3214                        // The worker function was dropped without sending a
3215                        // result, which can happen when lowering trapped. This
3216                        // isn't a bug but this specific handling here might be
3217                        // the last hurray of this task in the event queue, so
3218                        // don't kill anything other than our caller. The
3219                        // original trap should make its way back to the best
3220                        // location naturally.
3221                        Err(oneshot::Canceled) => {
3222                            bail!("stream write cancelled before it could complete")
3223                        }
3224                    }
3225                } else {
3226                    // Optimize flat payloads (i.e. those which do not
3227                    // require calling the guest's realloc function) by
3228                    // lowering directly instead of using a oneshot::channel
3229                    // and background task.
3230                    tls::get(|store| accept(token.as_context_mut(store)))?
3231                }
3232            }
3233
3234            tls::get(|store| {
3235                let count = old_remaining - producer.with(|p| p.buffer.remaining().len())?;
3236
3237                let transmit = store.concurrent_state_mut()?.get_mut(id)?;
3238
3239                let WriteState::HostReady { guest_offset, .. } = &mut transmit.write else {
3240                    bail_bug!("expected WriteState::HostReady")
3241                };
3242
3243                guest_offset.inc(count)?;
3244
3245                transmit.read = ReadState::GuestReady {
3246                    ty,
3247                    flat_abi,
3248                    options,
3249                    address,
3250                    count: ItemCount::new_usize(count)?,
3251                    handle,
3252                    instance,
3253                    caller_instance,
3254                };
3255
3256                crate::error::Ok(())
3257            })?;
3258
3259            Ok(())
3260        }
3261
3262        ReadState::HostToHost {
3263            accept,
3264            mut buffer,
3265            limit,
3266        } => {
3267            let mut state = StreamResult::Completed;
3268            let mut position = 0;
3269
3270            while !matches!(state, StreamResult::Dropped) && position < limit {
3271                let mut slice_buffer = SliceBuffer::new(buffer, position, limit);
3272                state = accept(&mut UntypedWriteBuffer::new(&mut slice_buffer)).await?;
3273                (buffer, position, _) = slice_buffer.into_parts();
3274            }
3275
3276            {
3277                let mut producer = producer.take()?;
3278                let ProducerState { buffer, .. } = &mut *producer;
3279
3280                while !(matches!(state, StreamResult::Dropped) || buffer.remaining().is_empty()) {
3281                    state = accept(&mut UntypedWriteBuffer::new(buffer)).await?;
3282                }
3283            }
3284
3285            tls::get(|store| {
3286                store.concurrent_state_mut()?.get_mut(id)?.read = match state {
3287                    StreamResult::Dropped => ReadState::Dropped,
3288                    StreamResult::Completed | StreamResult::Cancelled => ReadState::HostToHost {
3289                        accept,
3290                        buffer,
3291                        limit: 0,
3292                    },
3293                };
3294
3295                crate::error::Ok(())
3296            })?;
3297            Ok(())
3298        }
3299
3300        _ => bail_bug!("unexpected read state"),
3301    }
3302}
3303
3304/// Polls a `StreamProducer` or `StreamConsumer` future once, inline, from
3305/// within a guest's `{stream,future}.{read,write}` intrinsic.
3306///
3307/// The producer or consumer is embedder code which doesn't belong to the
3308/// reading or writing guest's task, so it's polled as though from the
3309/// top-level host, just as it is when it's later polled from the event loop.
3310fn poll_inline(
3311    store: &mut dyn VMStore,
3312    future: &mut Pin<Box<dyn Future<Output = Result<StreamResult>> + Send + 'static>>,
3313) -> Result<Poll<Result<StreamResult>>> {
3314    let old_thread = store.set_thread(CurrentThread::None)?;
3315    let poll = tls::set(store, || {
3316        future
3317            .as_mut()
3318            .poll(&mut Context::from_waker(&Waker::noop()))
3319    });
3320    store.set_thread(old_thread)?;
3321    Ok(poll)
3322}
3323
3324impl Instance {
3325    /// Handle a guest-initiated write by delivering the item(s) to the
3326    /// `StreamConsumer` for the specified stream or future.
3327    fn consume(
3328        self,
3329        store: &mut dyn VMStore,
3330        kind: TransmitKind,
3331        transmit_id: TableId<TransmitState>,
3332        consume: PollStream,
3333        guest_offset: ItemCount,
3334        cancel: bool,
3335    ) -> Result<ReturnCode> {
3336        let mut future = consume();
3337        store.concurrent_state_mut()?.get_mut(transmit_id)?.read = ReadState::HostReady {
3338            consume,
3339            guest_offset,
3340            cancel,
3341            cancel_waker: None,
3342        };
3343        let poll = poll_inline(store, &mut future)?;
3344
3345        Ok(match poll {
3346            Poll::Ready(state) => {
3347                let transmit = store.concurrent_state_mut()?.get_mut(transmit_id)?;
3348                let code = settle_host_read(transmit, kind, state?)?;
3349                transmit.write = WriteState::Open;
3350                code
3351            }
3352            Poll::Pending => {
3353                store.pipe_from_guest(kind, transmit_id, future);
3354                ReturnCode::Blocked
3355            }
3356        })
3357    }
3358
3359    /// Handle a guest-initiated read by polling the `StreamProducer` for the
3360    /// specified stream or future for items.
3361    fn produce(
3362        self,
3363        store: &mut dyn VMStore,
3364        kind: TransmitKind,
3365        transmit_id: TableId<TransmitState>,
3366        produce: PollStream,
3367        try_into: TryInto,
3368        guest_offset: ItemCount,
3369        cancel: bool,
3370    ) -> Result<ReturnCode> {
3371        let mut future = produce();
3372        store.concurrent_state_mut()?.get_mut(transmit_id)?.write = WriteState::HostReady {
3373            produce,
3374            try_into,
3375            guest_offset,
3376            cancel,
3377            cancel_waker: None,
3378        };
3379        let poll = poll_inline(store, &mut future)?;
3380
3381        Ok(match poll {
3382            Poll::Ready(state) => {
3383                let transmit = store.concurrent_state_mut()?.get_mut(transmit_id)?;
3384                let code = settle_host_write(transmit, kind, state?)?;
3385                transmit.read = ReadState::Open;
3386                code
3387            }
3388            Poll::Pending => {
3389                store.pipe_to_guest(kind, transmit_id, future);
3390                ReturnCode::Blocked
3391            }
3392        })
3393    }
3394
3395    /// Drop the writable end of the specified stream or future from the guest.
3396    pub(super) fn guest_drop_writable(
3397        self,
3398        store: &mut StoreOpaque,
3399        ty: TransmitIndex,
3400        writer: u32,
3401    ) -> Result<()> {
3402        let table = self.id().get_mut(store).table_for_transmit(ty);
3403        let (transmit_rep, is_done) = match ty {
3404            TransmitIndex::Future(ty) => table.future_remove_writable(ty, writer)?,
3405            TransmitIndex::Stream(ty) => (table.stream_remove_writable(ty, writer)?, false),
3406        };
3407
3408        let id = TableId::<TransmitHandle>::new(transmit_rep);
3409        log::trace!("guest_drop_writable: drop writer {id:?}");
3410        match ty {
3411            TransmitIndex::Stream(_) => store.host_drop_writer(id, None),
3412            TransmitIndex::Future(_) => store.host_drop_writer(
3413                id,
3414                if is_done {
3415                    None
3416                } else {
3417                    Some(|| {
3418                        Err(format_err!(
3419                            "cannot drop future write end without first writing a value"
3420                        ))
3421                    })
3422                },
3423            ),
3424        }
3425    }
3426
3427    /// Copy `count` items from `read_address` to `write_address` for the
3428    /// specified stream or future.
3429    fn copy<T: 'static>(
3430        store: StoreContextMut<T>,
3431        flat_abi: Option<FlatAbi>,
3432        write_runtime_instance: RuntimeInstance,
3433        write_ty: TransmitIndex,
3434        write_options: OptionsIndex,
3435        write_address: usize,
3436        read_runtime_instance: RuntimeInstance,
3437        read_ty: TransmitIndex,
3438        read_options: OptionsIndex,
3439        read_address: usize,
3440        count: ItemCount,
3441        rep: u32,
3442    ) -> Result<()> {
3443        let write_instance = Instance::from_runtime_instance(store.0, write_runtime_instance);
3444        let read_instance = Instance::from_runtime_instance(store.0, read_runtime_instance);
3445        let (write_component, store) = write_instance.component_and_store_mut(store.0);
3446        let (read_component, mut store) = read_instance.component_and_store_mut(store);
3447        let write_types = write_component.types();
3448        let read_types = read_component.types();
3449        let count = count.as_usize();
3450
3451        // Double-check that both buffers are properly aligned and in-bounds.
3452        // This was already validated in `check_bounds` when the read/write was
3453        // issued, so failure here is a bug.
3454        //
3455        // FIXME(#4311): this and the rest of this function use the 32-bit
3456        // layout of payloads, which needs memory64 handling.
3457        let write_payload_ty = write_ty.payload(write_types);
3458        let write_abi = match write_payload_ty {
3459            Some(ty) => write_types.canonical_abi(ty),
3460            None => &CanonicalAbiInfo::ZERO,
3461        };
3462        let write_length_in_bytes = match flat_abi {
3463            Some(abi) => usize::try_from(abi.size)? * count,
3464            None => usize::try_from(write_abi.size32)? * count,
3465        };
3466        if write_payload_ty.is_some() {
3467            if write_address % usize::try_from(write_abi.align32)? != 0 {
3468                bail_bug!("write pointer not aligned");
3469            }
3470            if write_instance
3471                .options_memory(store, write_options)
3472                .get(write_address..)
3473                .and_then(|b| b.get(..write_length_in_bytes))
3474                .is_none()
3475            {
3476                bail_bug!("write pointer out of bounds");
3477            }
3478        }
3479
3480        let read_payload_ty = read_ty.payload(read_types);
3481        let read_abi = match read_payload_ty {
3482            Some(ty) => read_types.canonical_abi(ty),
3483            None => &CanonicalAbiInfo::ZERO,
3484        };
3485        let read_length_in_bytes = match flat_abi {
3486            Some(abi) => usize::try_from(abi.size)? * count,
3487            None => usize::try_from(read_abi.size32)? * count,
3488        };
3489        if read_payload_ty.is_some() {
3490            if read_address % usize::try_from(read_abi.align32)? != 0 {
3491                bail_bug!("read pointer not aligned");
3492            }
3493            if read_instance
3494                .options_memory(store, read_options)
3495                .get(read_address..)
3496                .and_then(|b| b.get(..read_length_in_bytes))
3497                .is_none()
3498            {
3499                bail_bug!("read pointer out of bounds");
3500            }
3501        }
3502
3503        if write_runtime_instance == read_runtime_instance
3504            && !allow_intra_component_read_write(write_payload_ty)
3505        {
3506            bail!(
3507                "cannot read from and write to intra-component future/stream with non-numeric payload"
3508            )
3509        }
3510
3511        match (write_ty, read_ty) {
3512            (TransmitIndex::Future(_), TransmitIndex::Future(_)) => {
3513                if count != 1 {
3514                    bail_bug!("futures can only send 1 item");
3515                }
3516
3517                let val = write_payload_ty
3518                    .map(|ty| {
3519                        let lift = &mut LiftContext::new(store, write_options, write_instance)?;
3520                        let bytes = &lift.memory()[write_address..][..write_length_in_bytes];
3521                        Val::load(lift, *ty, bytes)
3522                    })
3523                    .transpose()?;
3524
3525                if let Some(val) = val {
3526                    let lower =
3527                        &mut LowerContext::new(store.as_context_mut(), read_options, read_instance);
3528                    let ptr = func::validate_inbounds_dynamic(
3529                        read_abi,
3530                        lower.as_slice(),
3531                        &ValRaw::u32(read_address.try_into()?),
3532                    )?;
3533                    let ty = match read_payload_ty {
3534                        Some(ty) => ty,
3535                        None => bail_bug!("expected read payload type to be present"),
3536                    };
3537                    val.store(lower, *ty, ptr)?;
3538                }
3539            }
3540            (TransmitIndex::Stream(_), TransmitIndex::Stream(_)) => {
3541                if write_length_in_bytes == 0 {
3542                    return Ok(());
3543                }
3544                let write_payload_ty = match write_payload_ty {
3545                    Some(ty) => ty,
3546                    None => bail_bug!("expected write payload type to be present"),
3547                };
3548                let read_payload_ty = match read_payload_ty {
3549                    Some(ty) => ty,
3550                    None => bail_bug!("expected read payload type to be present"),
3551                };
3552                let lift = &mut LiftContext::new(&mut **store, write_options, write_instance)?;
3553                if let Some(abi) = flat_abi {
3554                    // The host copies these bytes itself rather than wasm, so
3555                    // charge them against hostcall fuel.
3556                    lift.consume_fuel_array(count, usize::try_from(abi.size)?)?;
3557
3558                    // Fast path memcpy for "flat" (i.e. no pointers or handles) payloads:
3559                    let store_opaque = &mut **store;
3560
3561                    assert_eq!(read_length_in_bytes, write_length_in_bytes);
3562
3563                    if read_instance
3564                        .options_memory(store_opaque, read_options)
3565                        .as_ptr()
3566                        == write_instance
3567                            .options_memory(store_opaque, write_options)
3568                            .as_ptr()
3569                    {
3570                        let memory = read_instance.options_memory_mut(store_opaque, read_options);
3571                        memory.copy_within(
3572                            write_address..write_address + write_length_in_bytes,
3573                            read_address,
3574                        );
3575                    } else {
3576                        let src = write_instance.options_memory(store_opaque, write_options)
3577                            [write_address..][..write_length_in_bytes]
3578                            .as_ptr();
3579                        let dst = read_instance
3580                            .options_memory_range_mut(
3581                                store_opaque,
3582                                read_options,
3583                                read_address,
3584                                read_length_in_bytes,
3585                            )
3586                            .unwrap()
3587                            .as_mut_ptr();
3588
3589                        // SAFETY: Both `src` and `dst` have been validated
3590                        // above to be valid pointers as they're derived from
3591                        // slices that have the desired length with the desired
3592                        // read/write permission. The `unsafe` bit here is that
3593                        // the memories are disjoint (different base pointers)
3594                        // and there's no easy way to borrow both
3595                        // simultaneously from the store. Different memories
3596                        // are guaranteed to be disjoint, however, so the
3597                        // `unsafe` here should be ok.
3598                        unsafe {
3599                            src.copy_to_nonoverlapping(dst, write_length_in_bytes);
3600                        }
3601                    }
3602                } else {
3603                    let bytes = &lift.memory()[write_address..][..write_length_in_bytes];
3604                    lift.consume_fuel_array(count, size_of::<Val>())?;
3605
3606                    let values = (0..count)
3607                        .map(|index| {
3608                            let size = usize::try_from(write_abi.size32)?;
3609                            Val::load(lift, *write_payload_ty, &bytes[(index * size)..][..size])
3610                        })
3611                        .collect::<Result<Vec<_>>>()?;
3612
3613                    let id = TableId::<TransmitHandle>::new(rep);
3614                    log::trace!("copy values {values:?} for {id:?}");
3615
3616                    let lower =
3617                        &mut LowerContext::new(store.as_context_mut(), read_options, read_instance);
3618                    let mut ptr = read_address;
3619                    for value in values {
3620                        value.store(lower, *read_payload_ty, ptr)?;
3621                        ptr += usize::try_from(read_abi.size32)?;
3622                    }
3623                }
3624            }
3625            _ => bail_bug!("mismatched transmit types in copy"),
3626        }
3627
3628        Ok(())
3629    }
3630
3631    fn check_bounds(
3632        self,
3633        store: &StoreOpaque,
3634        options: OptionsIndex,
3635        ty: TransmitIndex,
3636        address: usize,
3637        count: usize,
3638    ) -> Result<()> {
3639        let types = self.id().get(store).component().types();
3640        let payload = match ty {
3641            TransmitIndex::Future(ty) => types[types[ty].ty].payload,
3642            TransmitIndex::Stream(ty) => types[types[ty].ty].payload,
3643        };
3644        // Empty payloads ignore the pointer entirely, but otherwise the
3645        // alignment and bounds checks apply even to zero-length reads/writes.
3646        let Some(payload) = payload else {
3647            return Ok(());
3648        };
3649        let abi = types.canonical_abi(&payload);
3650        // FIXME(#4311): needs memory64 handling
3651        let size = usize::try_from(abi.size32)?;
3652        let align = usize::try_from(abi.align32)?;
3653        if address % align != 0 {
3654            bail!("buffer pointer not aligned");
3655        }
3656        self.options_memory(store, options)
3657            .get(address..)
3658            .and_then(|b| b.get(..size.checked_mul(count)?))
3659            .map(drop)
3660            .ok_or_else(|| crate::format_err!("buffer pointer out of bounds of memory"))
3661    }
3662
3663    /// Write to the specified stream or future from the guest.
3664    pub(super) fn guest_write<T: 'static>(
3665        self,
3666        mut store: StoreContextMut<T>,
3667        caller: RuntimeComponentInstanceIndex,
3668        ty: TransmitIndex,
3669        options: OptionsIndex,
3670        flat_abi: Option<FlatAbi>,
3671        handle: u32,
3672        address: u32,
3673        count: u32,
3674    ) -> Result<ReturnCode> {
3675        let count = ItemCount::new(count)?;
3676
3677        let address = usize::try_from(address)?;
3678        self.check_bounds(store.0, options, ty, address, count.as_usize())?;
3679        let async_ = self.options(store.0, options).async_;
3680        let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;
3681        let TransmitLocalStateKind::Write { done } = state.kind() else {
3682            bail!(Trap::ConcurrentFutureStreamOp);
3683        };
3684
3685        *state = VMTransmitLocalState::busy();
3686        let transmit_handle = TableId::<TransmitHandle>::new(rep);
3687        let concurrent_state = store.0.concurrent_state_mut()?;
3688        let transmit_id = concurrent_state.get_mut(transmit_handle)?.state;
3689        let transmit = concurrent_state.get_mut(transmit_id)?;
3690        log::trace!(
3691            "guest_write {count} to {transmit_handle:?} (handle {handle}; state {transmit_id:?}); {:?}",
3692            transmit.read
3693        );
3694
3695        if done || transmit.done {
3696            bail!(match ty {
3697                TransmitIndex::Future(_) => Trap::WriteToDroppedFuture,
3698                TransmitIndex::Stream(_) => Trap::WriteToDroppedStream,
3699            });
3700        }
3701
3702        if !async_ {
3703            Waitable::Transmit(transmit_handle).trap_if_in_waitable_set(concurrent_state)?;
3704        }
3705        let transmit = concurrent_state.get_mut(transmit_id)?;
3706
3707        let new_state = if let ReadState::Dropped = &transmit.read {
3708            ReadState::Dropped
3709        } else {
3710            ReadState::Open
3711        };
3712
3713        let set_guest_ready = |me: &mut ConcurrentState| {
3714            let transmit = me.get_mut(transmit_id)?;
3715            if !matches!(&transmit.write, WriteState::Open) {
3716                bail_bug!("expected `WriteState::Open`; got `{:?}`", transmit.write);
3717            }
3718            transmit.write = WriteState::GuestReady {
3719                instance: self,
3720                caller,
3721                ty,
3722                flat_abi,
3723                options,
3724                address,
3725                count,
3726                handle,
3727            };
3728            Ok::<_, crate::Error>(())
3729        };
3730
3731        let mut result = match mem::replace(&mut transmit.read, new_state) {
3732            ReadState::GuestReady {
3733                ty: read_ty,
3734                flat_abi: read_flat_abi,
3735                options: read_options,
3736                address: read_address,
3737                count: read_count,
3738                handle: read_handle,
3739                instance: read_instance,
3740                caller_instance: read_caller_instance,
3741            } => {
3742                if flat_abi != read_flat_abi {
3743                    bail_bug!("expected flat ABI calculations to be the same");
3744                }
3745
3746                if let TransmitIndex::Future(_) = ty {
3747                    transmit.done = true;
3748                }
3749
3750                // Note that zero-length reads and writes are handling specially
3751                // by the spec to allow each end to signal readiness to the
3752                // other.  Quoting the spec:
3753                //
3754                // ```
3755                // The meaning of a read or write when the length is 0 is that
3756                // the caller is querying the "readiness" of the other
3757                // side. When a 0-length read/write rendezvous with a
3758                // non-0-length read/write, only the 0-length read/write
3759                // completes; the non-0-length read/write is kept pending (and
3760                // ready for a subsequent rendezvous).
3761                //
3762                // In the corner case where a 0-length read and write
3763                // rendezvous, only the writer is notified of readiness. To
3764                // avoid livelock, the Canonical ABI requires that a writer must
3765                // (eventually) follow a completed 0-length write with a
3766                // non-0-length write that is allowed to block (allowing the
3767                // reader end to run and rendezvous with its own non-0-length
3768                // read).
3769                // ```
3770
3771                let write_count = count;
3772                let write_complete = count == 0 || read_count > 0;
3773                let read_complete = count > 0;
3774                let read_buffer_remaining = count < read_count;
3775
3776                let read_handle_rep = transmit.read_handle.rep();
3777
3778                let count = count.min(read_count);
3779
3780                Instance::copy(
3781                    store.as_context_mut(),
3782                    flat_abi,
3783                    self.runtime_instance(caller),
3784                    ty,
3785                    options,
3786                    address,
3787                    read_instance.runtime_instance(read_caller_instance),
3788                    read_ty,
3789                    read_options,
3790                    read_address,
3791                    count,
3792                    rep,
3793                )?;
3794
3795                let instance = read_instance.id().get(store.0);
3796                let types = instance.component().types();
3797                // FIXME(#4311): needs memory64 handling
3798                let item_size = match read_ty.payload(types) {
3799                    Some(ty) => usize::try_from(types.canonical_abi(ty).size32)?,
3800                    None => 0,
3801                };
3802                let concurrent_state = store.0.concurrent_state_mut()?;
3803                if read_complete {
3804                    let total = if let Some(Event::StreamRead {
3805                        code: ReturnCode::Completed(old_total),
3806                        ..
3807                    }) = concurrent_state.take_event(read_handle_rep)?
3808                    {
3809                        count.add(old_total)?
3810                    } else {
3811                        count
3812                    };
3813
3814                    let code = ReturnCode::completed(ty.kind(), total);
3815
3816                    concurrent_state.send_read_result(read_ty, transmit_id, read_handle, code)?;
3817                }
3818
3819                // If the reader still has buffer remaining, or if this was a
3820                // zero-length rendezvous, then restore the state of the reader
3821                // back to what it was when we found it. Note that for the
3822                // zero-length rendezvous case this specifically won't execute
3823                // the `read_complete` logic above, which is intentional, as the
3824                // reader remains blocked.
3825                if read_buffer_remaining || (write_count == 0 && read_count == 0) {
3826                    let transmit = concurrent_state.get_mut(transmit_id)?;
3827                    transmit.read = ReadState::GuestReady {
3828                        ty: read_ty,
3829                        flat_abi: read_flat_abi,
3830                        options: read_options,
3831                        address: read_address + (count.as_usize() * item_size),
3832                        count: read_count.sub(count)?,
3833                        handle: read_handle,
3834                        instance: read_instance,
3835                        caller_instance: read_caller_instance,
3836                    };
3837                }
3838
3839                if write_complete {
3840                    ReturnCode::completed(ty.kind(), count)
3841                } else {
3842                    set_guest_ready(concurrent_state)?;
3843                    ReturnCode::Blocked
3844                }
3845            }
3846
3847            ReadState::HostReady {
3848                consume,
3849                guest_offset,
3850                cancel,
3851                cancel_waker,
3852            } => {
3853                if cancel_waker.is_some() {
3854                    bail_bug!("expected cancel_waker to be none");
3855                }
3856                if cancel {
3857                    bail_bug!("expected cancel to be false");
3858                }
3859                if guest_offset != 0 {
3860                    bail_bug!("expected guest_offset to be 0");
3861                }
3862
3863                set_guest_ready(concurrent_state)?;
3864                self.consume(
3865                    store.0,
3866                    ty.kind(),
3867                    transmit_id,
3868                    consume,
3869                    ItemCount::ZERO,
3870                    false,
3871                )?
3872            }
3873
3874            ReadState::HostToHost { .. } => bail_bug!("unexpected HostToHost"),
3875
3876            ReadState::Open => {
3877                set_guest_ready(concurrent_state)?;
3878                ReturnCode::Blocked
3879            }
3880
3881            ReadState::Dropped => {
3882                if let TransmitIndex::Future(_) = ty {
3883                    transmit.done = true;
3884                }
3885
3886                match Waitable::Transmit(transmit_handle).take_event(concurrent_state)? {
3887                    Some(
3888                        Event::StreamWrite {
3889                            code: ReturnCode::Dropped(ItemCount::ZERO),
3890                            ..
3891                        }
3892                        | Event::FutureWrite {
3893                            code: ReturnCode::Dropped(ItemCount::ZERO),
3894                            ..
3895                        },
3896                    ) => {}
3897                    event => bail_bug!("expected pending dropped event for writer; got {event:?}"),
3898                }
3899
3900                ReturnCode::Dropped(ItemCount::ZERO)
3901            }
3902        };
3903
3904        if result == ReturnCode::Blocked && !async_ {
3905            result = self.wait_for_write(store.0, caller, transmit_handle)?;
3906        }
3907
3908        if result != ReturnCode::Blocked {
3909            *self.id().get_mut(store.0).get_mut_by_index(ty, handle)?.1 =
3910                VMTransmitLocalState::write(matches!(result, ReturnCode::Dropped(_)));
3911        }
3912
3913        log::trace!(
3914            "guest_write result for {transmit_handle:?} (handle {handle}; state {transmit_id:?}): {result:?}",
3915        );
3916
3917        Ok(result)
3918    }
3919
3920    /// Read from the specified stream or future from the guest.
3921    pub(super) fn guest_read<T: 'static>(
3922        self,
3923        mut store: StoreContextMut<T>,
3924        caller_instance: RuntimeComponentInstanceIndex,
3925        ty: TransmitIndex,
3926        options: OptionsIndex,
3927        flat_abi: Option<FlatAbi>,
3928        handle: u32,
3929        address: u32,
3930        count: u32,
3931    ) -> Result<ReturnCode> {
3932        let count = ItemCount::new(count)?;
3933
3934        let address = usize::try_from(address)?;
3935        self.check_bounds(store.0, options, ty, address, count.as_usize())?;
3936        let async_ = self.options(store.0, options).async_;
3937        let (rep, state) = self.id().get_mut(store.0).get_mut_by_index(ty, handle)?;
3938        let TransmitLocalStateKind::Read { done } = state.kind() else {
3939            bail!(Trap::ConcurrentFutureStreamOp);
3940        };
3941
3942        *state = VMTransmitLocalState::busy();
3943        let transmit_handle = TableId::<TransmitHandle>::new(rep);
3944        let concurrent_state = store.0.concurrent_state_mut()?;
3945        let transmit_id = concurrent_state.get_mut(transmit_handle)?.state;
3946        let transmit = concurrent_state.get_mut(transmit_id)?;
3947        log::trace!(
3948            "guest_read {count} from {transmit_handle:?} (handle {handle}; state {transmit_id:?}); {:?}",
3949            transmit.write
3950        );
3951
3952        if done || transmit.done {
3953            match ty {
3954                TransmitIndex::Future(_) => {
3955                    bail!("cannot read from future after previous read succeeded")
3956                }
3957                TransmitIndex::Stream(_) => bail!(Trap::ReadFromDroppedStream),
3958            }
3959        }
3960
3961        if !async_ {
3962            Waitable::Transmit(transmit_handle).trap_if_in_waitable_set(concurrent_state)?;
3963        }
3964        let transmit = concurrent_state.get_mut(transmit_id)?;
3965
3966        let new_state = if let WriteState::Dropped = &transmit.write {
3967            WriteState::Dropped
3968        } else {
3969            WriteState::Open
3970        };
3971
3972        let set_guest_ready = |me: &mut ConcurrentState| {
3973            let transmit = me.get_mut(transmit_id)?;
3974            if !matches!(&transmit.read, ReadState::Open) {
3975                bail_bug!("expected `ReadState::Open`; got `{:?}`", transmit.read);
3976            }
3977            transmit.read = ReadState::GuestReady {
3978                ty,
3979                flat_abi,
3980                options,
3981                address,
3982                count,
3983                handle,
3984                instance: self,
3985                caller_instance,
3986            };
3987            Ok::<_, crate::Error>(())
3988        };
3989
3990        let mut result = match mem::replace(&mut transmit.write, new_state) {
3991            WriteState::GuestReady {
3992                instance: write_instance,
3993                ty: write_ty,
3994                flat_abi: write_flat_abi,
3995                options: write_options,
3996                address: write_address,
3997                count: write_count,
3998                handle: write_handle,
3999                caller: write_caller,
4000            } => {
4001                if flat_abi != write_flat_abi {
4002                    bail_bug!("expected flat ABI calculations to be the same");
4003                }
4004
4005                if let TransmitIndex::Future(_) = ty {
4006                    transmit.done = true;
4007                }
4008
4009                let write_handle_rep = transmit.write_handle.rep();
4010
4011                // See the comment in `guest_write` for the
4012                // `ReadState::GuestReady` case concerning zero-length reads and
4013                // writes.
4014
4015                let write_complete = write_count == 0 || count > 0;
4016                let read_complete = write_count > 0;
4017                let write_buffer_remaining = count < write_count;
4018
4019                let count = count.min(write_count);
4020
4021                Instance::copy(
4022                    store.as_context_mut(),
4023                    flat_abi,
4024                    write_instance.runtime_instance(write_caller),
4025                    write_ty,
4026                    write_options,
4027                    write_address,
4028                    self.runtime_instance(caller_instance),
4029                    ty,
4030                    options,
4031                    address,
4032                    count,
4033                    rep,
4034                )?;
4035
4036                let instance = write_instance.id().get(store.0);
4037                let types = instance.component().types();
4038                // FIXME(#4311): needs memory64 handling
4039                let item_size = match write_ty.payload(types) {
4040                    Some(ty) => usize::try_from(types.canonical_abi(ty).size32)?,
4041                    None => 0,
4042                };
4043                let concurrent_state = store.0.concurrent_state_mut()?;
4044
4045                if write_complete {
4046                    let total = if let Some(Event::StreamWrite {
4047                        code: ReturnCode::Completed(old_total),
4048                        ..
4049                    }) = concurrent_state.take_event(write_handle_rep)?
4050                    {
4051                        count.add(old_total)?
4052                    } else {
4053                        count
4054                    };
4055
4056                    let code = ReturnCode::completed(ty.kind(), total);
4057
4058                    concurrent_state.send_write_result(
4059                        write_ty,
4060                        transmit_id,
4061                        write_handle,
4062                        code,
4063                    )?;
4064                }
4065
4066                if write_buffer_remaining {
4067                    let transmit = concurrent_state.get_mut(transmit_id)?;
4068                    transmit.write = WriteState::GuestReady {
4069                        instance: write_instance,
4070                        caller: write_caller,
4071                        ty: write_ty,
4072                        flat_abi: write_flat_abi,
4073                        options: write_options,
4074                        address: write_address + (count.as_usize() * item_size),
4075                        count: write_count.sub(count)?,
4076                        handle: write_handle,
4077                    };
4078                }
4079
4080                if read_complete {
4081                    ReturnCode::completed(ty.kind(), count)
4082                } else {
4083                    set_guest_ready(concurrent_state)?;
4084                    ReturnCode::Blocked
4085                }
4086            }
4087
4088            WriteState::HostReady {
4089                produce,
4090                try_into,
4091                guest_offset,
4092                cancel,
4093                cancel_waker,
4094            } => {
4095                if cancel_waker.is_some() {
4096                    bail_bug!("expected cancel_waker to be none");
4097                }
4098                if cancel {
4099                    bail_bug!("expected cancel to be false");
4100                }
4101                if guest_offset != 0 {
4102                    bail_bug!("expected guest_offset to be 0");
4103                }
4104
4105                set_guest_ready(concurrent_state)?;
4106
4107                let code = self.produce(
4108                    store.0,
4109                    ty.kind(),
4110                    transmit_id,
4111                    produce,
4112                    try_into,
4113                    ItemCount::ZERO,
4114                    false,
4115                )?;
4116
4117                if let (TransmitIndex::Future(_), ReturnCode::Completed(_)) = (ty, code) {
4118                    store.0.concurrent_state_mut()?.get_mut(transmit_id)?.done = true;
4119                }
4120
4121                code
4122            }
4123
4124            WriteState::Open => {
4125                set_guest_ready(concurrent_state)?;
4126                ReturnCode::Blocked
4127            }
4128
4129            WriteState::Dropped => {
4130                if let TransmitIndex::Future(_) = ty {
4131                    bail_bug!(
4132                        "should not be possible to read from a future whose write end was dropped"
4133                    );
4134                }
4135
4136                match Waitable::Transmit(transmit_handle).take_event(concurrent_state)? {
4137                    Some(Event::StreamRead {
4138                        code: ReturnCode::Dropped(ItemCount::ZERO),
4139                        ..
4140                    }) => {}
4141                    event => bail_bug!("expected pending dropped event for reader; got {event:?}"),
4142                }
4143
4144                ReturnCode::Dropped(ItemCount::ZERO)
4145            }
4146        };
4147
4148        if result == ReturnCode::Blocked && !async_ {
4149            result = self.wait_for_read(store.0, caller_instance, transmit_handle)?;
4150        }
4151
4152        if result != ReturnCode::Blocked {
4153            *self.id().get_mut(store.0).get_mut_by_index(ty, handle)?.1 =
4154                VMTransmitLocalState::read(matches!(
4155                    (result, ty),
4156                    (ReturnCode::Dropped(_), TransmitIndex::Stream(_))
4157                ));
4158        }
4159
4160        log::trace!(
4161            "guest_read result for {transmit_handle:?} (handle {handle}; state {transmit_id:?}): {result:?}",
4162        );
4163
4164        Ok(result)
4165    }
4166
4167    fn wait_for_write(
4168        self,
4169        store: &mut StoreOpaque,
4170        caller: RuntimeComponentInstanceIndex,
4171        handle: TableId<TransmitHandle>,
4172    ) -> Result<ReturnCode> {
4173        let waitable = Waitable::Transmit(handle);
4174        store.wait_for_event(self.runtime_instance(caller), waitable)?;
4175        let event = waitable.take_event(store.concurrent_state_mut()?)?;
4176        if let Some(event @ (Event::StreamWrite { code, .. } | Event::FutureWrite { code, .. })) =
4177            event
4178        {
4179            waitable.on_delivery(store, self, event)?;
4180            Ok(code)
4181        } else {
4182            bail_bug!("expected either a stream or future write event")
4183        }
4184    }
4185
4186    /// Cancel a pending stream or future write.
4187    fn cancel_write(
4188        self,
4189        store: &mut StoreOpaque,
4190        caller: RuntimeComponentInstanceIndex,
4191        transmit_id: TableId<TransmitState>,
4192        async_: bool,
4193    ) -> Result<ReturnCode> {
4194        let state = store.concurrent_state_mut()?;
4195        let transmit = state.get_mut(transmit_id)?;
4196        log::trace!(
4197            "host_cancel_write state {transmit_id:?}; write state {:?} read state {:?}",
4198            transmit.read,
4199            transmit.write
4200        );
4201        let waitable = Waitable::Transmit(transmit.write_handle);
4202
4203        waitable.trap_if_has_sync_waiter(state)?;
4204        if !async_ {
4205            waitable.trap_if_in_waitable_set(state)?;
4206        }
4207
4208        let code = if let Some(event) = waitable.take_event(state)? {
4209            let (Event::FutureWrite { code, .. } | Event::StreamWrite { code, .. }) = event else {
4210                bail_bug!("expected either a stream or future write event")
4211            };
4212            waitable.on_delivery(store, self, event)?;
4213            match (code, event) {
4214                (ReturnCode::Completed(count), Event::StreamWrite { .. }) => {
4215                    ReturnCode::Cancelled(count)
4216                }
4217                (ReturnCode::Dropped(_) | ReturnCode::Completed(_), _) => code,
4218                _ => bail_bug!("unexpected code/event combo"),
4219            }
4220        } else if let ReadState::HostReady {
4221            cancel,
4222            cancel_waker,
4223            ..
4224        } = &mut state.get_mut(transmit_id)?.read
4225        {
4226            *cancel = true;
4227            if let Some(waker) = cancel_waker.take() {
4228                waker.wake();
4229            }
4230
4231            if async_ {
4232                ReturnCode::Blocked
4233            } else {
4234                let handle = store
4235                    .concurrent_state_mut()?
4236                    .get_mut(transmit_id)?
4237                    .write_handle;
4238                self.wait_for_write(store, caller, handle)?
4239            }
4240        } else {
4241            ReturnCode::Cancelled(ItemCount::ZERO)
4242        };
4243
4244        if !matches!(code, ReturnCode::Blocked) {
4245            let transmit = store.concurrent_state_mut()?.get_mut(transmit_id)?;
4246
4247            match &transmit.write {
4248                WriteState::GuestReady { .. } => {
4249                    transmit.write = WriteState::Open;
4250                }
4251                WriteState::HostReady { .. } => bail_bug!("support host write cancellation"),
4252                WriteState::Open | WriteState::Dropped => {}
4253            }
4254        }
4255
4256        log::trace!("cancelled write {transmit_id:?}: {code:?}");
4257
4258        Ok(code)
4259    }
4260
4261    fn wait_for_read(
4262        self,
4263        store: &mut StoreOpaque,
4264        caller: RuntimeComponentInstanceIndex,
4265        handle: TableId<TransmitHandle>,
4266    ) -> Result<ReturnCode> {
4267        let waitable = Waitable::Transmit(handle);
4268        store.wait_for_event(self.runtime_instance(caller), waitable)?;
4269        let event = waitable.take_event(store.concurrent_state_mut()?)?;
4270        if let Some(event @ (Event::StreamRead { code, .. } | Event::FutureRead { code, .. })) =
4271            event
4272        {
4273            waitable.on_delivery(store, self, event)?;
4274            Ok(code)
4275        } else {
4276            bail_bug!("expected either a stream or future read event")
4277        }
4278    }
4279
4280    /// Cancel a pending stream or future read.
4281    fn cancel_read(
4282        self,
4283        store: &mut StoreOpaque,
4284        caller: RuntimeComponentInstanceIndex,
4285        transmit_id: TableId<TransmitState>,
4286        async_: bool,
4287    ) -> Result<ReturnCode> {
4288        let state = store.concurrent_state_mut()?;
4289        let transmit = state.get_mut(transmit_id)?;
4290        log::trace!(
4291            "host_cancel_read state {transmit_id:?}; read state {:?} write state {:?}",
4292            transmit.read,
4293            transmit.write
4294        );
4295
4296        let waitable = Waitable::Transmit(transmit.read_handle);
4297
4298        waitable.trap_if_has_sync_waiter(state)?;
4299        if !async_ {
4300            waitable.trap_if_in_waitable_set(state)?;
4301        }
4302
4303        let code = if let Some(event) = waitable.take_event(state)? {
4304            let (Event::FutureRead { code, .. } | Event::StreamRead { code, .. }) = event else {
4305                bail_bug!("expected either a stream or future read event")
4306            };
4307            waitable.on_delivery(store, self, event)?;
4308            match (code, event) {
4309                (ReturnCode::Completed(count), Event::StreamRead { .. }) => {
4310                    ReturnCode::Cancelled(count)
4311                }
4312                (ReturnCode::Dropped(_) | ReturnCode::Completed(_), _) => code,
4313                _ => bail_bug!("unexpected code/event combo"),
4314            }
4315        } else if let WriteState::HostReady {
4316            cancel,
4317            cancel_waker,
4318            ..
4319        } = &mut state.get_mut(transmit_id)?.write
4320        {
4321            *cancel = true;
4322            if let Some(waker) = cancel_waker.take() {
4323                waker.wake();
4324            }
4325
4326            if async_ {
4327                ReturnCode::Blocked
4328            } else {
4329                let handle = store
4330                    .concurrent_state_mut()?
4331                    .get_mut(transmit_id)?
4332                    .read_handle;
4333                self.wait_for_read(store, caller, handle)?
4334            }
4335        } else {
4336            ReturnCode::Cancelled(ItemCount::ZERO)
4337        };
4338
4339        if !matches!(code, ReturnCode::Blocked) {
4340            let transmit = store.concurrent_state_mut()?.get_mut(transmit_id)?;
4341
4342            match &transmit.read {
4343                ReadState::GuestReady { .. } => {
4344                    transmit.read = ReadState::Open;
4345                }
4346                ReadState::HostReady { .. } | ReadState::HostToHost { .. } => {
4347                    bail_bug!("support host read cancellation")
4348                }
4349                ReadState::Open | ReadState::Dropped => {}
4350            }
4351        }
4352
4353        log::trace!("cancelled read {transmit_id:?}: {code:?}");
4354
4355        Ok(code)
4356    }
4357
4358    /// Cancel a pending write for the specified stream or future from the guest.
4359    fn guest_cancel_write(
4360        self,
4361        store: &mut StoreOpaque,
4362        caller: RuntimeComponentInstanceIndex,
4363        ty: TransmitIndex,
4364        async_: bool,
4365        writer: u32,
4366    ) -> Result<ReturnCode> {
4367        let (rep, state) =
4368            get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, writer)?;
4369        let id = TableId::<TransmitHandle>::new(rep);
4370        log::trace!("guest cancel write {id:?} (handle {writer})");
4371
4372        // This handle must be "busy" to have anything cancelled.
4373        match state.kind() {
4374            TransmitLocalStateKind::Write { .. } => {
4375                bail!("stream or future write cancelled when no write is pending")
4376            }
4377            TransmitLocalStateKind::Read { .. } => {
4378                bail!("passed read end to `{{stream|future}}.cancel-write`")
4379            }
4380            TransmitLocalStateKind::Cancelling => {
4381                bail!("stream or future write cancelled when a cancellation is already pending")
4382            }
4383            TransmitLocalStateKind::Busy => {}
4384        }
4385
4386        // Double-check that this is indeed the correct handle to cancel a write
4387        // from since `Busy` above didn't check that.
4388        let state = store.concurrent_state_mut()?;
4389        let transmit_id = state.get_mut(id)?.state;
4390        if state.get_mut(transmit_id)?.write_handle != id {
4391            bail!("passed read end to `{{stream|future}}.cancel-write`")
4392        }
4393
4394        let code = self.cancel_write(store, caller, transmit_id, async_)?;
4395        let state =
4396            get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, writer)?.1;
4397        if let TransmitLocalStateKind::Busy = state.kind() {
4398            *state = if matches!(code, ReturnCode::Blocked) {
4399                VMTransmitLocalState::cancelling()
4400            } else {
4401                VMTransmitLocalState::write(false)
4402            };
4403        }
4404        Ok(code)
4405    }
4406
4407    /// Cancel a pending read for the specified stream or future from the guest.
4408    fn guest_cancel_read(
4409        self,
4410        store: &mut StoreOpaque,
4411        caller: RuntimeComponentInstanceIndex,
4412        ty: TransmitIndex,
4413        async_: bool,
4414        reader: u32,
4415    ) -> Result<ReturnCode> {
4416        let (rep, state) =
4417            get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, reader)?;
4418        let id = TableId::<TransmitHandle>::new(rep);
4419        log::trace!("guest cancel read {id:?} (handle {reader})");
4420
4421        // This handle must be "busy" to have anything cancelled.
4422        match state.kind() {
4423            TransmitLocalStateKind::Read { .. } => {
4424                bail!("stream or future read cancelled when no read is pending")
4425            }
4426            TransmitLocalStateKind::Write { .. } => {
4427                bail!("passed write end to `{{stream|future}}.cancel-read`")
4428            }
4429            TransmitLocalStateKind::Cancelling => {
4430                bail!("stream or future read cancelled when a cancellation is already pending")
4431            }
4432            TransmitLocalStateKind::Busy => {}
4433        }
4434
4435        // Double-check that this is indeed the correct handle to cancel a read
4436        // from since `Busy` above didn't check that.
4437        let state = store.concurrent_state_mut()?;
4438        let transmit_id = state.get_mut(id)?.state;
4439        if state.get_mut(transmit_id)?.read_handle != id {
4440            bail!("passed write end to `{{stream|future}}.cancel-read`")
4441        }
4442
4443        let code = self.cancel_read(store, caller, transmit_id, async_)?;
4444        let state =
4445            get_mut_by_index_from(self.id().get_mut(store).table_for_transmit(ty), ty, reader)?.1;
4446        if let TransmitLocalStateKind::Busy = state.kind() {
4447            *state = if matches!(code, ReturnCode::Blocked) {
4448                VMTransmitLocalState::cancelling()
4449            } else {
4450                VMTransmitLocalState::read(false)
4451            };
4452        }
4453        Ok(code)
4454    }
4455
4456    /// Drop the readable end of the specified stream or future from the guest.
4457    fn guest_drop_readable(
4458        self,
4459        store: &mut StoreOpaque,
4460        ty: TransmitIndex,
4461        reader: u32,
4462    ) -> Result<()> {
4463        let table = self.id().get_mut(store).table_for_transmit(ty);
4464        let (rep, _is_done) = match ty {
4465            TransmitIndex::Stream(ty) => table.stream_remove_readable(ty, reader)?,
4466            TransmitIndex::Future(ty) => table.future_remove_readable(ty, reader)?,
4467        };
4468        let kind = match ty {
4469            TransmitIndex::Stream(_) => TransmitKind::Stream,
4470            TransmitIndex::Future(_) => TransmitKind::Future,
4471        };
4472        let id = TableId::<TransmitHandle>::new(rep);
4473        log::trace!("guest_drop_readable: drop reader {id:?}");
4474        store.host_drop_reader(id, kind)
4475    }
4476
4477    /// Create a new error context for the given component.
4478    pub(crate) fn error_context_new(
4479        self,
4480        store: &mut StoreOpaque,
4481        ty: TypeComponentLocalErrorContextTableIndex,
4482        options: OptionsIndex,
4483        debug_msg_address: u32,
4484        debug_msg_len: u32,
4485    ) -> Result<u32> {
4486        let lift_ctx = &mut LiftContext::new(store, options, self)?;
4487        let debug_msg = String::linear_lift_from_flat(
4488            lift_ctx,
4489            InterfaceType::String,
4490            &[ValRaw::u32(debug_msg_address), ValRaw::u32(debug_msg_len)],
4491        )?;
4492
4493        // Create a new ErrorContext that is tracked along with other concurrent state
4494        let err_ctx = ErrorContextState { debug_msg };
4495        let state = store.concurrent_state_mut()?;
4496        let table_id = state.push(err_ctx)?;
4497        let global_ref_count_idx =
4498            TypeComponentGlobalErrorContextTableIndex::from_u32(table_id.rep());
4499
4500        // Add to the global error context ref counts
4501        let _ = state
4502            .global_error_context_ref_counts
4503            .insert(global_ref_count_idx, GlobalErrorContextRefCount(1));
4504
4505        // Error context are tracked both locally (to a single component instance) and globally
4506        // the counts for both must stay in sync.
4507        //
4508        // Here we reflect the newly created global concurrent error context state into the
4509        // component instance's locally tracked count, along with the appropriate key into the global
4510        // ref tracking data structures to enable later lookup
4511        let (instance, counter) = self.id().get_mut_and_handle_counter(store);
4512        let local_idx = instance
4513            .error_context_table_insertion(ty, counter)
4514            .error_context(table_id.rep())?;
4515
4516        Ok(local_idx)
4517    }
4518
4519    /// Retrieve the debug message from the specified error context.
4520    pub(super) fn error_context_debug_message<T>(
4521        self,
4522        store: StoreContextMut<T>,
4523        ty: TypeComponentLocalErrorContextTableIndex,
4524        options: OptionsIndex,
4525        err_ctx_handle: u32,
4526        debug_msg_address: u32,
4527    ) -> Result<()> {
4528        // Retrieve the error context and internal debug message
4529        let handle_table_id_rep = self
4530            .id()
4531            .get_mut(store.0)
4532            .table_for_error_context(ty)
4533            .error_context_rep(err_ctx_handle)?;
4534
4535        let state = store.0.concurrent_state_mut()?;
4536        // Get the state associated with the error context
4537        let ErrorContextState { debug_msg } =
4538            state.get_mut(TableId::<ErrorContextState>::new(handle_table_id_rep))?;
4539        let debug_msg = debug_msg.clone();
4540
4541        let lower_cx = &mut LowerContext::new(store, options, self);
4542        let debug_msg_address = usize::try_from(debug_msg_address)?;
4543        if debug_msg_address % usize::try_from(<str as ComponentType>::ALIGN32)? != 0 {
4544            bail!("invalid debug message pointer: not aligned");
4545        }
4546        // Lower the string into the component's memory.
4547        //
4548        // Note that the "8" here is the size of a WIT `string` in linear
4549        // memory, the ptr+length. This'll need to be updated when `memory64`
4550        // comes along. (FIXME(#4311))
4551        let offset = lower_cx
4552            .as_slice()
4553            .get(debug_msg_address..)
4554            .and_then(|b| b.get(..8))
4555            .map(|_| debug_msg_address)
4556            .ok_or_else(|| crate::format_err!("invalid debug message pointer: out of bounds"))?;
4557        debug_msg
4558            .as_str()
4559            .linear_lower_to_memory(lower_cx, InterfaceType::String, offset)?;
4560
4561        Ok(())
4562    }
4563
4564    /// Implements the `future.cancel-read` intrinsic.
4565    pub(crate) fn future_cancel_read(
4566        self,
4567        store: &mut StoreOpaque,
4568        caller: RuntimeComponentInstanceIndex,
4569        ty: TypeFutureTableIndex,
4570        async_: bool,
4571        reader: u32,
4572    ) -> Result<u32> {
4573        self.guest_cancel_read(store, caller, TransmitIndex::Future(ty), async_, reader)
4574            .map(|v| v.encode())
4575    }
4576
4577    /// Implements the `future.cancel-write` intrinsic.
4578    pub(crate) fn future_cancel_write(
4579        self,
4580        store: &mut StoreOpaque,
4581        caller: RuntimeComponentInstanceIndex,
4582        ty: TypeFutureTableIndex,
4583        async_: bool,
4584        writer: u32,
4585    ) -> Result<u32> {
4586        self.guest_cancel_write(store, caller, TransmitIndex::Future(ty), async_, writer)
4587            .map(|v| v.encode())
4588    }
4589
4590    /// Implements the `stream.cancel-read` intrinsic.
4591    pub(crate) fn stream_cancel_read(
4592        self,
4593        store: &mut StoreOpaque,
4594        caller: RuntimeComponentInstanceIndex,
4595        ty: TypeStreamTableIndex,
4596        async_: bool,
4597        reader: u32,
4598    ) -> Result<u32> {
4599        self.guest_cancel_read(store, caller, TransmitIndex::Stream(ty), async_, reader)
4600            .map(|v| v.encode())
4601    }
4602
4603    /// Implements the `stream.cancel-write` intrinsic.
4604    pub(crate) fn stream_cancel_write(
4605        self,
4606        store: &mut StoreOpaque,
4607        caller: RuntimeComponentInstanceIndex,
4608        ty: TypeStreamTableIndex,
4609        async_: bool,
4610        writer: u32,
4611    ) -> Result<u32> {
4612        self.guest_cancel_write(store, caller, TransmitIndex::Stream(ty), async_, writer)
4613            .map(|v| v.encode())
4614    }
4615
4616    /// Implements the `future.drop-readable` intrinsic.
4617    pub(crate) fn future_drop_readable(
4618        self,
4619        store: &mut StoreOpaque,
4620        ty: TypeFutureTableIndex,
4621        reader: u32,
4622    ) -> Result<()> {
4623        self.guest_drop_readable(store, TransmitIndex::Future(ty), reader)
4624    }
4625
4626    /// Implements the `stream.drop-readable` intrinsic.
4627    pub(crate) fn stream_drop_readable(
4628        self,
4629        store: &mut StoreOpaque,
4630        ty: TypeStreamTableIndex,
4631        reader: u32,
4632    ) -> Result<()> {
4633        self.guest_drop_readable(store, TransmitIndex::Stream(ty), reader)
4634    }
4635
4636    /// Allocate a new future or stream and grant ownership of both the read and
4637    /// write ends to the (sub-)component instance to which the specified
4638    /// `TransmitIndex` belongs.
4639    fn guest_new(self, store: &mut StoreOpaque, ty: TransmitIndex) -> Result<ResourcePair> {
4640        let (write, read) = store
4641            .concurrent_state_mut()?
4642            .new_transmit(TransmitOrigin::guest(self.id().instance(), ty))?;
4643
4644        let (instance, counter) = self.id().get_mut_and_handle_counter(store);
4645        let mut insertion = instance.transmit_table_insertion(ty, counter);
4646        let (read_handle, write_handle) = match ty {
4647            TransmitIndex::Future(ty) => (
4648                insertion.future_read(ty, read.rep())?,
4649                insertion.future_write(ty, write.rep())?,
4650            ),
4651            TransmitIndex::Stream(ty) => (
4652                insertion.stream_read(ty, read.rep())?,
4653                insertion.stream_write(ty, write.rep())?,
4654            ),
4655        };
4656
4657        let state = store.concurrent_state_mut()?;
4658        state.get_mut(read)?.common.handle = Some(read_handle);
4659        state.get_mut(write)?.common.handle = Some(write_handle);
4660
4661        Ok(ResourcePair {
4662            write: write_handle,
4663            read: read_handle,
4664        })
4665    }
4666
4667    /// Drop the specified error context.
4668    pub(crate) fn error_context_drop(
4669        self,
4670        store: &mut StoreOpaque,
4671        ty: TypeComponentLocalErrorContextTableIndex,
4672        error_context: u32,
4673    ) -> Result<()> {
4674        let instance = self.id().get_mut(store);
4675
4676        let local_handle_table = instance.table_for_error_context(ty);
4677
4678        let rep = local_handle_table.error_context_drop(error_context)?;
4679
4680        let global_ref_count_idx = TypeComponentGlobalErrorContextTableIndex::from_u32(rep);
4681
4682        let state = store.concurrent_state_mut()?;
4683        let Some(GlobalErrorContextRefCount(global_ref_count)) = state
4684            .global_error_context_ref_counts
4685            .get_mut(&global_ref_count_idx)
4686        else {
4687            bail_bug!("retrieve concurrent state for error context during drop")
4688        };
4689
4690        // Reduce the component-global ref count, removing tracking if necessary
4691        if *global_ref_count < 1 {
4692            bail_bug!("ref count unexpectedly zero");
4693        }
4694        *global_ref_count -= 1;
4695        if *global_ref_count == 0 {
4696            state
4697                .global_error_context_ref_counts
4698                .remove(&global_ref_count_idx);
4699
4700            state
4701                .delete(TableId::<ErrorContextState>::new(rep))
4702                .context("deleting component-global error context data")?;
4703        }
4704
4705        Ok(())
4706    }
4707
4708    /// Transfer ownership of the specified stream or future read end from one
4709    /// guest to another.
4710    ///
4711    /// Maintains the invariant on `lower_transmit_to_index`: component
4712    /// validation requires `src` and `dst` to have equivalent payload types, so
4713    /// if `P::Item::typecheck` passed for `src`'s payload, it also passes for
4714    /// `dst`'s.
4715    fn guest_transfer(
4716        self,
4717        store: &mut StoreOpaque,
4718        src_idx: u32,
4719        src: TransmitIndex,
4720        dst: TransmitIndex,
4721    ) -> Result<u32> {
4722        let id = self.lift_index_to_transmit(store, src, src_idx)?;
4723        self.lower_transmit_to_index(store, dst, id)
4724    }
4725
4726    fn lift_index_to_transmit(
4727        self,
4728        store: &mut StoreOpaque,
4729        ty: TransmitIndex,
4730        src_idx: u32,
4731    ) -> Result<TableId<TransmitHandle>> {
4732        let (state, _, _, _, instance) = store.lift_context_parts(self);
4733        lift_index_to_transmit(instance, state.concurrent_state_mut(), ty, src_idx)
4734    }
4735
4736    /// Callers must maintain the invariant on the free function
4737    /// `lower_transmit_to_index`.
4738    fn lower_transmit_to_index(
4739        self,
4740        store: &mut StoreOpaque,
4741        ty: TransmitIndex,
4742        id: TableId<TransmitHandle>,
4743    ) -> Result<u32> {
4744        let (state, _, counter, _, instance) = store.lift_context_parts(self);
4745        lower_transmit_to_index(instance, counter, state.concurrent_state_mut(), ty, id)
4746    }
4747
4748    /// Implements the `future.new` intrinsic.
4749    pub(crate) fn future_new(
4750        self,
4751        store: &mut StoreOpaque,
4752        ty: TypeFutureTableIndex,
4753    ) -> Result<ResourcePair> {
4754        self.guest_new(store, TransmitIndex::Future(ty))
4755    }
4756
4757    /// Implements the `stream.new` intrinsic.
4758    pub(crate) fn stream_new(
4759        self,
4760        store: &mut StoreOpaque,
4761        ty: TypeStreamTableIndex,
4762    ) -> Result<ResourcePair> {
4763        self.guest_new(store, TransmitIndex::Stream(ty))
4764    }
4765
4766    /// Transfer ownership of the specified future read end from one guest to
4767    /// another.
4768    pub(crate) fn future_transfer(
4769        self,
4770        store: &mut StoreOpaque,
4771        src_idx: u32,
4772        src: TypeFutureTableIndex,
4773        dst: TypeFutureTableIndex,
4774    ) -> Result<u32> {
4775        self.guest_transfer(
4776            store,
4777            src_idx,
4778            TransmitIndex::Future(src),
4779            TransmitIndex::Future(dst),
4780        )
4781    }
4782
4783    /// Transfer ownership of the specified stream read end from one guest to
4784    /// another.
4785    pub(crate) fn stream_transfer(
4786        self,
4787        store: &mut StoreOpaque,
4788        src_idx: u32,
4789        src: TypeStreamTableIndex,
4790        dst: TypeStreamTableIndex,
4791    ) -> Result<u32> {
4792        self.guest_transfer(
4793            store,
4794            src_idx,
4795            TransmitIndex::Stream(src),
4796            TransmitIndex::Stream(dst),
4797        )
4798    }
4799
4800    /// Copy the specified error context from one component to another.
4801    pub(crate) fn error_context_transfer(
4802        self,
4803        store: &mut StoreOpaque,
4804        src_idx: u32,
4805        src: TypeComponentLocalErrorContextTableIndex,
4806        dst: TypeComponentLocalErrorContextTableIndex,
4807    ) -> Result<u32> {
4808        let (mut instance, counter) = self.id().get_mut_and_handle_counter(store);
4809        let rep = instance
4810            .as_mut()
4811            .table_for_error_context(src)
4812            .error_context_rep(src_idx)?;
4813        let dst_idx = instance
4814            .error_context_table_insertion(dst, counter)
4815            .error_context(rep)?;
4816
4817        // Update the global (cross-subcomponent) count for error contexts
4818        // as the new component has essentially created a new reference that will
4819        // be dropped/handled independently
4820        let global_ref_count = store
4821            .concurrent_state_mut()?
4822            .global_error_context_ref_counts
4823            .get_mut(&TypeComponentGlobalErrorContextTableIndex::from_u32(rep))
4824            .context("global ref count present for existing (sub)component error context")?;
4825
4826        global_ref_count.0 = global_ref_count
4827            .0
4828            .checked_add(1)
4829            .ok_or_else(|| format_err!(Trap::ReferenceCountOverflow))?;
4830
4831        Ok(dst_idx)
4832    }
4833}
4834
4835/// Performs the opertion of lifting a future or stream from and instance into
4836/// the `TransmitHandle` for it.
4837///
4838/// The `src_idx` is the guest-specified index within `instance` and `ty` is the
4839/// expected type of future/stream.
4840fn lift_index_to_transmit(
4841    instance: Pin<&mut ComponentInstance>,
4842    concurrent_state: &mut ConcurrentState,
4843    ty: TransmitIndex,
4844    src_idx: u32,
4845) -> Result<TableId<TransmitHandle>> {
4846    let handle_table = instance.table_for_transmit(ty);
4847    let (rep, is_done) = match ty {
4848        TransmitIndex::Future(idx) => handle_table.future_remove_readable(idx, src_idx)?,
4849        TransmitIndex::Stream(idx) => handle_table.stream_remove_readable(idx, src_idx)?,
4850    };
4851    let desc = match ty {
4852        TransmitIndex::Future(_) => "future",
4853        TransmitIndex::Stream(_) => "stream",
4854    };
4855    if is_done {
4856        bail!("cannot lift {desc} after being notified that the writable end dropped");
4857    }
4858    let id = TableId::<TransmitHandle>::new(rep);
4859    let future = concurrent_state.get_mut(id)?;
4860    if future.common.set.is_some() {
4861        bail!("cannot lift {desc} while it's in a waitable set");
4862    }
4863    future.common.handle = None;
4864
4865    let state = future.state;
4866    if concurrent_state.get_mut(state)?.done {
4867        match ty {
4868            TransmitIndex::Future(_) => bail!("cannot lift {desc} after previous read succeeded"),
4869            TransmitIndex::Stream(_) => bail!(Trap::LiftDroppedStream),
4870        };
4871    }
4872
4873    Ok(id)
4874}
4875
4876/// Performs the opertion of lowering a future or stream `TransmitHandle` into
4877/// an instance.
4878///
4879/// Invariant: if the read end of a stream or future is in a guest's handle
4880/// table under type `ty`, and it comes from a host `{Stream,Future}Producer`
4881/// `P`, then `P::Item::typecheck` has passed for `ty`'s payload. Apart from
4882/// `guest_new`, which only creates guest-written transmits, this is the only
4883/// place read ends are inserted into a guest's table, so callers must maintain
4884/// this invariant.
4885fn lower_transmit_to_index(
4886    instance: Pin<&mut ComponentInstance>,
4887    counter: &mut HandleCounter,
4888    concurrent_state: &mut ConcurrentState,
4889    ty: TransmitIndex,
4890    id: TableId<TransmitHandle>,
4891) -> Result<u32> {
4892    let state = concurrent_state.get_mut(id)?.state;
4893    debug_assert_eq!(concurrent_state.get_mut(state)?.read_handle, id);
4894    let mut insertion = instance.transmit_table_insertion(ty, counter);
4895    let handle = match ty {
4896        TransmitIndex::Future(idx) => insertion.future_read(idx, id.rep()),
4897        TransmitIndex::Stream(idx) => insertion.stream_read(idx, id.rep()),
4898    }?;
4899    concurrent_state.get_mut(id)?.common.handle = Some(handle);
4900    Ok(handle)
4901}
4902
4903impl ComponentInstance {
4904    fn transmit_instance(&self, ty: TransmitIndex) -> RuntimeComponentInstanceIndex {
4905        let types = self.component().types();
4906        match ty {
4907            TransmitIndex::Stream(ty) => types[ty].instance,
4908            TransmitIndex::Future(ty) => types[ty].instance,
4909        }
4910    }
4911
4912    fn table_for_transmit(self: Pin<&mut Self>, ty: TransmitIndex) -> &mut HandleTable {
4913        let runtime_instance = self.transmit_instance(ty);
4914        self.instance_state(runtime_instance).handle_table()
4915    }
4916
4917    fn transmit_table_insertion<'a>(
4918        self: Pin<&'a mut Self>,
4919        ty: TransmitIndex,
4920        counter: &'a mut HandleCounter,
4921    ) -> HandleTableInsertion<'a> {
4922        let runtime_instance = self.transmit_instance(ty);
4923        self.handle_table_insertion(runtime_instance, counter)
4924    }
4925
4926    fn table_for_error_context(
4927        self: Pin<&mut Self>,
4928        ty: TypeComponentLocalErrorContextTableIndex,
4929    ) -> &mut HandleTable {
4930        let runtime_instance = self.component().types()[ty].instance;
4931        self.instance_state(runtime_instance).handle_table()
4932    }
4933
4934    fn error_context_table_insertion<'a>(
4935        self: Pin<&'a mut Self>,
4936        ty: TypeComponentLocalErrorContextTableIndex,
4937        counter: &'a mut HandleCounter,
4938    ) -> HandleTableInsertion<'a> {
4939        let runtime_instance = self.component().types()[ty].instance;
4940        self.handle_table_insertion(runtime_instance, counter)
4941    }
4942
4943    fn get_mut_by_index(
4944        self: Pin<&mut Self>,
4945        ty: TransmitIndex,
4946        index: u32,
4947    ) -> Result<(u32, &mut VMTransmitLocalState)> {
4948        get_mut_by_index_from(self.table_for_transmit(ty), ty, index)
4949    }
4950}
4951
4952impl ConcurrentState {
4953    fn send_write_result(
4954        &mut self,
4955        ty: TransmitIndex,
4956        id: TableId<TransmitState>,
4957        handle: u32,
4958        code: ReturnCode,
4959    ) -> Result<()> {
4960        let write_handle = self.get_mut(id)?.write_handle.rep();
4961        self.set_event(
4962            write_handle,
4963            match ty {
4964                TransmitIndex::Future(ty) => Event::FutureWrite {
4965                    code,
4966                    pending: Some((ty, handle)),
4967                },
4968                TransmitIndex::Stream(ty) => Event::StreamWrite {
4969                    code,
4970                    pending: Some((ty, handle)),
4971                },
4972            },
4973        )
4974    }
4975
4976    fn send_read_result(
4977        &mut self,
4978        ty: TransmitIndex,
4979        id: TableId<TransmitState>,
4980        handle: u32,
4981        code: ReturnCode,
4982    ) -> Result<()> {
4983        let read_handle = self.get_mut(id)?.read_handle.rep();
4984        self.set_event(
4985            read_handle,
4986            match ty {
4987                TransmitIndex::Future(ty) => Event::FutureRead {
4988                    code,
4989                    pending: Some((ty, handle)),
4990                },
4991                TransmitIndex::Stream(ty) => Event::StreamRead {
4992                    code,
4993                    pending: Some((ty, handle)),
4994                },
4995            },
4996        )
4997    }
4998
4999    fn take_event(&mut self, waitable: u32) -> Result<Option<Event>> {
5000        Waitable::Transmit(TableId::<TransmitHandle>::new(waitable)).take_event(self)
5001    }
5002
5003    fn set_event(&mut self, waitable: u32, event: Event) -> Result<()> {
5004        Waitable::Transmit(TableId::<TransmitHandle>::new(waitable)).set_event(self, Some(event))
5005    }
5006
5007    /// Set or update the event for the specified waitable.
5008    ///
5009    /// If there is already an event set for this waitable, we assert that it is
5010    /// of the same variant as the new one and reuse the `ReturnCode` count and
5011    /// the `pending` field if applicable.
5012    // TODO: This is a bit awkward due to how
5013    // `Event::{Stream,Future}{Write,Read}` and
5014    // `ReturnCode::{Completed,Dropped,Cancelled}` are currently represented.
5015    // Consider updating those representations in a way that allows this
5016    // function to be simplified.
5017    fn update_event(&mut self, waitable: u32, event: Event) -> Result<()> {
5018        let waitable = Waitable::Transmit(TableId::<TransmitHandle>::new(waitable));
5019
5020        fn update_code(old: ReturnCode, new: ReturnCode) -> Result<ReturnCode> {
5021            let (ReturnCode::Completed(count)
5022            | ReturnCode::Dropped(count)
5023            | ReturnCode::Cancelled(count)) = old
5024            else {
5025                bail_bug!("unexpected old return code")
5026            };
5027
5028            Ok(match new {
5029                ReturnCode::Dropped(ItemCount::ZERO) => ReturnCode::Dropped(count),
5030                ReturnCode::Cancelled(ItemCount::ZERO) => ReturnCode::Cancelled(count),
5031                _ => bail_bug!("unexpected new return code"),
5032            })
5033        }
5034
5035        let event = match (waitable.take_event(self)?, event) {
5036            (None, _) => event,
5037            (Some(old @ Event::FutureWrite { .. }), Event::FutureWrite { .. }) => old,
5038            (Some(old @ Event::FutureRead { .. }), Event::FutureRead { .. }) => old,
5039            (
5040                Some(Event::StreamWrite {
5041                    code: old_code,
5042                    pending: old_pending,
5043                }),
5044                Event::StreamWrite { code, pending },
5045            ) => Event::StreamWrite {
5046                code: update_code(old_code, code)?,
5047                pending: old_pending.or(pending),
5048            },
5049            (
5050                Some(Event::StreamRead {
5051                    code: old_code,
5052                    pending: old_pending,
5053                }),
5054                Event::StreamRead { code, pending },
5055            ) => Event::StreamRead {
5056                code: update_code(old_code, code)?,
5057                pending: old_pending.or(pending),
5058            },
5059            _ => bail_bug!("unexpected event combination"),
5060        };
5061
5062        waitable.set_event(self, Some(event))
5063    }
5064
5065    /// Allocate a new future or stream, including the `TransmitState` and the
5066    /// `TransmitHandle`s corresponding to the read and write ends.
5067    fn new_transmit(
5068        &mut self,
5069        origin: TransmitOrigin,
5070    ) -> Result<(TableId<TransmitHandle>, TableId<TransmitHandle>)> {
5071        // All three entries must fit before inserting any of them so a full
5072        // table does not leave an inaccessible state or handle behind.
5073        self.table().ensure_capacity(3)?;
5074        let state_id = self.push(TransmitState::new(origin))?;
5075
5076        let write = self.push(TransmitHandle::new(state_id))?;
5077        let read = self.push(TransmitHandle::new(state_id))?;
5078
5079        let state = self.get_mut(state_id)?;
5080        state.write_handle = write;
5081        state.read_handle = read;
5082
5083        log::trace!("new transmit: state {state_id:?}; write {write:?}; read {read:?}",);
5084
5085        Ok((write, read))
5086    }
5087
5088    /// Delete the specified future or stream, including the read and write ends.
5089    fn delete_transmit(&mut self, state_id: TableId<TransmitState>) -> Result<()> {
5090        let state = self.delete(state_id)?;
5091        self.delete(state.write_handle)?;
5092        self.delete(state.read_handle)?;
5093
5094        log::trace!(
5095            "delete transmit: state {state_id:?}; write {:?}; read {:?}",
5096            state.write_handle,
5097            state.read_handle,
5098        );
5099
5100        Ok(())
5101    }
5102}
5103
5104pub(crate) struct ResourcePair {
5105    pub(crate) write: u32,
5106    pub(crate) read: u32,
5107}
5108
5109impl Waitable {
5110    /// Handle the imminent delivery of the specified event, e.g. by updating
5111    /// the state of the stream or future.
5112    pub(super) fn on_delivery(
5113        &self,
5114        store: &mut StoreOpaque,
5115        instance: Instance,
5116        event: Event,
5117    ) -> Result<()> {
5118        if let Event::FutureRead {
5119            code: ReturnCode::Dropped(_),
5120            ..
5121        }
5122        | Event::FutureWrite {
5123            code: ReturnCode::Dropped(_),
5124            ..
5125        }
5126        | Event::StreamRead {
5127            code: ReturnCode::Dropped(_),
5128            ..
5129        }
5130        | Event::StreamWrite {
5131            code: ReturnCode::Dropped(_),
5132            ..
5133        } = event
5134        {
5135            let Waitable::Transmit(transmit_handle) = self else {
5136                bail_bug!("unexpected `{event:?}` for `{self:?}`");
5137            };
5138            let state = store.concurrent_state_mut()?;
5139            let transmit_id = state.get_mut(*transmit_handle)?.state;
5140            state.get_mut(transmit_id)?.done = true;
5141        }
5142
5143        let instance = instance.id().get_mut(store);
5144        let (rep, state, code) = match event {
5145            Event::FutureRead {
5146                pending: Some((ty, handle)),
5147                code,
5148            }
5149            | Event::FutureWrite {
5150                pending: Some((ty, handle)),
5151                code,
5152            } => {
5153                let runtime_instance = instance.component().types()[ty].instance;
5154                let (rep, state) = instance.instance_states().0[runtime_instance]
5155                    .handle_table()
5156                    .future_rep(ty, handle)?;
5157                (rep, state, code)
5158            }
5159            Event::StreamRead {
5160                pending: Some((ty, handle)),
5161                code,
5162            }
5163            | Event::StreamWrite {
5164                pending: Some((ty, handle)),
5165                code,
5166            } => {
5167                let runtime_instance = instance.component().types()[ty].instance;
5168                let (rep, state) = instance.instance_states().0[runtime_instance]
5169                    .handle_table()
5170                    .stream_rep(ty, handle)?;
5171                (rep, state, code)
5172            }
5173            _ => return Ok(()),
5174        };
5175        if rep != self.rep() {
5176            bail_bug!("unexpected rep mismatch");
5177        }
5178        if !matches!(
5179            state.kind(),
5180            TransmitLocalStateKind::Busy | TransmitLocalStateKind::Cancelling
5181        ) {
5182            bail_bug!("expected state to be busy or cancelling");
5183        }
5184        let done = matches!(code, ReturnCode::Dropped(_));
5185        *state = match event {
5186            Event::FutureRead { .. } | Event::StreamRead { .. } => VMTransmitLocalState::read(done),
5187            Event::FutureWrite { .. } | Event::StreamWrite { .. } => {
5188                VMTransmitLocalState::write(done)
5189            }
5190            _ => bail_bug!("unexpected event for stream"),
5191        };
5192
5193        let transmit_handle = TableId::<TransmitHandle>::new(rep);
5194        let state = store.concurrent_state_mut()?;
5195        let transmit_id = state.get_mut(transmit_handle)?.state;
5196        let transmit = state.get_mut(transmit_id)?;
5197
5198        match event {
5199            Event::StreamRead { .. } => {
5200                transmit.read = ReadState::Open;
5201            }
5202            Event::StreamWrite { .. } => transmit.write = WriteState::Open,
5203            _ => {}
5204        }
5205        Ok(())
5206    }
5207}
5208
5209/// Determine whether an intra-component read/write is allowed for the specified
5210/// `stream` or `future` payload type according to the component model
5211/// specification.
5212fn allow_intra_component_read_write(ty: Option<&InterfaceType>) -> bool {
5213    matches!(
5214        ty,
5215        None | Some(
5216            InterfaceType::S8
5217                | InterfaceType::U8
5218                | InterfaceType::S16
5219                | InterfaceType::U16
5220                | InterfaceType::S32
5221                | InterfaceType::U32
5222                | InterfaceType::S64
5223                | InterfaceType::U64
5224                | InterfaceType::Float32
5225                | InterfaceType::Float64
5226        )
5227    )
5228}
5229
5230/// Helper structure to manage moving a `T` in/out of an interior `Mutex` which
5231/// contains an
5232/// `Option<T>`
5233struct LockedState<T> {
5234    inner: TryMutex<Option<T>>,
5235}
5236
5237impl<T> LockedState<T> {
5238    /// Creates a new initial state with `value` stored.
5239    fn new(value: T) -> Self {
5240        Self {
5241            inner: TryMutex::new(Some(value)),
5242        }
5243    }
5244
5245    /// Attempts to lock the inner mutex and return its guard.
5246    ///
5247    /// # Errors
5248    ///
5249    /// Fails if this lock is either poisoned or if it's currently locked.
5250    /// As-used in this file there should never actually be contention on this
5251    /// lock nor recursive access so failing to acquire the lock is a fatal
5252    /// error that gets propagated upwards.
5253    fn try_lock(&self) -> Result<TryMutexGuard<'_, Option<T>>> {
5254        match self.inner.try_lock() {
5255            Some(lock) => Ok(lock),
5256            None => bail_bug!("should not have contention on state lock"),
5257        }
5258    }
5259
5260    /// Takes the inner `T` out of this state, returning it as a guard which
5261    /// will put it back when finished.
5262    ///
5263    /// # Errors
5264    ///
5265    /// Returns an error if the state `T` isn't present.
5266    fn take(&self) -> Result<LockedStateGuard<'_, T>> {
5267        let result = self.try_lock()?.take();
5268        match result {
5269            Some(result) => Ok(LockedStateGuard {
5270                value: ManuallyDrop::new(result),
5271                state: self,
5272            }),
5273            None => bail_bug!("lock value unexpectedly missing"),
5274        }
5275    }
5276
5277    /// Performs the operation `f` on the inner state `&mut T`.
5278    ///
5279    /// This will acquire the internal lock and invoke `f`, so `f` should not
5280    /// expect to be able to recursively acquire this lock.
5281    ///
5282    /// # Errors
5283    ///
5284    /// Returns an error if the state `T` isn't present.
5285    fn with<R>(&self, f: impl FnOnce(&mut T) -> R) -> Result<R> {
5286        let mut inner = self.try_lock()?;
5287        match &mut *inner {
5288            Some(state) => Ok(f(state)),
5289            None => bail_bug!("lock value unexpectedly missing"),
5290        }
5291    }
5292}
5293
5294/// Helper structure returned from [`LockedState::take`] which will put the
5295/// state specified by `value` back into the original lock once this is dropped.
5296struct LockedStateGuard<'a, T> {
5297    value: ManuallyDrop<T>,
5298    state: &'a LockedState<T>,
5299}
5300
5301impl<T> Deref for LockedStateGuard<'_, T> {
5302    type Target = T;
5303
5304    fn deref(&self) -> &T {
5305        &self.value
5306    }
5307}
5308
5309impl<T> DerefMut for LockedStateGuard<'_, T> {
5310    fn deref_mut(&mut self) -> &mut T {
5311        &mut self.value
5312    }
5313}
5314
5315impl<T> Drop for LockedStateGuard<'_, T> {
5316    fn drop(&mut self) {
5317        // SAFETY: `ManuallyDrop::take` requires that after invoked the
5318        // original value is not read. This is the `Drop` for this type which
5319        // means we have exclusive ownership and it is not read further in the
5320        // destructor, satisfying this requirement.
5321        let value = unsafe { ManuallyDrop::take(&mut self.value) };
5322
5323        // If this fails due to contention that's a bug, but we're not in a
5324        // position to panic due to this being a destructor nor return an error,
5325        // so defer the bug to showing up later.
5326        if let Ok(mut lock) = self.state.try_lock() {
5327            *lock = Some(value);
5328        }
5329    }
5330}
5331
5332#[cfg(test)]
5333mod tests {
5334    use super::*;
5335    use crate::{Engine, Store};
5336    use core::future::pending;
5337    use core::pin::pin;
5338    use std::sync::LazyLock;
5339
5340    static ENGINE: LazyLock<Engine> = LazyLock::new(Engine::default);
5341
5342    fn poll_future_producer<T>(rx: Pin<&mut T>, finish: bool) -> Poll<Result<Option<T::Item>>>
5343    where
5344        T: FutureProducer<()>,
5345    {
5346        rx.poll_produce(
5347            &mut Context::from_waker(Waker::noop()),
5348            Store::new(&ENGINE, ()).as_context_mut(),
5349            finish,
5350        )
5351    }
5352
5353    #[test]
5354    fn future_producer() {
5355        let mut fut = pin!(async { crate::error::Ok(()) });
5356        assert!(matches!(
5357            poll_future_producer(fut.as_mut(), false),
5358            Poll::Ready(Ok(Some(()))),
5359        ));
5360
5361        let mut fut = pin!(async { crate::error::Ok(()) });
5362        assert!(matches!(
5363            poll_future_producer(fut.as_mut(), true),
5364            Poll::Ready(Ok(Some(()))),
5365        ));
5366
5367        let mut fut = pin!(pending::<Result<()>>());
5368        assert!(matches!(
5369            poll_future_producer(fut.as_mut(), false),
5370            Poll::Pending,
5371        ));
5372        assert!(matches!(
5373            poll_future_producer(fut.as_mut(), true),
5374            Poll::Ready(Ok(None)),
5375        ));
5376
5377        let (tx, rx) = oneshot::channel();
5378        let mut rx = pin!(rx);
5379        assert!(matches!(
5380            poll_future_producer(rx.as_mut(), false),
5381            Poll::Pending,
5382        ));
5383        assert!(matches!(
5384            poll_future_producer(rx.as_mut(), true),
5385            Poll::Ready(Ok(None)),
5386        ));
5387        tx.send(()).unwrap();
5388        assert!(matches!(
5389            poll_future_producer(rx.as_mut(), true),
5390            Poll::Ready(Ok(Some(()))),
5391        ));
5392
5393        let (tx, rx) = oneshot::channel();
5394        let mut rx = pin!(rx);
5395        tx.send(()).unwrap();
5396        assert!(matches!(
5397            poll_future_producer(rx.as_mut(), false),
5398            Poll::Ready(Ok(Some(()))),
5399        ));
5400
5401        let (tx, rx) = oneshot::channel::<()>();
5402        let mut rx = pin!(rx);
5403        drop(tx);
5404        assert!(matches!(
5405            poll_future_producer(rx.as_mut(), false),
5406            Poll::Ready(Err(..)),
5407        ));
5408
5409        let (tx, rx) = oneshot::channel::<()>();
5410        let mut rx = pin!(rx);
5411        drop(tx);
5412        assert!(matches!(
5413            poll_future_producer(rx.as_mut(), true),
5414            Poll::Ready(Err(..)),
5415        ));
5416    }
5417
5418    #[test]
5419    fn direct_destination_successive_writes() {
5420        use std::io::Write;
5421
5422        let mut store = Store::new(&ENGINE, ());
5423        let mut buffer = ();
5424        let mut dst = Vec::new();
5425        let mut marked_written = 0;
5426        let destination = Destination::<u8, ()> {
5427            id: TableId::new(0),
5428            buffer: &mut buffer,
5429            host_buffer: Some(HostBuffer {
5430                dst: &mut dst,
5431                marked_written: &mut marked_written,
5432            }),
5433            _phantom: PhantomData,
5434        };
5435
5436        // Each write must land after the bytes already marked as written,
5437        // not overwrite them.
5438        let mut direct = destination.as_direct(store.as_context_mut(), 6);
5439        direct.write_all(&[1, 2, 3]).unwrap();
5440        direct.write_all(&[4, 5, 6]).unwrap();
5441        drop(direct);
5442
5443        assert_eq!(marked_written, 6);
5444        assert_eq!(dst, [1, 2, 3, 4, 5, 6]);
5445    }
5446}