minor refactoring

This commit is contained in:
2024-07-10 09:56:15 -04:00
parent 52d7280628
commit cc7063313f
6 changed files with 68 additions and 55 deletions

View File

@@ -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<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()
}
pub fn fill_with<T, F: FnMut(usize) -> T>(slice: &mut [T], mut func: F) {
slice.iter_mut().enumerate().for_each(|(i,v)|*v = func(i))
}

View File

@@ -1,7 +1,4 @@
use core::fmt;
#[derive(Clone)]
pub struct TakeUntil<I, P> {
iter: I,

View File

@@ -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

53
src/pointers.rs Normal file
View File

@@ -0,0 +1,53 @@
pub fn null<T: internal::Pointer>() -> T { T::new_null() }
pub fn iterate<T: internal::Pointer>(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<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) }
fn new_null() -> Self { core::ptr::null() }
}
impl<T> Pointer for *mut T {
type IterType = PIterMut<T>;
fn into_iter(self) -> Self::IterType { PIterMut(self) }
fn new_null() -> Self { core::ptr::null_mut() }
}
}

View File

@@ -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]);

View File

@@ -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) {}