lexical_write_integer/
index.rs

1//! Wrapper around indexing for opt-in, additional safety.
2//!
3//! By default, writers tend to be safe, due to Miri, Valgrind,
4//! and other tests and careful validation against a wide range
5//! of randomized input. Parsers are much trickier to validate.
6
7// `index_unchecked_mut`'s 2nd arm is unused in `compact`.
8#![cfg_attr(feature = "compact", allow(unused_macros, unused_macro_rules))]
9#![doc(hidden)]
10
11/// Index a buffer, without bounds checking.
12#[cfg(not(feature = "safe"))]
13macro_rules! index_unchecked {
14    ($x:ident[$i:expr]) => {
15        *$x.get_unchecked($i)
16    };
17}
18
19/// Index a buffer and get a mutable reference, without bounds checking.
20// The `($x:ident[$i:expr] = $y:ident[$j:expr])` is not used with `compact`.
21// The newer version of the lint is `unused_macro_rules`, but this isn't
22// supported until nightly-2022-05-12.
23#[cfg(not(feature = "safe"))]
24#[allow(unknown_lints, unused_macro_rules)]
25macro_rules! index_unchecked_mut {
26    ($x:ident[$i:expr]) => {
27        *$x.get_unchecked_mut($i)
28    };
29
30    ($x:ident[$i:expr] = $y:ident[$j:expr]) => {
31        *$x.get_unchecked_mut($i) = *$y.get_unchecked($j)
32    };
33}
34
35/// Fill a slice with a value, without bounds checking.
36#[cfg(not(feature = "safe"))]
37macro_rules! slice_fill_unchecked {
38    ($slc:expr, $value:expr) => {
39        core::ptr::write_bytes($slc.as_mut_ptr(), $value, $slc.len())
40    };
41}
42
43/// Index a buffer, with bounds checking.
44#[cfg(feature = "safe")]
45macro_rules! index_unchecked {
46    ($x:ident[$i:expr]) => {
47        $x[$i]
48    };
49}
50
51/// Index a buffer and get a mutable reference, with bounds checking.
52// The `($x:ident[$i:expr] = $y:ident[$j:expr])` is not used with `compact`.
53// The newer version of the lint is `unused_macro_rules`, but this isn't
54// supported until nightly-2022-05-12.
55#[cfg(feature = "safe")]
56#[allow(unknown_lints, unused_macro_rules)]
57macro_rules! index_unchecked_mut {
58    ($x:ident[$i:expr]) => {
59        $x[$i]
60    };
61
62    ($x:ident[$i:expr] = $y:ident[$j:expr]) => {
63        $x[$i] = $y[$j]
64    };
65}
66
67/// Fill a slice with a value, without bounds checking.
68#[cfg(feature = "safe")]
69macro_rules! slice_fill_unchecked {
70    ($slc:expr, $value:expr) => {
71        $slc.fill($value)
72    };
73}