non-zero-byte-slice-0.1.0/.cargo_vcs_info.json0000644000000001700000000000100146350ustar { "git": { "sha1": "f5bde096c26a4731f59296d7e0d1fd204e7cc43c" }, "path_in_vcs": "crates/non-zero-byte-slice" }non-zero-byte-slice-0.1.0/Cargo.toml0000644000000014030000000000100126330ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "non-zero-byte-slice" version = "0.1.0" authors = ["Jiahao XU "] description = "openssh mux client." license = "MIT" repository = "https://github.com/openssh-rust/openssh-mux-client" [dependencies.serde] version = "1.0.103" features = ["derive"] non-zero-byte-slice-0.1.0/Cargo.toml.orig000064400000000000000000000004711046102023000163200ustar 00000000000000[package] name = "non-zero-byte-slice" version = "0.1.0" edition = "2018" authors = ["Jiahao XU "] license = "MIT" description = "openssh mux client." repository = "https://github.com/openssh-rust/openssh-mux-client" [dependencies] serde = { version = "1.0.103", features = ["derive"] } non-zero-byte-slice-0.1.0/LICENSE000064400000000000000000000020521046102023000144330ustar 00000000000000MIT License Copyright (c) 2021 Jiahao XU 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. non-zero-byte-slice-0.1.0/src/lib.rs000064400000000000000000000122231046102023000153320ustar 00000000000000use std::{ borrow::{Borrow, Cow, ToOwned}, convert::TryFrom, error::Error, ffi::{CStr, CString}, fmt, num::NonZeroU8, ops::Deref, }; use serde::Serialize; #[derive(Debug, Eq, PartialEq, Hash, Serialize)] #[repr(transparent)] pub struct NonZeroByteSlice([u8]); impl NonZeroByteSlice { pub const fn new(bytes: &[u8]) -> Option<&Self> { let mut i = 0; while i < bytes.len() { if bytes[i] == 0 { return None; } i += 1; } // safety: bytes does not contain 0 Some(unsafe { Self::new_unchecked(bytes) }) } /// # Safety /// /// * `bytes` - Must not contain `0`. pub const unsafe fn new_unchecked(bytes: &[u8]) -> &Self { &*(bytes as *const [u8] as *const Self) } pub const fn into_inner(&self) -> &[u8] { &self.0 } } /// The string contains null byte. #[derive(Debug)] pub struct NullByteError; impl fmt::Display for NullByteError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "NullByteError") } } impl Error for NullByteError {} impl<'a> TryFrom<&'a str> for &'a NonZeroByteSlice { type Error = NullByteError; fn try_from(s: &'a str) -> Result { NonZeroByteSlice::new(s.as_bytes()).ok_or(NullByteError) } } impl<'a> From<&'a CStr> for &'a NonZeroByteSlice { fn from(s: &'a CStr) -> Self { // safety: CStr cannot contain 0 byte unsafe { NonZeroByteSlice::new_unchecked(s.to_bytes()) } } } impl ToOwned for NonZeroByteSlice { type Owned = NonZeroByteVec; fn to_owned(&self) -> Self::Owned { self.into() } } #[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize)] #[repr(transparent)] pub struct NonZeroByteVec(Vec); impl NonZeroByteVec { pub fn new(bytes: Vec) -> Option { for byte in bytes.iter() { if *byte == 0 { return None; } } Some(Self(bytes)) } pub fn from_bytes_remove_nul(mut bytes: Vec) -> Self { bytes.retain(|byte| *byte != b'\0'); Self(bytes) } /// # Safety /// /// * `bytes` - Must not contain `0`. pub const unsafe fn new_unchecked(bytes: Vec) -> Self { Self(bytes) } pub fn from_slice(slice: &NonZeroByteSlice) -> Self { Self(slice.into_inner().into()) } pub fn push(&mut self, byte: NonZeroU8) { self.0.push(byte.get()) } pub fn from_bytes_slice_lossy(slice: &[u8]) -> Cow<'_, NonZeroByteSlice> { NonZeroByteSlice::new(slice) .map(Cow::Borrowed) .unwrap_or_else(|| { let bytes: Vec = slice .iter() .copied() .filter(|byte| *byte != b'\0') .collect(); // Safety: all null bytes from slice is filtered out. Cow::Owned(unsafe { Self::new_unchecked(bytes) }) }) } } impl From<&NonZeroByteSlice> for NonZeroByteVec { fn from(slice: &NonZeroByteSlice) -> Self { Self::from_slice(slice) } } impl TryFrom for NonZeroByteVec { type Error = NullByteError; fn try_from(s: String) -> Result { Self::new(s.into_bytes()).ok_or(NullByteError) } } impl From for NonZeroByteVec { fn from(s: CString) -> Self { // safety: CString cannot contain 0 byte unsafe { Self::new_unchecked(s.into_bytes()) } } } impl Deref for NonZeroByteVec { type Target = NonZeroByteSlice; fn deref(&self) -> &Self::Target { // safety: self.0 does not contain 0 unsafe { NonZeroByteSlice::new_unchecked(&self.0) } } } impl Borrow for NonZeroByteVec { fn borrow(&self) -> &NonZeroByteSlice { self.deref() } } #[cfg(test)] mod tests { use super::*; #[test] fn test_byte_slice_with_zero() { let mut vec: Vec<_> = (0..9).collect(); vec.push(0); let option = NonZeroByteSlice::new(&vec); debug_assert!(option.is_none(), "{:#?}", option); } #[test] fn test_byte_slice_without_zero() { let vec: Vec<_> = (1..102).collect(); NonZeroByteSlice::new(&vec).unwrap(); } #[test] fn test_byte_vec_without_zero() { let vec: Vec<_> = (1..102).collect(); NonZeroByteVec::new(vec).unwrap(); } #[test] fn test_byte_vec_from_bytes_remove_nul_zero() { let mut vec: Vec<_> = (0..3).collect(); vec.push(0); assert_eq!(NonZeroByteVec::from_bytes_remove_nul(vec).0, vec![1, 2]); } #[test] fn test_from_bytes_slice_lossy() { let non_zero_bytes = b"1234x4r2ex"; assert_eq!( NonZeroByteVec::from_bytes_slice_lossy(non_zero_bytes), Cow::Borrowed(NonZeroByteSlice::new(non_zero_bytes).unwrap()), ); let bytes_with_zero = b"\x00123x'1\x0023x\0"; assert_eq!( NonZeroByteVec::from_bytes_slice_lossy(bytes_with_zero), Cow::Owned(NonZeroByteVec::from_bytes_remove_nul( bytes_with_zero.to_vec() )), ); } }