From cc7063313f4f7a942eb713edc1bb62dff0e70f57 Mon Sep 17 00:00:00 2001 From: Jessi Date: Wed, 10 Jul 2024 09:56:15 -0400 Subject: [PATCH] minor refactoring --- src/data.rs | 45 ------------------------------ src/iter_tools/take_until.rs | 3 -- src/lib.rs | 5 ++-- src/pointers.rs | 53 ++++++++++++++++++++++++++++++++++++ src/strings.rs | 4 +-- tests/test_data.rs | 13 +++++++-- 6 files changed, 68 insertions(+), 55 deletions(-) create mode 100644 src/pointers.rs diff --git a/src/data.rs b/src/data.rs index 79b4428..069de2e 100644 --- a/src/data.rs +++ b/src/data.rs @@ -41,51 +41,6 @@ pub fn distance(p1: impl IntoUsize, p2: impl IntoUsize) -> usize { } } -mod pointer_iterator { - - pub trait Pointer { - type IterType; - fn into_iter(self) -> Self::IterType; - } - - pub struct PIter(*const T); - pub struct PIterMut(*mut T); - - impl Iterator for PIter { - type Item = &'static T; - fn next(&mut self) -> Option { - unsafe { - let r = Some(&*self.0); - self.0 = self.0.offset(1isize); - r - } - } - } - - impl Iterator for PIterMut { - type Item = &'static mut T; - fn next(&mut self) -> Option { - unsafe { - let r = Some(&mut *self.0); - self.0 = self.0.offset(1isize); - r - } - } - } - impl Pointer for *const T { - type IterType = PIter; - fn into_iter(self) -> Self::IterType { PIter(self) } - } - impl Pointer for *mut T { - type IterType = PIterMut; - fn into_iter(self) -> Self::IterType { PIterMut(self) } - } -} - -pub fn iterate(pointer: T) -> T::IterType { - pointer.into_iter() -} - pub fn fill_with T>(slice: &mut [T], mut func: F) { slice.iter_mut().enumerate().for_each(|(i,v)|*v = func(i)) } diff --git a/src/iter_tools/take_until.rs b/src/iter_tools/take_until.rs index b40b190..804324b 100644 --- a/src/iter_tools/take_until.rs +++ b/src/iter_tools/take_until.rs @@ -1,7 +1,4 @@ -use core::fmt; - - #[derive(Clone)] pub struct TakeUntil { iter: I, diff --git a/src/lib.rs b/src/lib.rs index 390f8d9..c4073f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,4 @@ -#![feature(decl_macro)] -#![no_std] +#![no_std] #![feature(decl_macro)] /// Virtual Struct Offset mod vso; @@ -15,6 +14,8 @@ pub use upcast::IntoUsize; mod data; pub use data::*; +mod pointers; +pub use pointers::*; /// utility macros for branching /// invoke_once, etc diff --git a/src/pointers.rs b/src/pointers.rs new file mode 100644 index 0000000..d7e5289 --- /dev/null +++ b/src/pointers.rs @@ -0,0 +1,53 @@ + +pub fn null() -> T { T::new_null() } +pub fn iterate(pointer: T) -> T::IterType { + pointer.into_iter() +} + + +mod internal { + + pub trait Pointer { + type IterType; + fn into_iter(self) -> Self::IterType; + fn new_null() -> Self; + } + + + pub struct PIter(*const T); + pub struct PIterMut(*mut T); + + impl Iterator for PIter { + type Item = &'static T; + fn next(&mut self) -> Option { + unsafe { + let r = Some(&*self.0); + self.0 = self.0.offset(1isize); + r + } + } + } + + impl Iterator for PIterMut { + type Item = &'static mut T; + fn next(&mut self) -> Option { + unsafe { + let r = Some(&mut *self.0); + self.0 = self.0.offset(1isize); + r + } + } + } + impl Pointer for *const T { + type IterType = PIter; + fn into_iter(self) -> Self::IterType { PIter(self) } + fn new_null() -> Self { core::ptr::null() } + } + + impl Pointer for *mut T { + type IterType = PIterMut; + fn into_iter(self) -> Self::IterType { PIterMut(self) } + fn new_null() -> Self { core::ptr::null_mut() } + } + +} \ No newline at end of file diff --git a/src/strings.rs b/src/strings.rs index 0b45225..8c34dd6 100644 --- a/src/strings.rs +++ b/src/strings.rs @@ -1,5 +1,5 @@ -use core::fmt::{Debug, Display, Error, Formatter, Write}; -use core::ops::{ControlFlow, Deref}; +use core::fmt::{Display, Formatter, Write}; +use core::ops::{ControlFlow}; #[repr(transparent)] pub struct Wide<'a>(pub &'a [u16]); diff --git a/tests/test_data.rs b/tests/test_data.rs index 5293fec..d040a4d 100644 --- a/tests/test_data.rs +++ b/tests/test_data.rs @@ -1,8 +1,7 @@ -use std::mem::offset_of; -use x::win32::ImageOptionalHeader64; + #[test] -pub fn test_distance() { +pub fn test_data() { let _ = x::dur![ 5 days 4 hours 7 minutes 2 seconds 2 minutes ]; let a = [0u8, 2, 3]; @@ -27,4 +26,12 @@ pub fn test_distance() { x::iterate(b).cloned().take_while(|&c| c != 0)) .filter_map(|_r| _r.ok()).collect(); assert_eq!("Hello World", hello_world); + + test1(x::null()); + test2(x::null()); + } + + +fn test1(_a: *const u8) {} +fn test2(_a: *mut u8) {} \ No newline at end of file