It... works?
This commit is contained in:
commit
9b6fe632bb
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
||||||
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "stack-dyn"
|
||||||
|
version = "0.1.0"
|
||||||
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
[package]
|
||||||
|
name = "stack-dyn"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
# rust-version = "nightly"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
2
rust-toolchain.toml
Normal file
2
rust-toolchain.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
[toolchain]
|
||||||
|
channel = "nightly"
|
||||||
1
rustfmt.toml
Normal file
1
rustfmt.toml
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
hard_tabs = true
|
||||||
87
src/lib.rs
Normal file
87
src/lib.rs
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
#![feature(
|
||||||
|
ptr_metadata,
|
||||||
|
ptr_as_ref_unchecked,
|
||||||
|
generic_const_exprs,
|
||||||
|
inherent_associated_types
|
||||||
|
)]
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
marker::PhantomData,
|
||||||
|
mem::MaybeUninit,
|
||||||
|
ops,
|
||||||
|
ptr::{self, addr_of_mut},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[repr(C, align(16))]
|
||||||
|
struct Data([u8; 48]);
|
||||||
|
|
||||||
|
pub struct Dyn<'a, Trait: ?Sized + 'a> {
|
||||||
|
_phantom1: PhantomData<Trait>,
|
||||||
|
_phantom2: PhantomData<&'a ()>,
|
||||||
|
meta: <Trait as ptr::Pointee>::Metadata,
|
||||||
|
data: Data,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Trait: ?Sized + 'a> Dyn<'a, Trait> {
|
||||||
|
pub type Target = Trait;
|
||||||
|
|
||||||
|
fn as_ptr(&self) -> *const Self::Target {
|
||||||
|
ptr::from_raw_parts::<Trait>(&self.data, self.meta)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Trait: ?Sized + 'a> ops::Deref for Dyn<'a, Trait> {
|
||||||
|
type Target = Trait;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
unsafe { self.as_ptr().as_ref_unchecked() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Trait: ?Sized + 'a> ops::DerefMut for Dyn<'a, Trait> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
unsafe { self.as_ptr().cast_mut().as_mut_unchecked() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Iter: Iterator + 'a> From<Iter> for Dyn<'a, dyn Iterator<Item = Iter::Item> + 'a>
|
||||||
|
where
|
||||||
|
[(); std::mem::size_of::<Data>() - std::mem::size_of::<Iter>()]:,
|
||||||
|
[(); std::mem::align_of::<Data>() - std::mem::align_of::<Iter>()]:,
|
||||||
|
{
|
||||||
|
fn from(value: Iter) -> Self {
|
||||||
|
let (_, meta) = (&value as &Self::Target as *const Self::Target).to_raw_parts();
|
||||||
|
unsafe {
|
||||||
|
let mut ret = MaybeUninit::<Self>::zeroed();
|
||||||
|
let pret = ret.as_mut_ptr();
|
||||||
|
(addr_of_mut!((*pret).data) as *mut Iter).write(value);
|
||||||
|
addr_of_mut!((*pret).meta).write(meta);
|
||||||
|
ret.assume_init()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
struct Small([u8; 48]);
|
||||||
|
|
||||||
|
impl Iterator for Small {
|
||||||
|
type Item = u8;
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
let ret = self.0.get(self.0[0] as usize).copied();
|
||||||
|
self.0[0] += 1;
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn it_works() {
|
||||||
|
let mut it = Small([42; 48]);
|
||||||
|
assert_eq!(it.next(), Some(42));
|
||||||
|
|
||||||
|
let mut it: Dyn<dyn Iterator<Item = u8>> = Small([42; 48]).into();
|
||||||
|
assert_eq!(it.next(), Some(42));
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user