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
//! Provides semi-safe helpers for the `slice` type.
use SliceIndex;
/// Returns element or subslice at `index` without bounds checks in release
/// builds.
///
/// Use this only when surrounding code guarantees that `index` selects a valid
/// in-bounds region of `arr`. In debug builds this delegates to [`slice::get`]
/// and panics on invalid indexes or ranges. In release builds, violating that
/// precondition causes undefined behavior.
///
/// # Panics
///
/// Panics in debug builds if `index` is out of bounds for `arr`.
///
/// # Examples
///
/// ```
/// let values = [10, 20, 30];
/// let value = semisafe::slice::get(&values, 1);
/// assert_eq!(*value, 20);
/// ```
/// Returns mutable element or subslice at `index` without bounds checks in
/// release builds.
///
/// Use this only when surrounding code guarantees that `index` selects a valid
/// in-bounds region of `arr`. In debug builds this delegates to
/// [`slice::get_mut`] and panics on invalid indexes or ranges. In release
/// builds, violating that precondition causes undefined behavior.
///
/// # Panics
///
/// Panics in debug builds if `index` is out of bounds for `arr`.
///
/// # Examples
///
/// ```
/// let mut values = [10, 20, 30];
/// *semisafe::slice::get_mut(&mut values, 1) = 99;
/// assert_eq!(values, [10, 99, 30]);
/// ```
/// Splits `arr` at `index` without bounds checks in release builds.
///
/// Use this only when surrounding code guarantees that `index <= arr.len()`.
/// In debug builds this delegates to [`slice::split_at`] and panics when index
/// is out of bounds. In release builds, violating that precondition causes
/// undefined behavior.
///
/// # Panics
///
/// Panics in debug builds if `index > arr.len()`.
///
/// # Examples
///
/// ```
/// let values = [1, 2, 3, 4];
/// let (left, right) = semisafe::slice::split_at(&values, 2);
/// assert_eq!(left, &[1, 2]);
/// assert_eq!(right, &[3, 4]);
/// ```
pub const
/// Splits `arr` at `index` into two mutable slices without bounds checks in
/// release builds.
///
/// Use this only when surrounding code guarantees that `index <= arr.len()`.
/// In debug builds this delegates to [`slice::split_at_mut`] and panics when
/// index is out of bounds. In release builds, violating that precondition
/// causes undefined behavior.
///
/// # Panics
///
/// Panics in debug builds if `index > arr.len()`.
///
/// # Examples
///
/// ```
/// let mut values = [1, 2, 3, 4];
/// let (left, right) = semisafe::slice::split_at_mut(&mut values, 2);
/// left[0] = 10;
/// right[0] = 30;
/// assert_eq!(values, [10, 2, 30, 4]);
/// ```
pub const
/// Copies all elements from `src` into `dest`, using a memcpy.
///
/// The length of `src` must be the same as `dest`.
///
/// # Panics
///
/// This function will panic in debug builds if the two slices have different lengths.
///
/// # Examples
///
/// ```
/// let src = [1, 2, 3, 4];
/// let mut dest = [0, 0, 0, 0];
/// semisafe::slice::copy(&src, &mut dest);
/// assert_eq!(src, dest);
/// ```
pub const