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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
use crateCharInput;
use crateSeekableInput;
/// A synchronous input that transfers and seeks in Unicode scalar values.
///
/// This trait adds no methods. Every [`SeekableInput`] with both
/// `Item = char` and `Unit = char`, including unsized streams, implements it
/// automatically. Implement the underlying input and seek traits instead.
///
/// Positions count Unicode scalar values, not encoded bytes or grapheme
/// clusters. This trait does not encode or decode text, and does not promise
/// constant-time seeking.
/// Seeking to position 1 selects the second Unicode scalar, not a UTF-8 byte
/// offset.
///
/// # Examples
///
/// This memory stream checks seek bounds as an example policy; the trait does
/// not impose that restriction:
/// ```
/// use std::io;
/// use std::io::SeekFrom;
///
/// use qubit_io::Input;
/// use qubit_io::Seekable;
/// use qubit_io::SeekableCharInput;
///
/// struct CharCursor<'a> {
/// items: &'a [char],
/// position: usize,
/// }
///
/// impl Input for CharCursor<'_> {
/// type Item = char;
///
/// unsafe fn read_unchecked(
/// &mut self,
/// output: &mut [char],
/// index: usize,
/// count: usize,
/// ) -> io::Result<usize> {
/// let read = count.min(self.items.len() - self.position);
/// output[index..index + read]
/// .copy_from_slice(&self.items[self.position..self.position + read]);
/// self.position += read;
/// Ok(read)
/// }
/// }
///
/// impl Seekable for CharCursor<'_> {
/// type Unit = char;
///
/// fn seek_to(&mut self, position: SeekFrom) -> io::Result<u64> {
/// let target = match position {
/// SeekFrom::Start(offset) => i128::from(offset),
/// SeekFrom::Current(offset) => self.position as i128 + i128::from(offset),
/// SeekFrom::End(offset) => self.items.len() as i128 + i128::from(offset),
/// };
/// if target < 0 || target > self.items.len() as i128 {
/// return Err(io::Error::new(io::ErrorKind::InvalidInput, "outside the example input"));
/// }
/// self.position = target as usize;
/// Ok(self.position as u64)
/// }
/// }
///
/// fn rewind(stream: &mut dyn SeekableCharInput) -> io::Result<u64> {
/// stream.seek_to(SeekFrom::Start(0))
/// }
///
/// let mut stream = CharCursor { items: &['你', '🦀', 'a'], position: 0 };
/// assert_eq!(stream.seek_to(SeekFrom::Start(1)).unwrap(), 1);
/// let mut output = ['\0'; 1];
/// assert_eq!(stream.read(&mut output).unwrap(), 1);
/// assert_eq!(output, ['🦀']);
/// assert_eq!(rewind(&mut stream).unwrap(), 0);
/// assert_eq!(stream.read(&mut output).unwrap(), 1);
/// assert_eq!(output, ['你']);
/// ```
///
/// Streams with a different item type do not satisfy this bound:
///
/// ```compile_fail
/// use qubit_io::{SeekableCharInput, SeekableInput};
///
/// fn accept(stream: impl SeekableCharInput) {}
///
/// fn reject(stream: impl SeekableInput<Item = u8, Unit = u8>) {
/// accept(stream);
/// }
/// ```
///
/// The seek unit must also match the transferred items:
///
/// ```compile_fail
/// use qubit_io::{Input, Seekable, SeekableCharInput};
///
/// fn accept(stream: impl SeekableCharInput) {}
///
/// fn reject(stream: impl Input<Item = char> + Seekable<Unit = u8>) {
/// accept(stream);
/// }
/// ```