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
// =============================================================================
// Copyright (c) 2026 Haixing Hu.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0.
// =============================================================================
use crateCharOutput;
use crateSeekableOutput;
/// A synchronous output that transfers and seeks in Unicode scalar values.
///
/// This trait adds no methods. Every [`SeekableOutput`] with both
/// `Item = char` and `Unit = char`, including unsized streams, implements it
/// automatically. Implement the underlying output 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.
///
/// # Examples
/// This example seeks by Unicode scalar value; its bounds are an example
/// policy:
///
/// ```
/// use std::io;
/// use std::io::SeekFrom;
/// use qubit_io::{Output, Seekable, SeekableCharOutput};
/// struct CharCursor { items: Vec<char>, position: usize }
/// impl Output for CharCursor {
/// type Item = char;
/// unsafe fn write_unchecked(&mut self, input: &[char], index: usize, count: usize) -> io::Result<usize> {
/// let n = count.min(self.items.len() - self.position);
/// self.items[self.position..self.position + n].copy_from_slice(&input[index..index + n]);
/// self.position += n;
/// Ok(n)
/// }
/// fn flush(&mut self) -> io::Result<()> { Ok(()) }
/// }
/// 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 output"));
/// }
/// self.position = target as usize;
/// Ok(self.position as u64)
/// }
/// }
/// let mut cursor = CharCursor { items: vec!['你', '🦀'], position: 0 };
/// let stream: &mut dyn SeekableCharOutput = &mut cursor;
/// assert_eq!(stream.seek_to(SeekFrom::Start(1))?, 1);
/// stream.write_fully(&['好'])?;
/// assert_eq!(stream.seek_to(SeekFrom::Start(0))?, 0);
/// assert_eq!(cursor.items, ['你', '好']);
/// assert_eq!(cursor.position, 0);
/// # Ok::<(), io::Error>(())
/// ```
/// Streams with a different item type do not satisfy this bound:
///
/// ```compile_fail
/// use qubit_io::{SeekableCharOutput, SeekableOutput};
///
/// fn accept(stream: impl SeekableCharOutput) {}
///
/// fn reject(stream: impl SeekableOutput<Item = u8, Unit = u8>) {
/// accept(stream);
/// }
/// ```
///
/// The seek unit must also match the transferred items:
///
/// ```compile_fail
/// use qubit_io::{Output, Seekable, SeekableCharOutput};
///
/// fn accept(stream: impl SeekableCharOutput) {}
///
/// fn reject(stream: impl Output<Item = char> + Seekable<Unit = u8>) {
/// accept(stream);
/// }
/// ```