1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// Copyright 2023 The rust-ggstd authors.
// SPDX-License-Identifier: BSD-3-Clause
//! compat module contains functions that ease porting of Go code to Rust.
//! These functions are intended for internal usage only.
/// Copy data from one buffer to another similar to what Go copy function does.
pub fn copy(dest: &mut [u8], src: &[u8]) -> usize {
let dest_size = dest.len();
let src_size = src.len();
if dest_size == src_size {
dest.copy_from_slice(src);
src_size
} else {
let size = src_size.min(dest_size);
dest[..size].copy_from_slice(&src[..size]);
size
}
}
/// Copy data from one place of the buffer to another similar to what Go copy function can do.
pub fn copy_within(buf: &mut [u8], src: std::ops::Range<usize>, dest: usize) -> usize {
let buf_len = buf.len();
let dest_size = buf_len - dest;
let src_size = src.end - src.start;
if src_size > dest_size {
buf.copy_within(src.start..src.start + dest_size, dest);
dest_size
} else {
buf.copy_within(src, dest);
src_size
}
}
/// It is not possible to copy std::io::Error directly because it doesn't implement
/// Copy trait. This function creates a new std::io::Error with the same
/// error kind and a message copied from the original error.
pub fn copy_stdio_error(e: &std::io::Error) -> std::io::Error {
std::io::Error::new(e.kind(), e.to_string())
}
/// string converts a byte slice into a string.
/// This is somewhat similar to Go `string(b)`, but not exactly.
/// In Go any sequence of bytes can be converted into a string, but in Rust
/// a string can contain only valid utf-8.
/// This function panics if the byte slice cannot be converted into a valid Rust string.
pub fn string(b: &[u8]) -> String {
String::from_utf8(b.to_vec()).unwrap()
}
pub mod chars;
mod defer;
pub mod readers;
pub use defer::{Defer, DeferDirRemoval};
#[cfg(test)]
mod readers_test;