qubit-io 0.14.1

Runtime-neutral synchronous and asynchronous item-stream I/O for Rust
Documentation
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// =============================================================================
//    Copyright (c) 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Low-level unchecked slice helpers in a dedicated namespace.
//!
//! These helpers avoid bound checks and are intended for call sites that
//! already validate bounds in their own protocol.

use core::mem;
use std::convert::Infallible;
use std::io::{
    Error,
    ErrorKind,
    Result,
};

/// Namespace for low-level slice operations without bound checks.
///
/// All functions are unsafe and assume the caller has already validated their
/// preconditions. Safety requirements in each method are explicit.
pub struct UncheckedSlice {
    /// Prevents construction of this namespace type.
    _private: Infallible,
}

impl UncheckedSlice {
    /// Returns the exclusive end index of a checked slice range.
    ///
    /// # Parameters
    ///
    /// - `len`: Slice length.
    /// - `start`: Start index in the slice.
    /// - `count`: Number of requested items after `start`.
    ///
    /// # Returns
    ///
    /// `Some(end)` if `start + count <= len` and no overflow occurs, or
    /// `None` when the requested range does not fit inside the slice.
    #[inline]
    pub const fn range_end(
        len: usize,
        start: usize,
        count: usize,
    ) -> Option<usize> {
        match start.checked_add(count) {
            Some(end) if len >= end => Some(end),
            _ => None,
        }
    }

    /// Returns whether a slice has at least `count` readable/writable items
    /// from `start`.
    ///
    /// # Parameters
    ///
    /// - `len`: Slice length.
    /// - `start`: Start index in the slice.
    /// - `count`: Number of requested items after `start`.
    ///
    /// # Returns
    ///
    /// `true` if `start + count <= len` and no overflow occurs.
    #[must_use]
    #[inline(always)]
    pub const fn range_fits(len: usize, start: usize, count: usize) -> bool {
        Self::range_end(len, start, count).is_some()
    }

    /// Returns the exclusive end index of a checked slice range as an I/O
    /// result.
    ///
    /// # Parameters
    ///
    /// - `len`: Slice length.
    /// - `start`: Start index in the slice.
    /// - `count`: Number of requested items after `start`.
    /// - `message`: Error message used when the requested range is invalid.
    ///
    /// # Returns
    ///
    /// Returns the exclusive end index when the range fits inside the slice.
    ///
    /// # Errors
    ///
    /// Returns [`ErrorKind::InvalidInput`] with `message` when
    /// `start + count` overflows or exceeds `len`.
    #[inline]
    pub fn checked_range_end(
        len: usize,
        start: usize,
        count: usize,
        message: &'static str,
    ) -> Result<usize> {
        Self::range_end(len, start, count)
            .ok_or_else(|| Error::new(ErrorKind::InvalidInput, message))
    }

    /// Reads one value from an unchecked slice index.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Copyable element type read from the slice.
    ///
    /// # Parameters
    ///
    /// - `input`: Source slice.
    /// - `index`: Start index that must be valid for reading one item.
    ///
    /// # Returns
    ///
    /// A copy of the value stored at `index`.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `index < input.len()`.
    #[must_use]
    #[inline(always)]
    pub unsafe fn read<T: Copy>(input: &[T], index: usize) -> T {
        // SAFETY: The caller guarantees that `index` is in-bounds.
        unsafe { *input.as_ptr().add(index) }
    }

    /// Writes one value to an unchecked mutable slice index.
    ///
    /// This replaces the existing initialized element at `index`. The previous
    /// value is dropped before `value` is moved into the slot.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Element type stored by the slice.
    ///
    /// # Parameters
    ///
    /// - `output`: Destination slice.
    /// - `index`: Start index that must be valid for writing one item.
    /// - `value`: Value to write.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `index < output.len()`.
    #[inline(always)]
    pub unsafe fn write<T>(output: &mut [T], index: usize, value: T) {
        // SAFETY: The caller guarantees that `index` is in-bounds.
        unsafe {
            *output.as_mut_ptr().add(index) = value;
        }
    }

    /// Returns an immutable reference to one value at an unchecked slice index.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Element type stored by the slice.
    ///
    /// # Parameters
    ///
    /// - `input`: Source slice.
    /// - `index`: Start index that must be valid for reading one item.
    ///
    /// # Returns
    ///
    /// A shared reference to the value at `index`.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `index < input.len()`.
    #[must_use]
    #[inline(always)]
    pub unsafe fn get<T>(input: &[T], index: usize) -> &T {
        // SAFETY: The caller guarantees that `index` is in-bounds.
        unsafe { &*input.as_ptr().add(index) }
    }

