diff --git a/src/util/slice.rs b/src/util/slice.rs index 573d4e2e54e8ba70290f244bef4e477d58cf405d..6f07e61469ccf2666bd2c2f1d3c94e55aca0c29b 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -1,10 +1,7 @@ -use std::{ptr, slice}; -use std::alloc::{alloc, Layout}; +use std::mem; use std::borrow::Cow; use std::cmp::Ordering; -use std::mem::ManuallyDrop; use std::ops::Deref; -use std::ptr::{copy, NonNull}; pub struct Slice { data: Vec, @@ -18,10 +15,8 @@ extern { impl Default for Slice { /// 构造一个空的 Slice fn default() -> Self { - unsafe { - Self { - data: Vec::new() - } + Self { + data: Vec::new() } } } @@ -45,11 +40,9 @@ impl Slice { if self.size() == 0 { return Slice::default(); } - unsafe { - let sub_data = &(*self.data)[n..self.size()]; - Self { - data: Vec::from(sub_data) - } + let sub_data = &(*self.data)[n..self.size()]; + Self { + data: Vec::from(sub_data) } } @@ -71,10 +64,11 @@ impl Slice { impl<'a> Slice { /// 借取 Slice 中的数据, 调用方只拥有读权限 pub fn borrow_data(&mut self) -> Cow<'a, String> { - let str = unsafe { - String::from_raw_parts(self.data.as_mut_ptr(), self.size(), self.size()) - }; - Cow::Owned(str) + unsafe { + // String & Vec has the same layout + let s: &String = mem::transmute(&self.data); + Cow::Borrowed(s) + } } } @@ -148,9 +142,7 @@ impl Deref for Slice { /// Slice 解引用到 &[u8] fn deref(&self) -> &Self::Target { - unsafe { - self.data.deref() - } + &*self.data } } diff --git a/src/util/slice_test.rs b/src/util/slice_test.rs index 9bb11d81d5bfc935f81542339c5684fe560ae067..9eabf5957b93857008ac218e34c8ed4b7d0e3637 100644 --- a/src/util/slice_test.rs +++ b/src/util/slice_test.rs @@ -1,78 +1,90 @@ - mod test { + use std::cmp::Ordering; use crate::util::slice::Slice; #[test] fn test_from() { - let mut a = Slice::from("123"); - assert_eq!(String::from("123"), String::from(a)) - + // from &str + let a0 = Slice::from("123"); + assert_eq!(String::from("123"), String::from(a0)); + // from String + let a1 = Slice::from(String::from("123")); + assert_eq!(String::from("123"), String::from(a1)); } #[test] - fn test_addr() { - let a = 123_u32; - let slice = a.to_le_bytes(); - unsafe { - let a_addr = &a as *const u32 as usize; - let slice_addr = slice.as_ptr() as usize; - println!("a_addr: {}, slice_addr: {}", a_addr, slice_addr); - } + fn test_empty() { + let a0 = Slice::default(); + assert_eq!(true, a0.empty()); + + let a1 = Slice::from("123"); + assert_eq!(false, a1.empty()); } #[test] - fn test_size() { - let mut a: Vec = Vec::with_capacity(4); - // 把 u32 编码到4个byte中, 在内存中结构为 [a0, a1, a2, a3, ....] - // 方式1, 用位移来编码 - let len = 12_u32; // 长度 - a.push(len as u8); - a.push((len >> 8) as u8); - a.push((len >> 16) as u8); - a.push((len >> 24) as u8); + fn test_remove_prefix() { + let a0 = Slice::from("123"); + let a1 = a0.remove_prefix(1); + assert_eq!(2, a1.len()); + } - // 方式2, 获取到内存地址直接写 - // unsafe { - // a.as_mut_ptr().cast::().write(12); - // 指针待偏移量可以这样写 - // a.as_mut_ptr().cast::().offset(0).write(12); - // a.set_len(4); - // } + #[test] + fn test_starts_with() { + let a0 = Slice::from("12345"); + let a1 = a0.remove_prefix(2); + assert_eq!(String::from("345"), String::from(a1)); + } + #[test] + fn test_borrow_data() { + let mut a0 = Slice::from("123"); + let borrowed = a0.borrow_data(); + assert_eq!(3, borrowed.len()); + let owned = borrowed.to_owned(); + assert_eq!(3, owned.len()); + } - // 取出数据校验, a 这个 vec 指针指向的数据为 [a0, a1, a2, a3, ....], - // 读取地址前4个byte到u32中, 比较原始值 - let value = unsafe { - a.as_ptr().cast::().read() - }; - assert_eq!(12, value); + #[test] + fn test_partial_eq() { + let a0 = Slice::from("123"); + let a1 = Slice::from("123"); + let a2 = Slice::from("234"); + let a3 = Slice::from("012"); + assert_eq!(true, a0 == a1); + assert_eq!(true, a0 < a2); + assert_eq!(true, a0 > a3); + } - // let slice: Slice = String::from("123").into(); - // assert_eq!(3, slice.size()); + #[test] + fn test_partial_ord() { + let a0 = Slice::from("123"); + let a1 = Slice::from("123"); + let a2 = Slice::from("234"); + let a3 = Slice::from("012"); + assert_eq!(Ordering::Equal, a0.partial_cmp(&a1).unwrap()); + assert_eq!(Ordering::Less, a0.partial_cmp(&a2).unwrap()); + assert_eq!(Ordering::Greater, a0.partial_cmp(&a3).unwrap()); } #[test] fn test_memory_leak() { // 申请 100G 内存, 查看是否内存泄漏。如果内存泄漏,程序会OOM (0..100_000_000).for_each(|_| { - unsafe { - // 1k - let str = "0123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ - 301230123012301230123012301230123"; - let _: Slice = str.into(); - } + // 1k + let str = "0123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123012301230123012301230123012301230123012301230123012\ + 301230123012301230123012301230123"; + let _: Slice = str.into(); }) } - } \ No newline at end of file