From fb77ad2f8c19f42dac327e7bbc0c072d71a54adc Mon Sep 17 00:00:00 2001 From: Chris Macklin Date: Wed, 6 Mar 2024 09:33:24 -0800 Subject: [PATCH] remove binary_vec_io and other dead custom ser/de code (#387) * Delete the binary_vec_io package. * Delete the packing module. --- Cargo.lock | 7 - Cargo.toml | 1 - binary_vec_io/Cargo.toml | 11 -- binary_vec_io/LICENSE | 21 --- binary_vec_io/README.md | 2 - binary_vec_io/src/lib.rs | 150 ---------------- enclone_core/src/lib.rs | 1 - enclone_core/src/packing.rs | 344 ------------------------------------ 8 files changed, 537 deletions(-) delete mode 100644 binary_vec_io/Cargo.toml delete mode 100644 binary_vec_io/LICENSE delete mode 100644 binary_vec_io/README.md delete mode 100644 binary_vec_io/src/lib.rs delete mode 100644 enclone_core/src/packing.rs diff --git a/Cargo.lock b/Cargo.lock index 20e23baa9..cdf177945 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,13 +130,6 @@ dependencies = [ "rustc-demangle", ] -[[package]] -name = "binary_vec_io" -version = "0.1.12" -dependencies = [ - "itertools", -] - [[package]] name = "bincode" version = "1.3.3" diff --git a/Cargo.toml b/Cargo.toml index 5207e6968..c918991eb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,6 @@ members = [ "align_tools", "amino", "ansi_escape", - "binary_vec_io", "build_enclone_proto", "dna", "enclone", diff --git a/binary_vec_io/Cargo.toml b/binary_vec_io/Cargo.toml deleted file mode 100644 index a91079c18..000000000 --- a/binary_vec_io/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "binary_vec_io" -version = "0.1.12" -authors = ["David Jaffe "] -license = "MIT" -description = "Some tools that are 'internal' for now because they are insufficiently refined and unstable, but which are used by other 'public' crates." -repository = "https://github.com/10XGenomics/enclone_ranger" -edition = "2018" - -[dependencies] -itertools = ">= 0.8, <= 0.11" diff --git a/binary_vec_io/LICENSE b/binary_vec_io/LICENSE deleted file mode 100644 index 2837d2442..000000000 --- a/binary_vec_io/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -The MIT License (MIT) - -Copyright (c) 2018-2019 10x Genomics, Inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. diff --git a/binary_vec_io/README.md b/binary_vec_io/README.md deleted file mode 100644 index 4cad8e1d5..000000000 --- a/binary_vec_io/README.md +++ /dev/null @@ -1,2 +0,0 @@ -This crate contains utilities that are used by the crate -pretty_trace. In time the contents of this crate may be documented, but for now, we recommend against directly using them, as they may be changed. diff --git a/binary_vec_io/src/lib.rs b/binary_vec_io/src/lib.rs deleted file mode 100644 index ac330607a..000000000 --- a/binary_vec_io/src/lib.rs +++ /dev/null @@ -1,150 +0,0 @@ -// Copyright (c) 2019 10X Genomics, Inc. All rights reserved. - -// Write and read functions to which one passes a File, a ref to a number type -// defining the start of a 'vector' of entries, and the number of entries. -// -// See also crate memmap. - -use itertools::Itertools; -use std::fmt::Write as _; -use std::io::Write; - -#[cfg(not(target_os = "windows"))] -use std::os::unix::fs::MetadataExt; - -pub trait BinaryInputOutputSafe {} -impl BinaryInputOutputSafe for i8 {} -impl BinaryInputOutputSafe for i16 {} -impl BinaryInputOutputSafe for i32 {} -impl BinaryInputOutputSafe for i64 {} -impl BinaryInputOutputSafe for u8 {} -impl BinaryInputOutputSafe for u16 {} -impl BinaryInputOutputSafe for u32 {} -impl BinaryInputOutputSafe for u64 {} -impl BinaryInputOutputSafe for f32 {} -impl BinaryInputOutputSafe for f64 {} -// i128, u128? - -use std::io::Error; - -pub fn binary_write_from_ref(f: &mut std::fs::File, p: &T, n: usize) -> Result<(), Error> { - let raw = p as *const T as *const u8; - unsafe { - let sli: &[u8] = std::slice::from_raw_parts(raw, n * (std::mem::size_of::())); - f.write_all(sli)?; - Ok(()) - } -} - -pub fn binary_read_to_ref(f: &mut std::fs::File, p: &mut T, n: usize) -> Result<(), Error> { - let mut raw = p as *mut T as *mut u8; - unsafe { - use std::io::Read; - let bytes_to_read = n * std::mem::size_of::(); - let mut bytes_read = 0; - // Rarely, one must read twice (maybe, not necessarily proven). Conceivably one needs - // to read more than twice on occasion. - const MAX_TRIES: usize = 10; - let mut reads = Vec::::new(); - for _ in 0..MAX_TRIES { - if bytes_read == bytes_to_read { - break; - } - raw = raw.add(bytes_read); - let sli: &mut [u8] = std::slice::from_raw_parts_mut(raw, bytes_to_read - bytes_read); - let n = f.read(sli).unwrap(); - reads.push(n); - bytes_read += n; - } - if bytes_read != bytes_to_read { - let mut msg = format!( - "Failure in binary_read_to_ref, bytes_read = {}, but \ - bytes_to_read = {}. Bytes read on successive\nattempts = {}.\n", - bytes_read, - bytes_to_read, - reads.iter().format(","), - ); - #[cfg(not(target_os = "windows"))] - { - let metadata = f.metadata()?; - writeln!( - msg, - "File has length {} and inode {}.", - metadata.len(), - metadata.ino(), - ) - .unwrap(); - } - panic!("{}", msg); - } - } - Ok(()) -} - -// The functions binary_write_vec and binary_read_vec append, either to a file, -// in the first case, or to a vector, in the second case. - -pub fn binary_write_vec(f: &mut std::fs::File, x: &[T]) -> Result<(), Error> -where - T: BinaryInputOutputSafe, -{ - let n = x.len(); - binary_write_from_ref::(f, &n, 1)?; - binary_write_from_ref::(f, &x[0], x.len()) -} - -pub fn binary_read_vec(f: &mut std::fs::File, x: &mut Vec) -> Result<(), Error> -where - T: BinaryInputOutputSafe, -{ - // Read the vector size. - - let mut n: usize = 0; - binary_read_to_ref::(f, &mut n, 1)?; - - // Resize the vector without setting any of its entries. - // (could use resize_without_setting) - - let len = x.len(); - if len + n > x.capacity() { - let extra: usize = len + n - x.capacity(); - x.reserve(extra); - } - unsafe { - x.set_len(len + n); - } - - // Read the vector entries. - - binary_read_to_ref::(f, &mut x[len], n) -} - -pub fn binary_write_vec_vec(f: &mut std::fs::File, x: &[impl AsRef<[T]>]) -> Result<(), Error> -where - T: BinaryInputOutputSafe, -{ - let n = x.len(); - binary_write_from_ref::(f, &n, 1)?; - for xi in x { - binary_write_vec::(f, xi.as_ref())?; - } - Ok(()) -} - -pub fn binary_read_vec_vec(f: &mut std::fs::File, x: &mut Vec>) -> Result<(), Error> -where - T: BinaryInputOutputSafe + Clone, -{ - let mut n: usize = 0; - binary_read_to_ref::(f, &mut n, 1)?; - let len = x.len(); - if len + n > x.capacity() { - let extra: usize = len + n - x.capacity(); - x.reserve(extra); - } - x.resize(len + n, Vec::::new()); - for xi in x.iter_mut().take(n) { - binary_read_vec::(f, xi)?; - } - Ok(()) -} diff --git a/enclone_core/src/lib.rs b/enclone_core/src/lib.rs index eae185b2c..6a8485250 100644 --- a/enclone_core/src/lib.rs +++ b/enclone_core/src/lib.rs @@ -14,7 +14,6 @@ pub mod logging; pub mod mammalian_fixed_len; pub mod median; pub mod opt_d; -pub mod packing; pub mod print_tools; pub mod set_speakers; pub mod slurp; diff --git a/enclone_core/src/packing.rs b/enclone_core/src/packing.rs deleted file mode 100644 index 44310472d..000000000 --- a/enclone_core/src/packing.rs +++ /dev/null @@ -1,344 +0,0 @@ -// Copyright (c) 2021 10X Genomics, Inc. All rights reserved. - -// Unoptimized functions for packing and unpacking some data structures. - -use zstd::bulk::{compress, decompress}; - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -// Compression and decompression. We use zstd rather than gzip because when tested it yielded -// slightly smaller compression size and much lower compression time. Note that using zstd -// appears to add about 4 MB to the executable size. If this is really true, it's not obvious -// that it's a good tradeoff. - -pub fn compress_bytes(x: &[u8]) -> Vec { - compress(x, 0).unwrap() -} - -pub fn uncompress_bytes(x: &[u8], uncompressed_size: usize) -> Vec { - decompress(x, uncompressed_size).unwrap() -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn u32_bytes(x: usize) -> [u8; 4] { - (x as u32).to_le_bytes() -} - -pub fn u32_from_bytes(x: &[u8]) -> u32 { - u32::from_le_bytes([x[0], x[1], x[2], x[3]]) -} - -pub fn f32_bytes(x: usize) -> [u8; 4] { - (x as f32).to_le_bytes() -} - -pub fn f32_from_bytes(x: &[u8]) -> f32 { - f32::from_le_bytes([x[0], x[1], x[2], x[3]]) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_string(x: &String) -> Vec { - let mut bytes = Vec::::new(); - bytes.extend(u32_bytes(x.len())); - bytes.extend(x.as_bytes()); - bytes -} - -pub fn restore_string(x: &[u8], pos: &mut usize) -> Result { - if *pos + 4 > x.len() { - return Err(()); - } - let k = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + k > x.len() { - return Err(()); - } - let s = String::from_utf8(x[*pos..*pos + k].to_vec()); - if s.is_err() { - return Err(()); - } - *pos += k; - Ok(s.unwrap()) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_string(x: &[String]) -> Vec { - let mut bytes = Vec::::with_capacity(4 + 5 * x.len()); - bytes.extend(u32_bytes(x.len())); - for xi in x { - bytes.extend(u32_bytes(xi.len())); - bytes.extend(xi.as_bytes()); - } - bytes -} - -pub fn restore_vec_string(x: &[u8], pos: &mut usize) -> Result, ()> { - if *pos + 4 > x.len() { - return Err(()); - } - let n = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - let mut y = vec![String::new(); n]; - for yj in &mut y { - if *pos + 4 > x.len() { - return Err(()); - } - let k = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + k > x.len() { - return Err(()); - } - let s = String::from_utf8(x[*pos..*pos + k].to_vec()); - match s { - Err(_) => return Err(()), - Ok(s) => { - *pos += k; - *yj = s; - } - } - } - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_string_comp(x: &[String]) -> Vec { - let z = save_vec_string(x); - let mut y = compress_bytes(&z); - let mut bytes = Vec::::with_capacity(8 + y.len()); - bytes.extend(u32_bytes(y.len())); - bytes.extend(u32_bytes(z.len())); - bytes.append(&mut y); - bytes -} - -pub fn restore_vec_string_comp(x: &[u8], pos: &mut usize) -> Result, ()> { - if *pos + 8 > x.len() { - return Err(()); - } - let n = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - let uncompressed_size = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + n > x.len() { - return Err(()); - } - let uncomp = uncompress_bytes(&x[*pos..*pos + n], uncompressed_size); - *pos += n; - let mut posx = 0; - restore_vec_string(&uncomp, &mut posx) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_vec_string(x: &[Vec]) -> Vec { - let mut bytes = Vec::::with_capacity(4 + 5 * x.len()); - bytes.extend(u32_bytes(x.len())); - for xi in x { - bytes.append(&mut save_vec_string(xi)); - } - bytes -} - -pub fn restore_vec_vec_string(x: &[u8], pos: &mut usize) -> Result>, ()> { - if *pos + 4 > x.len() { - return Err(()); - } - let n = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - let mut y = vec![Vec::::new(); n]; - for yj in &mut y { - *yj = restore_vec_string(x, pos)?; - } - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_vec_u8(x: &Vec>) -> Vec { - let mut bytes = Vec::::with_capacity(4 + 5 * x.len()); - bytes.extend(u32_bytes(x.len())); - for vi in x { - bytes.extend(u32_bytes(vi.len())); - bytes.extend(vi); - } - bytes -} - -pub fn restore_vec_vec_u8(x: &[u8], pos: &mut usize) -> Result>, ()> { - if *pos + 4 > x.len() { - return Err(()); - } - let n = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - let mut y = vec![Vec::::new(); n]; - for yj in &mut y { - if *pos + 4 > x.len() { - return Err(()); - } - let k = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + k > x.len() { - return Err(()); - } - *yj = x[*pos..*pos + k].to_vec(); - *pos += k; - } - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_vec_u32(x: &[Vec]) -> Vec { - let mut bytes = Vec::::with_capacity(4 + 8 * x.len()); - bytes.extend(u32_bytes(x.len())); - for vi in x { - bytes.extend(u32_bytes(vi.len())); - for xj in vi { - bytes.extend(xj.to_le_bytes()); - } - } - bytes -} - -pub fn restore_vec_vec_u32(x: &[u8], pos: &mut usize) -> Result>, ()> { - if *pos + 4 > x.len() { - return Err(()); - } - let n = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - let mut y = vec![Vec::::new(); n]; - for yj in &mut y { - if *pos + 4 > x.len() { - return Err(()); - } - let k = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + 4 * k > x.len() { - return Err(()); - } - yj.reserve(4 * k); - for _ in 0..k { - yj.push(u32_from_bytes(&x[*pos..*pos + 4])); - *pos += 4; - } - } - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_bool(x: &Vec) -> Vec { - let mut bytes = Vec::::with_capacity(4 + x.len()); - bytes.extend(u32_bytes(x.len())); - for &xi in x { - bytes.push(u8::from(xi)); - } - bytes -} - -pub fn restore_vec_bool(x: &[u8], pos: &mut usize) -> Result, ()> { - if *pos + 4 > x.len() { - return Err(()); - } - let n = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + n > x.len() { - return Err(()); - } - let mut y = vec![false; n]; - for (yj, &xj) in y[..n].iter_mut().zip(x[*pos..].iter()) { - *yj = xj == 1; - *pos += 1; - } - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_u32(x: &[u32]) -> Vec { - let mut bytes = Vec::::with_capacity(4 * (x.len() + 1)); - bytes.extend(u32_bytes(x.len())); - for &xi in x { - bytes.extend(xi.to_le_bytes()); - } - bytes -} - -pub fn restore_vec_u32(x: &[u8], pos: &mut usize) -> Result, ()> { - if *pos + 4 > x.len() { - return Err(()); - } - let n = u32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + 4 * n > x.len() { - return Err(()); - } - let mut y = vec![0; n]; - for yj in &mut y { - *yj = u32_from_bytes(&x[*pos..*pos + 4]); - *pos += 4; - } - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_u32(x: u32) -> [u8; 4] { - x.to_le_bytes() -} - -pub fn restore_u32(x: &[u8], pos: &mut usize) -> Result { - if *pos + 4 > x.len() { - return Err(()); - } - let y = u32_from_bytes(&x[*pos..*pos + 4]); - *pos += 4; - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_bool(x: bool) -> [u8; 1] { - [x as u8] -} - -pub fn restore_bool(x: &[u8], pos: &mut usize) -> Result { - if *pos + 1 > x.len() { - return Err(()); - } - let y = x[*pos] != 0; - *pos += 1; - Ok(y) -} - -// ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓ - -pub fn save_vec_f32(x: &[f32]) -> Vec { - let mut bytes = Vec::::with_capacity(4 * (x.len() + 1)); - bytes.extend(f32_bytes(x.len())); - for &xi in x { - bytes.extend(xi.to_le_bytes()); - } - bytes -} - -pub fn restore_vec_f32(x: &[u8], pos: &mut usize) -> Result, ()> { - if *pos + 4 > x.len() { - return Err(()); - } - let n = f32_from_bytes(&x[*pos..*pos + 4]) as usize; - *pos += 4; - if *pos + 4 * n > x.len() { - return Err(()); - } - let mut y = vec![0.0; n]; - for yj in &mut y[..n] { - *yj = f32_from_bytes(&x[*pos..*pos + 4]); - *pos += 4; - } - Ok(y) -}