iterate
This commit is contained in:
45
src/data.rs
45
src/data.rs
@@ -39,3 +39,48 @@ pub fn distance(p1: impl IntoUsize, p2: impl IntoUsize) -> usize {
|
||||
Ordering::Equal => 0,
|
||||
}
|
||||
}
|
||||
|
||||
mod pointer_iterator {
|
||||
|
||||
pub trait Pointer {
|
||||
type IterType;
|
||||
fn into_iter(self) -> Self::IterType;
|
||||
}
|
||||
|
||||
pub struct PIter<T>(*const T);
|
||||
pub struct PIterMut<T>(*mut T);
|
||||
|
||||
impl<T: 'static> Iterator for PIter<T> {
|
||||
type Item = &'static T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
unsafe {
|
||||
let r = Some(&*self.0);
|
||||
self.0 = self.0.offset(1isize);
|
||||
r
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> Iterator for PIterMut<T> {
|
||||
type Item = &'static mut T;
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
unsafe {
|
||||
let r = Some(&mut *self.0);
|
||||
self.0 = self.0.offset(1isize);
|
||||
r
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T> Pointer for *const T {
|
||||
type IterType = PIter<T>;
|
||||
fn into_iter(self) -> Self::IterType { PIter(self) }
|
||||
}
|
||||
impl<T> Pointer for *mut T {
|
||||
type IterType = PIterMut<T>;
|
||||
fn into_iter(self) -> Self::IterType { PIterMut(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iterate<T: pointer_iterator::Pointer>(pointer: T) -> T::IterType {
|
||||
pointer.into_iter()
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ pub use cffi::*;
|
||||
pub mod win32;
|
||||
|
||||
#[cfg(feature = "win32")]
|
||||
pub use win32::{ image_base, image_header, find_kernel32 };
|
||||
pub use win32::{ image_base, image_header, find_kernel32, process_executable };
|
||||
|
||||
|
||||
/// re-export the signature macro
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
|
||||
#[test]
|
||||
pub fn test_distance() {
|
||||
let _ = x::dur![ 5 days 4 hours 7 minutes 2 seconds 2 minutes ];
|
||||
|
||||
let a = x::dur![ 5 days 4 hours 7 minutes 2 seconds 2 minutes ];
|
||||
|
||||
let a = [0u8,2,3];
|
||||
let a = [0u8, 2, 3];
|
||||
let p1 = &a[0];
|
||||
let p2 = &a[2];
|
||||
assert_eq!(x::distance(p1, p2), 2);
|
||||
assert_eq!(x::distance(p2, p1), 2);
|
||||
assert_eq!(x::distance(p1, p1), 0);
|
||||
|
||||
|
||||
let a = b"Hello World\0".as_ptr();
|
||||
assert_eq!(Some(11), x::iterate(a).position(|&a| a == 0));
|
||||
|
||||
let b = b"H\0e\0l\0l\0o\0 \0W\0o\0r\0l\0d\0\0\0".as_ptr() as *const u16;
|
||||
let bytes: Vec<u16> = x::iterate(b)
|
||||
.take_while(|&&c| c != 0)
|
||||
.cloned().collect();
|
||||
assert_eq!("Hello World", String::from_utf16_lossy(bytes.as_slice()));
|
||||
|
||||
|
||||
let hello_world: String = char::decode_utf16(
|
||||
x::iterate(b).cloned().take_while(|&c| c != 0))
|
||||
.filter_map(|_r| _r.ok()).collect();
|
||||
assert_eq!("Hello World", hello_world);
|
||||
}
|
||||
Reference in New Issue
Block a user