-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.rs
584 lines (584 loc) · 20 KB
/
example.rs
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#![feature(prelude_import)]
#![feature(rustc_attrs)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
use std::{mem::ManuallyDrop, ptr::NonNull, marker::PhantomData, ops::{Deref, DerefMut}};
use bytecheck::CheckBytes;
use rkyv::{
Archive, Deserialize, Serialize, Archived, Resolver, ArchiveUnsized,
boxed::ArchivedBox, SerializeUnsized,
};
use trace::trace;
const DEPTH: ::std::thread::LocalKey<::std::cell::Cell<usize>> = {
#[inline]
fn __init() -> ::std::cell::Cell<usize> {
::std::cell::Cell::new(0)
}
#[inline]
unsafe fn __getit(
init: ::std::option::Option<&mut ::std::option::Option<::std::cell::Cell<usize>>>,
) -> ::std::option::Option<&'static ::std::cell::Cell<usize>> {
#[thread_local]
#[cfg(
all(
target_thread_local,
not(all(target_family = "wasm", not(target_feature = "atomics"))),
)
)]
static __KEY: ::std::thread::__FastLocalKeyInner<::std::cell::Cell<usize>> = ::std::thread::__FastLocalKeyInner::new();
#[allow(unused_unsafe)]
unsafe {
__KEY
.get(move || {
if let ::std::option::Option::Some(init) = init {
if let ::std::option::Option::Some(value) = init.take() {
return value;
} else if true {
::core::panicking::panic_fmt(
::core::fmt::Arguments::new_v1(
&["internal error: entered unreachable code: "],
&[
::core::fmt::ArgumentV1::new_display(
&::core::fmt::Arguments::new_v1(
&["missing default value"],
&[],
),
),
],
),
);
}
}
__init()
})
}
}
unsafe { ::std::thread::LocalKey::new(__getit) }
};
pub enum Foo {
First(bool),
Second(u32),
OutOfLine(SmallSliceBox<i32>),
}
#[automatically_derived]
///An archived [`Foo`]
#[repr(u8)]
pub enum ArchivedFoo
where
bool: ::rkyv::Archive,
u32: ::rkyv::Archive,
SmallSliceBox<i32>: ::rkyv::Archive,
{
///The archived counterpart of [`Foo::First`]
#[allow(dead_code)]
First(
///The archived counterpart of [`Foo::First::0`]
::rkyv::Archived<bool>,
),
///The archived counterpart of [`Foo::Second`]
#[allow(dead_code)]
Second(
///The archived counterpart of [`Foo::Second::0`]
::rkyv::Archived<u32>,
),
///The archived counterpart of [`Foo::OutOfLine`]
#[allow(dead_code)]
OutOfLine(
///The archived counterpart of [`Foo::OutOfLine::0`]
::rkyv::Archived<SmallSliceBox<i32>>,
),
}
#[automatically_derived]
///The resolver for an archived [`Foo`]
pub enum FooResolver
where
bool: ::rkyv::Archive,
u32: ::rkyv::Archive,
SmallSliceBox<i32>: ::rkyv::Archive,
{
///The resolver for [`Foo::First`]
#[allow(dead_code)]
First(
///The resolver for [`Foo::First::0`]
::rkyv::Resolver<bool>,
),
///The resolver for [`Foo::Second`]
#[allow(dead_code)]
Second(
///The resolver for [`Foo::Second::0`]
::rkyv::Resolver<u32>,
),
///The resolver for [`Foo::OutOfLine`]
#[allow(dead_code)]
OutOfLine(
///The resolver for [`Foo::OutOfLine::0`]
::rkyv::Resolver<SmallSliceBox<i32>>,
),
}
#[automatically_derived]
const _: () = {
use ::core::marker::PhantomData;
use ::rkyv::{out_field, Archive, Archived};
#[repr(u8)]
enum ArchivedTag {
First,
Second,
OutOfLine,
}
#[repr(C)]
struct ArchivedVariantFirst(
ArchivedTag,
Archived<bool>,
PhantomData<Foo>,
)
where
bool: ::rkyv::Archive,
u32: ::rkyv::Archive,
SmallSliceBox<i32>: ::rkyv::Archive;
#[repr(C)]
struct ArchivedVariantSecond(
ArchivedTag,
Archived<u32>,
PhantomData<Foo>,
)
where
bool: ::rkyv::Archive,
u32: ::rkyv::Archive,
SmallSliceBox<i32>: ::rkyv::Archive;
#[repr(C)]
struct ArchivedVariantOutOfLine(
ArchivedTag,
Archived<SmallSliceBox<i32>>,
PhantomData<Foo>,
)
where
bool: ::rkyv::Archive,
u32: ::rkyv::Archive,
SmallSliceBox<i32>: ::rkyv::Archive;
impl Archive for Foo
where
bool: ::rkyv::Archive,
u32: ::rkyv::Archive,
SmallSliceBox<i32>: ::rkyv::Archive,
{
type Archived = ArchivedFoo;
type Resolver = FooResolver;
#[allow(clippy::unit_arg)]
#[inline]
unsafe fn resolve(
&self,
pos: usize,
resolver: Self::Resolver,
out: *mut Self::Archived,
) {
match resolver {
FooResolver::First(resolver_0) => {
match self {
Foo::First(self_0) => {
let out = out.cast::<ArchivedVariantFirst>();
(&raw mut (*out).0).write(ArchivedTag::First);
let (fp, fo) = {
#[allow(unused_unsafe)]
unsafe {
let fo = &raw mut (*out).1;
(fo.cast::<u8>().offset_from(out.cast::<u8>()) as usize, fo)
}
};
::rkyv::Archive::resolve(self_0, pos + fp, resolver_0, fo);
}
#[allow(unreachable_patterns)]
_ => ::core::hint::unreachable_unchecked(),
}
}
FooResolver::Second(resolver_0) => {
match self {
Foo::Second(self_0) => {
let out = out.cast::<ArchivedVariantSecond>();
(&raw mut (*out).0).write(ArchivedTag::Second);
let (fp, fo) = {
#[allow(unused_unsafe)]
unsafe {
let fo = &raw mut (*out).1;
(fo.cast::<u8>().offset_from(out.cast::<u8>()) as usize, fo)
}
};
::rkyv::Archive::resolve(self_0, pos + fp, resolver_0, fo);
}
#[allow(unreachable_patterns)]
_ => ::core::hint::unreachable_unchecked(),
}
}
FooResolver::OutOfLine(resolver_0) => {
match self {
Foo::OutOfLine(self_0) => {
let out = out.cast::<ArchivedVariantOutOfLine>();
(&raw mut (*out).0).write(ArchivedTag::OutOfLine);
let (fp, fo) = {
#[allow(unused_unsafe)]
unsafe {
let fo = &raw mut (*out).1;
(fo.cast::<u8>().offset_from(out.cast::<u8>()) as usize, fo)
}
};
::rkyv::Archive::resolve(self_0, pos + fp, resolver_0, fo);
}
#[allow(unreachable_patterns)]
_ => ::core::hint::unreachable_unchecked(),
}
}
}
}
}
};
#[automatically_derived]
const _: () = {
use ::rkyv::{Archive, Fallible, Serialize};
impl<__S: Fallible + ?Sized> Serialize<__S> for Foo
where
bool: Serialize<__S>,
u32: Serialize<__S>,
SmallSliceBox<i32>: Serialize<__S>,
{
#[inline]
fn serialize(
&self,
serializer: &mut __S,
) -> ::core::result::Result<Self::Resolver, __S::Error> {
Ok(
match self {
Self::First(_0) => {
FooResolver::First(Serialize::<__S>::serialize(_0, serializer)?)
}
Self::Second(_0) => {
FooResolver::Second(Serialize::<__S>::serialize(_0, serializer)?)
}
Self::OutOfLine(_0) => {
FooResolver::OutOfLine(
Serialize::<__S>::serialize(_0, serializer)?,
)
}
},
)
}
}
};
#[automatically_derived]
const _: () = {
use ::rkyv::{Archive, Archived, Deserialize, Fallible};
impl<__D: Fallible + ?Sized> Deserialize<Foo, __D> for Archived<Foo>
where
bool: Archive,
Archived<bool>: Deserialize<bool, __D>,
u32: Archive,
Archived<u32>: Deserialize<u32, __D>,
SmallSliceBox<i32>: Archive,
Archived<SmallSliceBox<i32>>: Deserialize<SmallSliceBox<i32>, __D>,
{
#[inline]
fn deserialize(
&self,
deserializer: &mut __D,
) -> ::core::result::Result<Foo, __D::Error> {
Ok(
match self {
Self::First(_0) => {
Foo::First(
Deserialize::<bool, __D>::deserialize(_0, deserializer)?,
)
}
Self::Second(_0) => {
Foo::Second(
Deserialize::<u32, __D>::deserialize(_0, deserializer)?,
)
}
Self::OutOfLine(_0) => {
Foo::OutOfLine(
Deserialize::<
SmallSliceBox<i32>,
__D,
>::deserialize(_0, deserializer)?,
)
}
},
)
}
}
};
/// A packed alternative to `Box<[T]>` for slices with at most 2ˆ32 (4_294_967_296) elements.
///
/// A normal `Box<[T]>` is an owned 'fat pointer' that contains both the 'raw' pointer to memory
/// as well as the size (as an usize) of the managed slice.
///
/// On 64-bit targets (where sizeof(usize) == sizeof(u64)), this makes a `Box<[T]>` take up 16 bytes (128 bits, 2 words).
/// That's a shame: It means that if you build an enum that contains a `Box<[T]>`,
/// then it will at least require 24 bytes (196 bits, 3 words) of stack memory.
///
/// But it is rather common to work with slices that will never be that large:
/// a `[u8; 2^32]` takes 4GiB of space. Are you really working with strings that are larger in your app?
///
/// And since the length is counted in elements, a `[u64; 2^32]` takes 32GiB.
///
/// By storing the length of such a 'fat pointer' inside a u32 rather than a u64,
/// a SmallSliceBox only takes up 12 bytes (96 bits, 1.5 words) rather than 16 bytes.
///
/// This allows it to be used inside another structure, such as in one or more variants of an enum.
/// The resulting structure will then still only take up 16 bytes.
///
/// In situations where you are trying to optimize for memory usage, cache locality, etc,
/// this might make a difference.
///
/// # Niche optimization
/// Just like a normal Box, `sizeof(Option<SmallSliceBox<T>>) == sizeof(SmallSliceBox<T>)`.
///
/// # Rkyv
/// rkyv's Archive, Serialize and Deserialize have been implemented for SmallSliceBox.
/// The serialized version of a SmallSliceBox<T> is 'just' a normal `rkyv::ArchivedBox<[T]>`.
/// This is a match made in heaven, since rkyv's relative pointers use only 32 bits for the pointer part _as well as_ the length part.
/// As such, `sizeof(rkyv::Archived<SmallSliceBox<T>>) == 8` bytes (!).
/// (This is assuming rkyv's feature `size_32` is used which is the default.
/// Changing it to `size_64` is rarely useful for the same reason as the rant about lengths above.)
#[repr(packed)]
pub struct SmallSliceBox<T> {
ptr: core::ptr::NonNull<T>,
size: u32,
}
pub struct SmallSliceBoxResolver<T>(
rkyv::boxed::BoxResolver<<[T] as ArchiveUnsized>::MetadataResolver>,
)
where
Box<[T]>: Archive,
[T]: ArchiveUnsized;
/// SmallSliceBox is archived into an ArchivedBox<[T]>, just like a normal box.
impl<T> Archive for SmallSliceBox<T>
where
Box<[T]>: Archive,
[T]: ArchiveUnsized,
{
type Archived = ArchivedBox<<[T] as ArchiveUnsized>::Archived>;
type Resolver = SmallSliceBoxResolver<T>;
#[inline]
unsafe fn resolve(
&self,
pos: usize,
resolver: Self::Resolver,
out: *mut Self::Archived,
) {
{
::std::io::_print(
::core::fmt::Arguments::new_v1(
&["Resolving ", "\n"],
&[::core::fmt::ArgumentV1::new_debug(&(&self as *const _))],
),
);
};
rkyv::boxed::ArchivedBox::resolve_from_ref(self.as_ref(), pos, resolver.0, out)
}
}
impl<S: rkyv::Fallible + ?Sized, T> Serialize<S> for SmallSliceBox<T>
where
Box<[T]>: Serialize<S>,
[T]: SerializeUnsized<S>,
{
#[inline]
fn serialize(&self, serializer: &mut S) -> Result<Self::Resolver, S::Error> {
{
::std::io::_print(
::core::fmt::Arguments::new_v1(
&["Serializing ", "\n"],
&[::core::fmt::ArgumentV1::new_debug(&(&self as *const _))],
),
);
};
let res = ArchivedBox::serialize_from_ref(self.as_ref(), serializer)?;
Ok(SmallSliceBoxResolver(res))
}
}
impl<T, D> Deserialize<SmallSliceBox<T>, D>
for ArchivedBox<<[T] as ArchiveUnsized>::Archived>
where
[T]: ArchiveUnsized,
<[T] as ArchiveUnsized>::Archived: rkyv::DeserializeUnsized<[T], D>,
D: rkyv::Fallible + ?Sized,
{
fn deserialize(&self, deserializer: &mut D) -> Result<SmallSliceBox<T>, D::Error> {
{
::std::io::_print(
::core::fmt::Arguments::new_v1(
&["Deserializing ", "\n"],
&[::core::fmt::ArgumentV1::new_debug(&(&self as *const _))],
),
);
};
let boxed: Box<[T]> = self.deserialize(deserializer)?;
Ok(SmallSliceBox::from_box(boxed))
}
}
impl<T> SmallSliceBox<T> {
/// Creates a new SmallSliceBox from the given slice.
///
/// This involves cloning the slice (which will clone all elements one by one)
/// first into a vector, which is then turned into the SmallSliceBox.
///
/// Panics if the slice's length cannot fit in a u32.
pub fn new(slice: &[T]) -> Self
where
T: Clone,
{
let boxed: Box<[T]> = slice.to_vec().into_boxed_slice();
Self::from_box(boxed)
}
/// Creates a new SmallSliceBox from the given slice.
///
/// This involves cloning the slice (which will clone all elements one by one)
/// first into a vector, which is then turned into the SmallSliceBox.
///
/// # Safety
/// The caller must ensure that `slice`'s length can fit in a u32.
pub unsafe fn new_unchecked(slice: &[T]) -> Self
where
T: Clone,
{
let boxed: Box<[T]> = slice.to_vec().into_boxed_slice();
Self::from_box_unchecked(boxed)
}
/// Variant of `new` which will return an error if the slice is too long instead of panicing.
pub fn try_new(slice: &[T]) -> Result<Self, <u32 as TryFrom<usize>>::Error>
where
T: Clone,
{
let boxed: Box<[T]> = slice.to_vec().into_boxed_slice();
Self::try_from_box(boxed)
}
/// Optimization of `new` for types implementing `Copy`
///
/// Does not need to work with an intermediate vec,
/// and creating a copy from the slice is much faster.
///
/// Panics if the slice's length cannot fit in a u32.
pub fn new_from_copy(slice: &[T]) -> Self
where
T: Copy,
{
let boxed: Box<[T]> = slice.into();
Self::from_box(boxed)
}
/// Variant of `new_from_copy` which will return an error if the slice is too long instead of panicing.
pub fn try_new_from_copy(slice: &[T]) -> Result<Self, <u32 as TryFrom<usize>>::Error>
where
T: Copy,
{
let boxed: Box<[T]> = slice.into();
Self::try_from_box(boxed)
}
/// Variant of `from_box` which will return an error if the slice is too long instead of panicing.
pub fn try_from_box(
boxed: Box<[T]>,
) -> Result<Self, <u32 as TryFrom<usize>>::Error> {
{
::std::io::_print(::core::fmt::Arguments::new_v1(&["Hello\n"], &[]));
};
let size = boxed.len().try_into()?;
let fat_ptr = Box::into_raw(boxed);
let thin_ptr = fat_ptr as *mut T;
let ptr = unsafe { core::ptr::NonNull::new_unchecked(thin_ptr) };
let res = SmallSliceBox { ptr, size };
Ok(res)
}
/// Turns a Box into a SmallSliceBox.
///
/// This is a fast constant-time operation that needs no allocation.
///
/// Panics if the slice's length cannot fit in a u32.
pub fn from_box(boxed: Box<[T]>) -> Self {
Self::try_from_box(boxed).unwrap()
}
/// Variant of `from_box` that will not check whether the slice is short enough.
///
/// # Safety
/// - The caller needs to ensure that the slice never has more elements than can fit in a u32.
pub unsafe fn from_box_unchecked(boxed: Box<[T]>) -> Self {
Self::try_from_box(boxed).unwrap_unchecked()
}
}
impl<T> SmallSliceBox<T> {
/// Turns a SmallSliceBox into a box.
///
/// This is a fast constant-time operation that needs no allocation.
///
/// Not an associated function to not interfere with Deref
pub fn to_box(this: Self) -> Box<[T]> {
{
::std::io::_print(::core::fmt::Arguments::new_v1(&["to_box called\n"], &[]));
};
let ptr = core::ptr::slice_from_raw_parts(this.ptr.as_ptr(), this.size as usize)
as *mut _;
let res = unsafe { Box::from_raw(ptr) };
core::mem::forget(this);
res
}
}
impl<T> Drop for SmallSliceBox<T> {
fn drop(&mut self) {
let me = std::mem::replace(
self,
SmallSliceBox {
ptr: NonNull::dangling(),
size: 0,
},
);
core::mem::forget(self);
let _drop_this_box = SmallSliceBox::to_box(me);
}
}
impl<T> Deref for SmallSliceBox<T> {
type Target = [T];
fn deref(&self) -> &Self::Target {
unsafe { core::slice::from_raw_parts(self.ptr.as_ptr(), self.size as usize) }
}
}
impl<T> DerefMut for SmallSliceBox<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { core::slice::from_raw_parts_mut(self.ptr.as_ptr(), self.size as usize) }
}
}
impl<T> core::borrow::Borrow<[T]> for SmallSliceBox<T> {
fn borrow(&self) -> &[T] {
&**self
}
}
impl<T> core::borrow::BorrowMut<[T]> for SmallSliceBox<T> {
fn borrow_mut(&mut self) -> &mut [T] {
&mut **self
}
}
impl<T> AsRef<[T]> for SmallSliceBox<T> {
fn as_ref(&self) -> &[T] {
&**self
}
}
impl<T> AsMut<[T]> for SmallSliceBox<T> {
fn as_mut(&mut self) -> &mut [T] {
&mut **self
}
}
impl<T> Unpin for SmallSliceBox<T> {}
impl<T> Clone for SmallSliceBox<T>
where
T: Clone,
{
fn clone(&self) -> Self {
let slice = self.deref();
unsafe { SmallSliceBox::new_unchecked(slice) }
}
}
impl<T: core::fmt::Debug> core::fmt::Debug for SmallSliceBox<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(&**self, f)
}
}
pub enum Thing {
LocalString { bytes: [u8; 14], len: u8 },
RemoteString { ptr: SmallSliceBox<u8> },
}