Skip to main content

zerocopy/
deprecated.rs

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