-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Just a comment about -g2 if const fn never materializes
- Loading branch information
¨Jeff
committed
Mar 13, 2023
1 parent
36425dc
commit a5b3f91
Showing
5 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "any_tools" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
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,3 @@ | ||
Very unfinsihed, but if const fn were going to take too long | ||
then ideas here combined with lazy_static gives a substitute. | ||
|
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,44 @@ | ||
|
||
use ark_std::any::Any; | ||
|
||
// use std::{any::Any}; | ||
|
||
pub struct AnyLinkedList { | ||
fence: | ||
start: AnyLLPtr, | ||
} | ||
|
||
pub type AnyLLPtr = AnyLL<dyn Any>; | ||
|
||
pub struct AnyLL<T: ?Sized + 'static> { | ||
next: AnyLLPtr, | ||
any: T, | ||
} | ||
|
||
impl AnyLL<dyn Any> { | ||
pub fn try_fetch<'a,T: 'static>(&'a self) -> Option<&'a T> { | ||
let mut selfy: &'a Self = self; | ||
loop { | ||
if let Some(r) = selfy.any.downcast_ref::<T>() { return Some(r); } | ||
if selfy.next.is_none() { return None; } | ||
selfy = selfy.next.as_ref().unwrap() | ||
} | ||
} | ||
|
||
pub fn fetch_or_extend<'a,T: 'static,F: FnOnce() -> T>(&'a mut self, init: F) -> &'a T { | ||
let mut selfy: &'a mut Self = self; | ||
loop { | ||
if let Some(r) = selfy.any.downcast_ref::<T>() { return r; } | ||
if selfy.next.is_none() { | ||
let b = Box::new(AnyLL { next: None, any: init(), }); | ||
let new = unsafe { core::mem::transmute(&b.any) }; | ||
selfy.next = Some(b as Box<AnyLL<dyn Any>>); | ||
return new; | ||
// We deduce the above code is safe because you could comment | ||
// the two lines containing new and uncomment the line below: | ||
// return selfy.next.as_mut().unwrap().any.downcast_ref::<T>().unwrap(); | ||
} | ||
selfy = selfy.next.as_mut().unwrap() | ||
} | ||
} | ||
} |
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,57 @@ | ||
|
||
|
||
use ark_std::any::Any; | ||
|
||
// use std::{any::Any}; | ||
|
||
pub struct AnyLinkedList { | ||
fence: | ||
start: AnyLLPtr, | ||
} | ||
|
||
pub type AnyLLPtr = Cell<Option<Box<AnyLL<dyn Any>>>>; | ||
|
||
pub struct AnyLL<T: ?Sized + 'static> { | ||
next: AnyLLPtr, | ||
any: T, | ||
} | ||
|
||
impl AnyLL<dyn Any> { | ||
pub fn try_fetch<'a,T: 'static>(&'a self) -> Option<&'a T> { | ||
let mut selfy: &'a Self = self; | ||
loop { | ||
if let Some(r) = selfy.any.downcast_ref::<T>() { return Some(r); } | ||
if selfy.next.is_none() { return None; } | ||
selfy = selfy.next.as_ref().unwrap() | ||
} | ||
} | ||
|
||
pub fn fetch_or_extend<'a,T: 'static,F: FnOnce() -> T>(&'a mut self, init: F) -> &'a T { | ||
let mut selfy: &'a mut Self = self; | ||
loop { | ||
if let Some(r) = selfy.any.downcast_ref::<T>() { return r; } | ||
if selfy.next.is_none() { | ||
let b = Box::new(AnyLL { next: None, any: init(), }); | ||
let new = unsafe { core::mem::transmute(&b.any) }; | ||
selfy.next = Some(b as Box<AnyLL<dyn Any>>); | ||
return new; | ||
// We deduce the above code is safe because you could comment | ||
// the two lines containing new and uncomment the line below: | ||
// return selfy.next.as_mut().unwrap().any.downcast_ref::<T>().unwrap(); | ||
} | ||
selfy = selfy.next.as_mut().unwrap() | ||
} | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} |
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