-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from spastorino/non-future-impl-traits
Properly support non future impl traits
- Loading branch information
Showing
7 changed files
with
283 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[dynosaur::dynosaur(DynSomeTrait)] | ||
trait SomeTrait { | ||
fn get_iter(&mut self) -> impl Iterator<Item = u8> + '_; | ||
} | ||
|
||
struct MyImpl([u8; 4]); | ||
|
||
impl SomeTrait for MyImpl { | ||
fn get_iter(&mut self) -> impl Iterator<Item = u8> + '_ { | ||
return self.0.into_iter(); | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut st = DynSomeTrait::boxed(MyImpl([3,2,4,1])); | ||
for x in st.get_iter() { | ||
println!("{}", x); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#![feature(prelude_import)] | ||
#[prelude_import] | ||
use std::prelude::rust_2021::*; | ||
#[macro_use] | ||
extern crate std; | ||
trait SomeTrait { | ||
fn get_iter(&mut self) | ||
-> impl Iterator<Item = u8> + '_; | ||
} | ||
mod _dynosaur_macro_dynsometrait { | ||
use super::*; | ||
trait ErasedSomeTrait { | ||
fn get_iter<'life0, 'dynosaur>(&'life0 mut self) | ||
-> Box<dyn Iterator<Item = u8> + 'dynosaur> | ||
where | ||
'life0: 'dynosaur, | ||
Self: 'dynosaur; | ||
} | ||
impl<DYNOSAUR: SomeTrait> ErasedSomeTrait for DYNOSAUR { | ||
fn get_iter<'life0, 'dynosaur>(&'life0 mut self) | ||
-> Box<dyn Iterator<Item = u8> + 'dynosaur> where | ||
'life0: 'dynosaur, Self: 'dynosaur { | ||
Box::new(<Self as SomeTrait>::get_iter(self)) | ||
} | ||
} | ||
#[repr(transparent)] | ||
pub struct DynSomeTrait<'dynosaur_struct> { | ||
ptr: dyn ErasedSomeTrait + 'dynosaur_struct, | ||
} | ||
impl<'dynosaur_struct> SomeTrait for DynSomeTrait<'dynosaur_struct> { | ||
fn get_iter(&mut self) -> impl Iterator<Item = u8> + '_ { | ||
let ret: Box<dyn Iterator<Item = u8> + '_> = self.ptr.get_iter(); | ||
let ret: Box<dyn Iterator<Item = u8> + '_> = | ||
unsafe { ::core::mem::transmute(ret) }; | ||
ret | ||
} | ||
} | ||
impl<'dynosaur_struct> DynSomeTrait<'dynosaur_struct> { | ||
pub fn boxed(value: impl SomeTrait + 'dynosaur_struct) | ||
-> Box<DynSomeTrait<'dynosaur_struct>> { | ||
let value = Box::new(value); | ||
let value: Box<dyn ErasedSomeTrait + 'dynosaur_struct> = value; | ||
unsafe { ::core::mem::transmute(value) } | ||
} | ||
pub fn from_ref(value: &(impl SomeTrait + 'dynosaur_struct)) | ||
-> &DynSomeTrait<'dynosaur_struct> { | ||
let value: &(dyn ErasedSomeTrait + 'dynosaur_struct) = &*value; | ||
unsafe { ::core::mem::transmute(value) } | ||
} | ||
pub fn from_mut(value: &mut (impl SomeTrait + 'dynosaur_struct)) | ||
-> &mut DynSomeTrait<'dynosaur_struct> { | ||
let value: &mut (dyn ErasedSomeTrait + 'dynosaur_struct) = | ||
&mut *value; | ||
unsafe { ::core::mem::transmute(value) } | ||
} | ||
} | ||
} | ||
use _dynosaur_macro_dynsometrait::DynSomeTrait; | ||
|
||
struct MyImpl([u8; 4]); | ||
|
||
impl SomeTrait for MyImpl { | ||
fn get_iter(&mut self) -> impl Iterator<Item = u8> + '_ { | ||
return self.0.into_iter(); | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut st = DynSomeTrait::boxed(MyImpl([3, 2, 4, 1])); | ||
for x in st.get_iter() { | ||
{ ::std::io::_print(format_args!("{0}\n", x)); }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.