    /// Returns a mutable reference to one value at an unchecked mutable slice
    /// index.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Element type stored by the slice.
    ///
    /// # Parameters
    ///
    /// - `output`: Destination slice.
    /// - `index`: Start index that must be valid for writing one item.
    ///
    /// # Returns
    ///
    /// An exclusive reference to the value at `index`.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `index < output.len()`.
    #[must_use]
    #[inline(always)]
    pub unsafe fn get_mut<T>(output: &mut [T], index: usize) -> &mut T {
        // SAFETY: The caller guarantees that `index` is in-bounds.
        unsafe { &mut *output.as_mut_ptr().add(index) }
    }

    /// Returns an immutable subslice at an unchecked offset and length.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Element type stored by the slice.
    ///
    /// # Parameters
    ///
    /// - `input`: Source slice.
    /// - `start`: Start index in `input`.
    /// - `count`: Number of items in the returned subslice.
    ///
    /// # Returns
    ///
    /// The shared subslice spanning the requested range.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested range does not fit.
    /// Callers do not need to repeat this range assertion.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `start + count <= input.len()` and that
    /// the addition does not overflow.
    #[must_use]
    #[inline(always)]
    pub unsafe fn subslice<T>(input: &[T], start: usize, count: usize) -> &[T] {
        debug_assert!(
            Self::range_fits(input.len(), start, count),
            "subslice range exceeds input buffer"
        );
        // SAFETY: The caller guarantees that the range is valid inside `input`.
        unsafe { core::slice::from_raw_parts(input.as_ptr().add(start), count) }
    }

    /// Returns a mutable subslice at an unchecked offset and length.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Element type stored by the slice.
    ///
    /// # Parameters
    ///
    /// - `output`: Destination slice.
    /// - `start`: Start index in `output`.
    /// - `count`: Number of items in the returned subslice.
    ///
    /// # Returns
    ///
    /// The exclusive subslice spanning the requested range.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested range does not fit.
    /// Callers do not need to repeat this range assertion.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `start + count <= output.len()` and that
    /// the addition does not overflow.
    #[must_use]
    #[inline(always)]
    pub unsafe fn subslice_mut<T>(
        output: &mut [T],
        start: usize,
        count: usize,
    ) -> &mut [T] {
        debug_assert!(
            Self::range_fits(output.len(), start, count),
            "subslice range exceeds output buffer"
        );
        // SAFETY: The caller guarantees that the range is valid inside
        // `output`.
        unsafe {
            core::slice::from_raw_parts_mut(
                output.as_mut_ptr().add(start),
                count,
            )
        }
    }

    /// Copies `count` values between unchecked slice offsets.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Copyable element type stored by both slices.
    ///
    /// # Parameters
    ///
    /// - `source`: Source slice.
    /// - `source_index`: Source offset, must be valid for `count` items.
    /// - `destination`: Destination slice.
    /// - `destination_index`: Destination offset, must be valid for `count`
    ///   items.
    /// - `count`: Number of items to copy.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if either requested range does not fit.
    /// Callers do not need to repeat these range assertions.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that both source and destination ranges are
    /// valid for `count` elements, the copy does not overflow pointer
    /// arithmetic, and the two memory regions do not overlap.
    #[inline(always)]
    pub unsafe fn copy_nonoverlapping<T: Copy>(
        source: &[T],
        source_index: usize,
        destination: &mut [T],
        destination_index: usize,
        count: usize,
    ) {
        debug_assert!(
            Self::range_fits(source.len(), source_index, count),
            "unchecked source range exceeds source buffer"
        );
        debug_assert!(
            Self::range_fits(destination.len(), destination_index, count),
            "unchecked destination range exceeds destination buffer"
        );
        // SAFETY: The caller guarantees both ranges are valid and
        // non-overlapping.
        unsafe {
            let src = source.as_ptr().add(source_index);
            let dst = destination.as_mut_ptr().add(destination_index);
            core::ptr::copy_nonoverlapping(src, dst, count);
        }
    }

