deno.land / x / deno@v1.28.2 / serde_v8 / magic / rawbytes.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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.pub(crate) type AtomicPtr<T> = *mut T;#[allow(unused)]pub(crate) struct RawBytes { ptr: *const u8, len: usize, // inlined "trait object" data: AtomicPtr<()>, vtable: &'static Vtable,}
impl RawBytes { pub fn new_raw( ptr: *const u8, len: usize, data: AtomicPtr<()>, vtable: &'static Vtable, ) -> bytes::Bytes { RawBytes { ptr, len, data, vtable, } .into() }}
// Validate some bytes::Bytes layout assumptions at compile time.const _: () = { assert!( core::mem::size_of::<RawBytes>() == core::mem::size_of::<bytes::Bytes>(), ); assert!( core::mem::align_of::<RawBytes>() == core::mem::align_of::<bytes::Bytes>(), );};
#[allow(unused)]pub(crate) struct Vtable { /// fn(data, ptr, len) pub clone: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> bytes::Bytes, /// fn(data, ptr, len) /// /// takes `Bytes` to value pub to_vec: unsafe fn(&AtomicPtr<()>, *const u8, usize) -> Vec<u8>, /// fn(data, ptr, len) pub drop: unsafe fn(&mut AtomicPtr<()>, *const u8, usize),}
impl From<RawBytes> for bytes::Bytes { fn from(b: RawBytes) -> Self { // SAFETY: RawBytes has the same layout as bytes::Bytes // this is tested below, both are composed of usize-d ptrs/values // thus aren't currently subject to rust's field re-ordering to minimize padding unsafe { std::mem::transmute(b) } }}
#[cfg(test)]mod tests { use super::*; use std::mem;
const HELLO: &str = "hello";
// ===== impl StaticVtable =====
const STATIC_VTABLE: Vtable = Vtable { clone: static_clone, drop: static_drop, to_vec: static_to_vec, };
unsafe fn static_clone( _: &AtomicPtr<()>, ptr: *const u8, len: usize, ) -> bytes::Bytes { from_static(std::slice::from_raw_parts(ptr, len)).into() }
unsafe fn static_to_vec( _: &AtomicPtr<()>, ptr: *const u8, len: usize, ) -> Vec<u8> { let slice = std::slice::from_raw_parts(ptr, len); slice.to_vec() }
unsafe fn static_drop(_: &mut AtomicPtr<()>, _: *const u8, _: usize) { // nothing to drop for &'static [u8] }
fn from_static(bytes: &'static [u8]) -> RawBytes { RawBytes { ptr: bytes.as_ptr(), len: bytes.len(), data: std::ptr::null_mut(), vtable: &STATIC_VTABLE, } }
#[test] fn bytes_identity() { let b1: bytes::Bytes = from_static(HELLO.as_bytes()).into(); let b2 = bytes::Bytes::from_static(HELLO.as_bytes()); assert_eq!(b1, b2); // Values are equal }
#[test] fn bytes_layout() { let u1: [usize; 4] = // SAFETY: ensuring layout is the same unsafe { mem::transmute(from_static(HELLO.as_bytes())) }; let u2: [usize; 4] = // SAFETY: ensuring layout is the same unsafe { mem::transmute(bytes::Bytes::from_static(HELLO.as_bytes())) }; assert_eq!(u1[..3], u2[..3]); // Struct bytes are equal besides Vtables }}
deno

Version Info

Tagged at
a year ago