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
use std::{
io::{self, Write},
marker::PhantomData,
mem,
ops::{Deref, DerefMut, Index, IndexMut},
};
use serde::{Serialize, Serializer};
#[allow(unused_imports)]
use core_extensions::prelude::*;
use crate::std_types::{RSlice, RVec};
mod privacy {
use super::*;
#[repr(C)]
#[derive(StableAbi)]
#[sabi(inside_abi_stable_crate)]
#[sabi(bound = "T:'a")]
pub struct RSliceMut<'a, T> {
data: *mut T,
length: usize,
_marker: PhantomData<&'a mut T>,
}
impl_from_rust_repr! {
impl['a, T] From<&'a mut [T]> for RSliceMut<'a, T> {
fn(this){
RSliceMut {
data: this.as_mut_ptr(),
length: this.len(),
_marker: Default::default(),
}
}
}
}
impl<'a, T> RSliceMut<'a, T> {
#[inline(always)]
pub(super) const fn data(&self) -> *mut T {
self.data
}
#[inline(always)]
pub const fn len(&self) -> usize {
self.length
}
pub unsafe fn from_raw_parts_mut(ptr_: *mut T, len: usize) -> Self {
Self {
data: ptr_,
length: len,
_marker: PhantomData,
}
}
}
}
pub use self::privacy::RSliceMut;
impl<'a, T> RSliceMut<'a, T> {
pub fn from_mut(ref_:&'a mut T)->Self{
unsafe{
Self::from_raw_parts_mut(ref_,1)
}
}
pub fn slice<I>(&self, i: I) -> RSlice<'_, T>
where
[T]: Index<I, Output = [T]>,
{
self.as_slice().index(i).into()
}
pub fn slice_mut<'b, I>(&'b mut self, i: I) -> RSliceMut<'b, T>
where
[T]: IndexMut<I, Output = [T]>,
{
self.as_mut_slice().index_mut(i).into()
}
pub fn to_rvec(&self) -> RVec<T>
where
T: Clone,
{
self.to_vec().into()
}
unsafe fn as_slice_unbounded_lifetime(&self) -> &'a [T] {
::std::slice::from_raw_parts(self.data(), self.len())
}
unsafe fn as_mut_slice_unbounded_lifetime(&mut self) -> &'a mut [T] {
::std::slice::from_raw_parts_mut(self.data(), self.len())
}
pub fn as_slice(&self) -> &[T] {
unsafe { self.as_slice_unbounded_lifetime() }
}
pub fn into_slice(self) -> &'a [T] {
unsafe { self.as_slice_unbounded_lifetime() }
}
pub fn as_rslice(&self) -> RSlice<'_, T> {
self.as_slice().into()
}
pub fn into_rslice(self) -> RSlice<'a, T> {
self.into_slice().into()
}
pub fn as_mut_slice(&mut self) -> &mut [T] {
unsafe { self.as_mut_slice_unbounded_lifetime() }
}
pub fn into_slice_mut(mut self) -> &'a mut [T] {
unsafe { self.as_mut_slice_unbounded_lifetime() }
}
}
unsafe impl<'a, T> Send for RSliceMut<'a, T> where &'a mut [T]: Send {}
unsafe impl<'a, T> Sync for RSliceMut<'a, T> where &'a mut [T]: Sync {}
impl<'a, T> Default for RSliceMut<'a, T> {
fn default() -> Self {
(&mut [][..]).into()
}
}
impl<'a, T> IntoIterator for RSliceMut<'a, T> {
type Item = &'a mut T;
type IntoIter = ::std::slice::IterMut<'a, T>;
fn into_iter(self) -> ::std::slice::IterMut<'a, T> {
self.into_slice_mut().into_iter()
}
}
impl<'a, T> Deref for RSliceMut<'a, T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}
impl<'a, T> DerefMut for RSliceMut<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.as_mut_slice()
}
}
impl_into_rust_repr! {
impl['a, T] Into<&'a mut [T]> for RSliceMut<'a, T> {
fn(this){
this.into_slice_mut()
}
}
}
impl<'a, T> Into<&'a [T]> for RSliceMut<'a, T> {
fn into(self) -> &'a [T] {
self.into_slice()
}
}
impl<'a,T:'a> AsRef<[T]> for RSliceMut<'a,T>{
fn as_ref(&self)->&[T]{
self
}
}
impl<'a,T:'a> AsMut<[T]> for RSliceMut<'a,T>{
fn as_mut(&mut self)->&mut [T]{
self
}
}
impl<'a, T> Serialize for RSliceMut<'a, T>
where
T: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.as_slice().serialize(serializer)
}
}
impl<'a> Write for RSliceMut<'a, u8> {
#[inline]
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
let mut this = mem::replace(self, Self::default()).into_slice_mut();
let ret = this.write(data);
*self = this.into();
ret
}
#[inline]
fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
let mut this = mem::replace(self, Self::default()).into_slice_mut();
let ret = this.write_all(data);
*self = this.into();
ret
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
#[allow(dead_code)]
type SliceMut<'a, T> = &'a mut [T];
shared_impls! {
mod=slice_impls
new_type=RSliceMut['a][T],
original_type=SliceMut,
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn from_to_slice() {
let a = b"what the hell".to_vec();
let mut a_clone = a.clone();
let a_addr = a_clone.as_ptr();
let mut b = RSliceMut::from(&mut a_clone[..]);
assert_eq!(&*a, &*b);
assert_eq!(&*a, &mut *b);
assert_eq!(a_addr, b.data());
assert_eq!(a.len(), b.len());
}
}