    /// Copies `count` values between unchecked offsets in one buffer.
    ///
    /// Overlapping source and destination ranges are supported.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Copyable element type stored by the buffer.
    ///
    /// # Parameters
    ///
    /// - `buffer`: Buffer containing both ranges.
    /// - `source_index`: Source offset, must be valid for `count` items.
    /// - `destination_index`: Destination offset, must be valid for `count`
    ///   items.
    /// - `count`: Number of values to copy.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if either requested range does not fit.
    /// Callers do not need to repeat these range assertions.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that both ranges lie within `buffer` and that
    /// `source_index + count` and `destination_index + count` do not overflow
    /// `usize`.
    #[inline(always)]
    pub unsafe fn copy_within<T: Copy>(
        buffer: &mut [T],
        source_index: usize,
        destination_index: usize,
        count: usize,
    ) {
        debug_assert!(
            Self::range_fits(buffer.len(), source_index, count),
            "unchecked source range exceeds buffer"
        );
        debug_assert!(
            Self::range_fits(buffer.len(), destination_index, count),
            "unchecked destination range exceeds buffer"
        );
        // SAFETY: The caller guarantees both ranges are valid; `copy` supports
        // overlapping regions within the same allocation.
        unsafe {
            let base = buffer.as_mut_ptr();
            let source = base.add(source_index);
            let destination = base.add(destination_index);
            core::ptr::copy(source, destination, count);
        }
    }

    /// Reads one value from an unchecked unaligned byte slice offset.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Copyable value type represented by the source bytes.
    ///
    /// # Parameters
    ///
    /// - `input`: Source byte buffer.
    /// - `index`: Byte offset in `input`.
    ///
    /// # Returns
    ///
    /// The value reconstructed from the bytes at `index`.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested byte range does not fit.
    /// Callers do not need to repeat this range assertion.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `index..index + size_of::<T>()` is a
    /// valid readable range inside `input` and that the addition does not
    /// overflow. Every byte in that range must be initialized and together
    /// form a valid value of `T`, including all bit-validity and pointer
    /// provenance requirements imposed by `T`.
    ///
    /// `T: Copy` does not guarantee that an arbitrary byte sequence is a valid
    /// `T`. Primitive integer and floating-point types satisfy this
    /// representation requirement; types with restricted bit patterns,
    /// references, or pointers require additional justification from the
    /// caller.
    #[must_use]
    #[inline(always)]
    pub unsafe fn read_ne_unaligned<T: Copy>(input: &[u8], index: usize) -> T {
        debug_assert!(
            Self::range_fits(input.len(), index, mem::size_of::<T>()),
            "unchecked input range exceeds source buffer"
        );
        // SAFETY: The caller guarantees byte-level validity for this unaligned
        // load.
        unsafe {
            let src = input.as_ptr().add(index).cast::<T>();
            core::ptr::read_unaligned(src)
        }
    }

    /// Writes one value to an unchecked unaligned byte slice offset.
    ///
    /// # Type Parameters
    ///
    /// - `T`: Copyable value type whose object representation is written.
    ///
    /// # Parameters
    ///
    /// - `output`: Destination byte buffer.
    /// - `index`: Byte offset in `output`.
    /// - `value`: Value to write.
    ///
    /// # Panics
    ///
    /// Panics in debug builds if the requested byte range does not fit.
    /// Callers do not need to repeat this range assertion.
    ///
    /// # Safety
    ///
    /// The caller must guarantee that `index..index + size_of::<T>()` is a
    /// valid writable range inside `output` and that the addition does not
    /// overflow. The complete object representation of `value`, including any
    /// padding bytes, must be initialized and valid to store in and later
    /// observe through the destination byte slice.
    ///
    /// `T: Copy` does not guarantee initialized padding or unrestricted
    /// bytewise representation. Types containing padding, references, or
    /// pointers require additional justification from the caller.
    #[inline(always)]
    pub unsafe fn write_ne_unaligned<T: Copy>(
        output: &mut [u8],
        index: usize,
        value: T,
    ) {
        debug_assert!(
            Self::range_fits(output.len(), index, mem::size_of::<T>()),
            "unchecked output range exceeds destination buffer"
        );
        // SAFETY: The caller guarantees byte-level validity for this unaligned
        // store.
        unsafe {
            let dst = output.as_mut_ptr().add(index).cast::<T>();
            core::ptr::write_unaligned(dst, value);
        }
    }
}