xref: /linux/rust/zerocopy/src/deprecated.rs (revision c37398010a05055e78cf0c75defb90df06c4e999)
1 // Copyright 2024 The Fuchsia Authors
2 //
3 // Licensed under the 2-Clause BSD License <LICENSE-BSD or
4 // https://opensource.org/license/bsd-2-clause>, Apache License, Version 2.0
5 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
6 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
7 // This file may not be copied, modified, or distributed except according to
8 // those terms.
9 
10 //! Deprecated items. These are kept separate so that they don't clutter up
11 //! other modules.
12 
13 use super::*;
14 
15 impl<B, T> Ref<B, T>
16 where
17     B: ByteSlice,
18     T: KnownLayout + Immutable + ?Sized,
19 {
20     #[deprecated(since = "0.8.0", note = "renamed to `Ref::from_bytes`")]
21     #[doc(hidden)]
22     #[must_use = "has no side effects"]
23     #[inline(always)]
24     pub fn new(bytes: B) -> Option<Ref<B, T>> {
25         Self::from_bytes(bytes).ok()
26     }
27 }
28 
29 impl<B, T> Ref<B, T>
30 where
31     B: SplitByteSlice,
32     T: KnownLayout + Immutable + ?Sized,
33 {
34     #[deprecated(since = "0.8.0", note = "renamed to `Ref::from_prefix`")]
35     #[doc(hidden)]
36     #[must_use = "has no side effects"]
37     #[inline(always)]
38     pub fn new_from_prefix(bytes: B) -> Option<(Ref<B, T>, B)> {
39         Self::from_prefix(bytes).ok()
40     }
41 }
42 
43 impl<B, T> Ref<B, T>
44 where
45     B: SplitByteSlice,
46     T: KnownLayout + Immutable + ?Sized,
47 {
48     #[deprecated(since = "0.8.0", note = "renamed to `Ref::from_suffix`")]
49     #[doc(hidden)]
50     #[must_use = "has no side effects"]
51     #[inline(always)]
52     pub fn new_from_suffix(bytes: B) -> Option<(B, Ref<B, T>)> {
53         Self::from_suffix(bytes).ok()
54     }
55 }
56 
57 impl<B, T> Ref<B, T>
58 where
59     B: ByteSlice,
60     T: Unaligned + KnownLayout + Immutable + ?Sized,
61 {
62     #[deprecated(
63         since = "0.8.0",
64         note = "use `Ref::from_bytes`; for `T: Unaligned`, the returned `CastError` implements `Into<SizeError>`"
65     )]
66     #[doc(hidden)]
67     #[must_use = "has no side effects"]
68     #[inline(always)]
69     pub fn new_unaligned(bytes: B) -> Option<Ref<B, T>> {
70         Self::from_bytes(bytes).ok()
71     }
72 }
73 
74 impl<B, T> Ref<B, T>
75 where
76     B: SplitByteSlice,
77     T: Unaligned + KnownLayout + Immutable + ?Sized,
78 {
79     #[deprecated(
80         since = "0.8.0",
81         note = "use `Ref::from_prefix`; for `T: Unaligned`, the returned `CastError` implements `Into<SizeError>`"
82     )]
83     #[doc(hidden)]
84     #[must_use = "has no side effects"]
85     #[inline(always)]
86     pub fn new_unaligned_from_prefix(bytes: B) -> Option<(Ref<B, T>, B)> {
87         Self::from_prefix(bytes).ok()
88     }
89 }
90 
91 impl<B, T> Ref<B, T>
92 where
93     B: SplitByteSlice,
94     T: Unaligned + KnownLayout + Immutable + ?Sized,
95 {
96     #[deprecated(
97         since = "0.8.0",
98         note = "use `Ref::from_suffix`; for `T: Unaligned`, the returned `CastError` implements `Into<SizeError>`"
99     )]
100     #[doc(hidden)]
101     #[must_use = "has no side effects"]
102     #[inline(always)]
103     pub fn new_unaligned_from_suffix(bytes: B) -> Option<(B, Ref<B, T>)> {
104         Self::from_suffix(bytes).ok()
105     }
106 }
107 
108 impl<B, T> Ref<B, [T]>
109 where
110     B: ByteSlice,
111     T: Immutable,
112 {
113     #[deprecated(since = "0.8.0", note = "`Ref::from_bytes` now supports slices")]
114     #[doc(hidden)]
115     #[inline(always)]
116     pub fn new_slice(bytes: B) -> Option<Ref<B, [T]>> {
117         Self::from_bytes(bytes).ok()
118     }
119 }
120 
121 impl<B, T> Ref<B, [T]>
122 where
123     B: ByteSlice,
124     T: Unaligned + Immutable,
125 {
126     #[deprecated(
127         since = "0.8.0",
128         note = "`Ref::from_bytes` now supports slices; for `T: Unaligned`, the returned `CastError` implements `Into<SizeError>`"
129     )]
130     #[doc(hidden)]
131     #[inline(always)]
132     pub fn new_slice_unaligned(bytes: B) -> Option<Ref<B, [T]>> {
133         Ref::from_bytes(bytes).ok()
134     }
135 }
136 
137 impl<'a, B, T> Ref<B, [T]>
138 where
139     B: 'a + IntoByteSlice<'a>,
140     T: FromBytes + Immutable,
141 {
142     #[deprecated(since = "0.8.0", note = "`Ref::into_ref` now supports slices")]
143     #[doc(hidden)]
144     #[inline(always)]
145     pub fn into_slice(self) -> &'a [T] {
146         Ref::into_ref(self)
147     }
148 }
149 
150 impl<'a, B, T> Ref<B, [T]>
151 where
152     B: 'a + IntoByteSliceMut<'a>,
153     T: FromBytes + IntoBytes + Immutable,
154 {
155     #[deprecated(since = "0.8.0", note = "`Ref::into_mut` now supports slices")]
156     #[doc(hidden)]
157     #[inline(always)]
158     pub fn into_mut_slice(self) -> &'a mut [T] {
159         Ref::into_mut(self)
160     }
161 }
162 
163 impl<B, T> Ref<B, [T]>
164 where
165     B: SplitByteSlice,
166     T: Immutable,
167 {
168     #[deprecated(since = "0.8.0", note = "replaced by `Ref::from_prefix_with_elems`")]
169     #[must_use = "has no side effects"]
170     #[doc(hidden)]
171     #[inline(always)]
172     pub fn new_slice_from_prefix(bytes: B, count: usize) -> Option<(Ref<B, [T]>, B)> {
173         Ref::from_prefix_with_elems(bytes, count).ok()
174     }
175 
176     #[deprecated(since = "0.8.0", note = "replaced by `Ref::from_suffix_with_elems`")]
177     #[must_use = "has no side effects"]
178     #[doc(hidden)]
179     #[inline(always)]
180     pub fn new_slice_from_suffix(bytes: B, count: usize) -> Option<(B, Ref<B, [T]>)> {
181         Ref::from_suffix_with_elems(bytes, count).ok()
182     }
183 }
184 
185 impl<B, T> Ref<B, [T]>
186 where
187     B: SplitByteSlice,
188     T: Unaligned + Immutable,
189 {
190     #[deprecated(
191         since = "0.8.0",
192         note = "use `Ref::from_prefix_with_elems`; for `T: Unaligned`, the returned `CastError` implements `Into<SizeError>`"
193     )]
194     #[doc(hidden)]
195     #[must_use = "has no side effects"]
196     #[inline(always)]
197     pub fn new_slice_unaligned_from_prefix(bytes: B, count: usize) -> Option<(Ref<B, [T]>, B)> {
198         Ref::from_prefix_with_elems(bytes, count).ok()
199     }
200 
201     #[deprecated(
202         since = "0.8.0",
203         note = "use `Ref::from_suffix_with_elems`; for `T: Unaligned`, the returned `CastError` implements `Into<SizeError>`"
204     )]
205     #[doc(hidden)]
206     #[must_use = "has no side effects"]
207     #[inline(always)]
208     pub fn new_slice_unaligned_from_suffix(bytes: B, count: usize) -> Option<(B, Ref<B, [T]>)> {
209         Ref::from_suffix_with_elems(bytes, count).ok()
210     }
211 }
212 
213 #[cfg(test)]
214 mod tests {
215     use super::*;
216 
217     #[test]
218     #[allow(deprecated)]
219     fn test_deprecated_ref_methods() {
220         let bytes = &[0u8; 1][..];
221         let bytes_slice = &[0u8; 4][..];
222 
223         let r: Option<Ref<&[u8], u8>> = Ref::new(bytes);
224         assert!(r.is_some());
225 
226         let r: Option<(Ref<&[u8], u8>, &[u8])> = Ref::new_from_prefix(bytes);
227         assert!(r.is_some());
228 
229         let r: Option<(&[u8], Ref<&[u8], u8>)> = Ref::new_from_suffix(bytes);
230         assert!(r.is_some());
231 
232         let r: Option<Ref<&[u8], u8>> = Ref::new_unaligned(bytes);
233         assert!(r.is_some());
234 
235         let r: Option<(Ref<&[u8], u8>, &[u8])> = Ref::new_unaligned_from_prefix(bytes);
236         assert!(r.is_some());
237 
238         let r: Option<(&[u8], Ref<&[u8], u8>)> = Ref::new_unaligned_from_suffix(bytes);
239         assert!(r.is_some());
240 
241         let r: Option<Ref<&[u8], [u8]>> = Ref::new_slice(bytes_slice);
242         assert!(r.is_some());
243 
244         let r: Option<Ref<&[u8], [u8]>> = Ref::new_slice_unaligned(bytes_slice);
245         assert!(r.is_some());
246 
247         let r: Option<(Ref<&[u8], [u8]>, &[u8])> = Ref::new_slice_from_prefix(bytes_slice, 1);
248         assert!(r.is_some());
249 
250         let r: Option<(&[u8], Ref<&[u8], [u8]>)> = Ref::new_slice_from_suffix(bytes_slice, 1);
251         assert!(r.is_some());
252 
253         let r: Option<(Ref<&[u8], [u8]>, &[u8])> =
254             Ref::new_slice_unaligned_from_prefix(bytes_slice, 1);
255         assert!(r.is_some());
256 
257         let r: Option<(&[u8], Ref<&[u8], [u8]>)> =
258             Ref::new_slice_unaligned_from_suffix(bytes_slice, 1);
259         assert!(r.is_some());
260     }
261 
262     #[test]
263     #[allow(deprecated)]
264     fn test_deprecated_into_slice() {
265         let bytes = &[0u8; 4][..];
266         let r: Ref<&[u8], [u8]> = Ref::from_bytes(bytes).unwrap();
267         let slice: &[u8] = r.into_slice();
268         assert_eq!(slice.len(), 4);
269     }
270 
271     #[test]
272     #[allow(deprecated)]
273     fn test_deprecated_into_mut_slice() {
274         let mut bytes = [0u8; 4];
275         let r: Ref<&mut [u8], [u8]> = Ref::from_bytes(&mut bytes[..]).unwrap();
276         let slice: &mut [u8] = r.into_mut_slice();
277         assert_eq!(slice.len(), 4);
278     }
279 }
280