1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
use crateAsyncInput;
/// An asynchronous input whose items are Unicode scalar values.
///
/// This trait adds no methods. Every [`AsyncInput<Item = char>`](AsyncInput),
/// including unsized streams, implements it automatically. Implement
/// [`AsyncInput`] to provide the operations; no separate implementation of
/// `AsyncCharInput` is needed.
///
/// Character counts measure Unicode scalar values, not encoded bytes or
/// grapheme clusters. This trait does not encode or decode text.
///
/// The inherited polling methods retain their pinning and readiness contracts;
/// implementors do not need to be `Unpin`.
///
/// # Examples
///
/// `CharSlice` is always ready, so it does not need to register a waker:
/// ```
/// use std::io;
/// use std::pin::Pin;
/// use std::task::Context;
/// use std::task::Poll;
/// use std::task::Waker;
///
/// use qubit_io::AsyncCharInput;
/// use qubit_io::AsyncInput;
///
/// struct CharSlice<'a> {
/// items: &'a [char],
/// position: usize,
/// }
///
/// impl AsyncInput for CharSlice<'_> {
/// type Item = char;
///
/// unsafe fn poll_read_unchecked(
/// self: Pin<&mut Self>,
/// _cx: &mut Context<'_>,
/// output: &mut [char],
/// index: usize,
/// count: usize,
/// ) -> Poll<io::Result<usize>> {
/// let this = self.get_mut();
/// let read = count.min(this.items.len() - this.position);
/// output[index..index + read]
/// .copy_from_slice(&this.items[this.position..this.position + read]);
/// this.position += read;
/// Poll::Ready(Ok(read))
/// }
/// }
///
/// fn read<S: AsyncCharInput + ?Sized>(
/// stream: Pin<&mut S>,
/// cx: &mut Context<'_>,
/// output: &mut [char],
/// ) -> Poll<io::Result<usize>> {
/// stream.poll_read(cx, output)
/// }
///
/// let mut stream = CharSlice { items: &['你', '🦀'], position: 0 };
/// let mut cx = Context::from_waker(Waker::noop());
/// let mut output = ['\0'; 2];
/// assert!(matches!(read(Pin::new(&mut stream), &mut cx, &mut output), Poll::Ready(Ok(2))));
/// assert_eq!(output, ['你', '🦀']);
/// assert!(matches!(read(Pin::new(&mut stream), &mut cx, &mut output), Poll::Ready(Ok(0))));
/// ```
///
/// Streams with a different item type do not satisfy this bound:
///
/// ```compile_fail
/// use qubit_io::AsyncCharInput;
/// use qubit_io::AsyncInput;
///
/// fn accept(stream: impl AsyncCharInput) {}
///
/// fn reject(stream: impl AsyncInput<Item = u8>) {
/// accept(stream);
/// }
/// ```