Debian Codegraph @

src/methods.rs

raw symbols SCIP
1
2 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::{memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/, slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/};
3
4 use {Errorrust-analyzer cargo plain 0.2.3 error/Error#, Plainrust-analyzer cargo plain 0.2.3 plain/Plain#};
5
6 /// Check if a byte slice is aligned suitably for type T.
7 #[inline]
8 pub fn is_alignedrust-analyzer cargo plain 0.2.3 methods/is_aligned().<T>(bytes: &[u8]) -> bool {
9 ((bytes.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as usize) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::align_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/align_of().::<T>()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0
10 }
11
12 #[inline(always)]
13 fn check_alignmentrust-analyzer cargo plain 0.2.3 methods/check_alignment().<T>(bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo plain 0.2.3 error/Error#> {
14 if is_alignedrust-analyzer cargo plain 0.2.3 methods/is_aligned().::<T>(bytes) {
15 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
16 } else {
17 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo plain 0.2.3 error/Error#::BadAlignmentrust-analyzer cargo plain 0.2.3 error/Error#BadAlignment#)
18 }
19 }
20
21 #[inline(always)]
22 fn check_lengthrust-analyzer cargo plain 0.2.3 methods/check_length().<T>(bytes: &[u8], len: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo plain 0.2.3 error/Error#> {
23 if memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 && (bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>()) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().len {
24 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo plain 0.2.3 error/Error#::TooShortrust-analyzer cargo plain 0.2.3 error/Error#TooShort#)
25 } else {
26 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
27 }
28 }
29
30 /// Interpret data as bytes. Not safe for data with padding.
31 #[inline(always)]
32 pub unsafe fn as_bytesrust-analyzer cargo plain 0.2.3 methods/as_bytes().<S>(s: &S) -> &[u8]
33 where
34 S: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
35 {
36 let bptr = s as *const S as *const u8;
37 let bsize = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_of_valrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of_val().(s);
38 slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(bptr, bsize)
39 }
40
41 /// Interpret data as mutable bytes.
42 /// Reading is not safe for data with padding. Writing is ok.
43 #[inline(always)]
44 pub unsafe fn as_mut_bytesrust-analyzer cargo plain 0.2.3 methods/as_mut_bytes().<S>(s: &mut S) -> &mut [u8]
45 where
46 S: Plainrust-analyzer cargo plain 0.2.3 plain/Plain# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
47 {
48 let bptr = s as *mut S as *mut u8;
49 let bsize = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_of_valrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of_val().(s);
50 slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(bptr, bsize)
51 }
52
53 /// Safely converts a byte slice to a reference.
54 ///
55 /// However, if the byte slice is not long enough
56 /// to contain target type, or if it doesn't
57 /// satisfy the type's alignment requirements,
58 /// the function returns an error.
59 ///
60 /// The function will not fail when the
61 /// byte slice is longer than necessary, since it is
62 /// a common practice to interpret the beginning of
63 /// a slice as a fixed-size header.
64 ///
65 /// In many cases it is preferrable to allocate
66 /// a value/slice of the target type and use
67 /// [`copy_from_bytes()`](fn.copy_from_bytes.html) to copy
68 /// data instead. That way, any issues with alignment
69 /// are implicitly avoided.
70 ///
71 #[inline]
72 pub fn from_bytesrust-analyzer cargo plain 0.2.3 methods/from_bytes().<T>(bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&T, Errorrust-analyzer cargo plain 0.2.3 error/Error#>
73 where
74 T: Plainrust-analyzer cargo plain 0.2.3 plain/Plain#,
75 {
76 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_alignmentrust-analyzer cargo plain 0.2.3 methods/check_alignment().::<T>(bytes));
77 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_lengthrust-analyzer cargo plain 0.2.3 methods/check_length().::<T>(bytes, 1));
78 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { &*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().(bytes.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as *const T) })
79 }
80
81 /// Similar to [`from_bytes()`](fn.from_bytes.html),
82 /// except that the output is a slice of T, instead
83 /// of a reference to a single T. All concerns about
84 /// alignment also apply here, but size is handled
85 /// differently.
86 ///
87 /// The result slice's length is set to be
88 /// `bytes.len() / size_of::<T>()`, and there
89 /// are no requirements for input size. I.e.
90 /// the result may be empty slice, and the input
91 /// slice doesn't necessarily have to end on `T`'s
92 /// boundary. The latter has pragmatic reasons: If the
93 /// length of the array is not known in advance,
94 /// e.g. if it's terminated by a special element,
95 /// it's perfectly legal to turn the whole rest
96 /// of data into `&[T]` and set the proper length
97 /// after inspecting the array.
98 ///
99 /// In many cases it is preferrable to allocate
100 /// a value/slice of the target type and use
101 /// [`copy_from_bytes()`](fn.copy_from_bytes.html) to copy
102 /// data instead. That way, any issues with alignment
103 /// are implicitly avoided.
104 ///
105 #[inline]
106 pub fn slice_from_bytesrust-analyzer cargo plain 0.2.3 methods/slice_from_bytes().<T>(bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&[T], Errorrust-analyzer cargo plain 0.2.3 error/Error#>
107 where
108 T: Plainrust-analyzer cargo plain 0.2.3 plain/Plain#,
109 {
110 let len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>();
111 slice_from_bytes_lenrust-analyzer cargo plain 0.2.3 methods/slice_from_bytes_len().(bytes, len)
112 }
113
114
115 /// Same as [`slice_from_bytes()`](fn.slice_from_bytes.html),
116 /// except that it takes explicit length of the result slice.
117 ///
118 /// If the input slice cannot satisfy the length, returns error.
119 /// The input slice is allowed to be longer than necessary.
120 ///
121 #[inline]
122 pub fn slice_from_bytes_lenrust-analyzer cargo plain 0.2.3 methods/slice_from_bytes_len().<T>(bytes: &[u8], len: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&[T], Errorrust-analyzer cargo plain 0.2.3 error/Error#>
123 where
124 T: Plainrust-analyzer cargo plain 0.2.3 plain/Plain#,
125 {
126 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_alignmentrust-analyzer cargo plain 0.2.3 methods/check_alignment().::<T>(bytes));
127 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_lengthrust-analyzer cargo plain 0.2.3 methods/check_length().::<T>(bytes, len));
128 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe {
129 slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(bytes.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as *const T, len)
130 })
131 }
132
133 /// See [`from_bytes()`](fn.from_bytes.html).
134 ///
135 /// Does the same, except with mutable references.
136 ///
137 #[inline]
138 pub fn from_mut_bytesrust-analyzer cargo plain 0.2.3 methods/from_mut_bytes().<T>(bytes: &mut [u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut T, Errorrust-analyzer cargo plain 0.2.3 error/Error#>
139 where
140 T: Plainrust-analyzer cargo plain 0.2.3 plain/Plain#,
141 {
142 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_alignmentrust-analyzer cargo plain 0.2.3 methods/check_alignment().::<T>(bytes));
143 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_lengthrust-analyzer cargo plain 0.2.3 methods/check_length().::<T>(bytes, 1));
144 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe { &mut *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().(bytes.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().() as *mut T) })
145 }
146
147 /// See [`slice_from_bytes()`](fn.slice_from_bytes.html).
148 ///
149 /// Does the same, except with mutable references.
150 ///
151 #[inline]
152 pub fn slice_from_mut_bytesrust-analyzer cargo plain 0.2.3 methods/slice_from_mut_bytes().<T>(bytes: &mut [u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut [T], Errorrust-analyzer cargo plain 0.2.3 error/Error#>
153 where
154 T: Plainrust-analyzer cargo plain 0.2.3 plain/Plain#,
155 {
156 let len = bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>();
157 slice_from_mut_bytes_lenrust-analyzer cargo plain 0.2.3 methods/slice_from_mut_bytes_len().(bytes, len)
158 }
159
160 /// See [`slice_from_bytes_len()`](fn.slice_from_bytes_len.html).
161 ///
162 /// Does the same, except with mutable references.
163 ///
164 #[inline]
165 pub fn slice_from_mut_bytes_lenrust-analyzer cargo plain 0.2.3 methods/slice_from_mut_bytes_len().<T>(bytes: &mut [u8], len: usize) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<&mut [T], Errorrust-analyzer cargo plain 0.2.3 error/Error#>
166 where
167 T: Plainrust-analyzer cargo plain 0.2.3 plain/Plain#,
168 {
169 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_alignmentrust-analyzer cargo plain 0.2.3 methods/check_alignment().::<T>(bytes));
170 tryrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/`r#try`!!(check_lengthrust-analyzer cargo plain 0.2.3 methods/check_length().::<T>(bytes, len));
171 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(unsafe {
172 slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(bytes.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_ptr().() as *mut T, len)
173 })
174 }
175
176 /// Copies data from a byte slice into existing memory.
177 /// Suitable when [`from_bytes()`](fn.from_bytes.html) would normally
178 /// be used, but the data is not aligned properly in memory.
179 ///
180 /// For an example how to use it, see crate-level documentation.
181 ///
182 #[inline]
183 pub fn copy_from_bytesrust-analyzer cargo plain 0.2.3 methods/copy_from_bytes().<T>(into: &mut T, bytes: &[u8]) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo plain 0.2.3 error/Error#>
184 where
185 T: Plainrust-analyzer cargo plain 0.2.3 plain/Plain# + ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
186 {
187 let sz = memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_of_valrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of_val().(into);
188
189 if bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().sz {
190 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo plain 0.2.3 error/Error#::TooShortrust-analyzer cargo plain 0.2.3 error/Error#TooShort#);
191 }
192
193 unsafe {
194 as_mut_bytesrust-analyzer cargo plain 0.2.3 methods/as_mut_bytes().(into).copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#sz]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
195 }
196
197 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
198 }