multiversx_sc_codec/
transmute.rs1use alloc::{boxed::Box, vec::Vec};
2
3#[inline(never)]
4pub fn boxed_slice_into_vec<T>(mut bs: Box<[T]>) -> Vec<T> {
5 let l = bs.len();
6 if l == 0 {
7 return Vec::new();
8 }
9 let ptr = &mut bs[0] as *mut T;
10 core::mem::forget(bs);
11 unsafe { Vec::from_raw_parts(ptr, l, l) }
12}
13
14#[inline(never)]
15pub fn vec_into_boxed_slice<T>(v: Vec<T>) -> Box<[T]> {
16 v.into_boxed_slice()
17}
18
19#[cfg(test)]
20pub mod tests {
21 use super::*;
22
23 #[test]
24 fn test_boxed_slice_into_vec_0() {
25 let bs = Box::<[u8]>::from([]);
26 let v = boxed_slice_into_vec(bs);
27 assert_eq!(v, Vec::<u8>::new());
28 }
29
30 #[test]
31 fn test_boxed_slice_into_vec_1() {
32 let bs = Box::<[u8]>::from([1, 2, 3]);
33 let v = boxed_slice_into_vec(bs);
34 assert_eq!(v, [1u8, 2, 3].to_vec());
35 }
36
37 #[test]
38 fn test_vec_into_boxed_slice_0() {
39 let v = Vec::<u8>::new();
40 let bs = vec_into_boxed_slice(v);
41 assert_eq!(bs, Box::<[u8]>::from([]));
42 }
43
44 #[test]
45 fn test_vec_into_boxed_slice_1() {
46 let v = [1u8, 2, 3].to_vec();
47 let bs = vec_into_boxed_slice(v);
48 assert_eq!(bs, Box::<[u8]>::from([1, 2, 3]));
49 }
50}