Make graphene_core no_std

This commit is contained in:
Dennis 2022-04-22 15:15:39 +02:00 committed by Keavon Chambers
parent 90e465b35c
commit 09deee0c4d
12 changed files with 94 additions and 56 deletions

7
node-graph/Cargo.lock generated
View File

@ -35,6 +35,10 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "borrow_stack"
version = "0.1.0"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -253,9 +257,6 @@ dependencies = [
[[package]]
name = "graphene-core"
version = "0.1.0"
dependencies = [
"dyn-any",
]
[[package]]
name = "graphene-std"

View File

@ -4,4 +4,5 @@ members = [
"proc-macro",
"gcore",
"gstd",
"borrow_stack"
]

View File

@ -0,0 +1,8 @@
[package]
name = "borrow_stack"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@ -0,0 +1,20 @@
trait BorrowStack {
type Item;
unsafe fn push(&mut self, T) -> &Item;
unsafe fn pop(&mut self) -> T;
unsafe fn get(&self) -> &T;
}
struct BorrowStack<S> {
data: S,
}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
let result = 2 + 2;
assert_eq!(result, 4);
}
}

View File

@ -7,6 +7,9 @@ authors = ["Dennis Kobert <dennis@kobert.dev>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
#std = []
#default = ["std"]
[dependencies]
dyn-any = {path = "../dyn-any", features = ["derive"]}
#dyn-any = {path = "../dyn-any", features = ["derive"], optional = true}

View File

@ -1,4 +1,4 @@
use std::{borrow::Borrow, marker::PhantomData};
use core::{borrow::Borrow, marker::PhantomData};
use crate::Node;
pub struct FnNode<T: Fn(&In) -> O, In, O>(T, PhantomData<In>, PhantomData<O>);

View File

@ -1,11 +1,10 @@
#![no_std]
pub mod generic;
pub mod ops;
pub mod structural;
pub mod value;
use dyn_any::{downcast_ref, DynAny, StaticType};
use std::any::Any;
#[rustfmt::skip]
pub trait Node< 'n, Input> {
type Output : 'n;
@ -23,26 +22,3 @@ impl<'n, T: Node<'n, ()>> Exec<'n> for T {}
pub trait Cache {
fn clear(&mut self);
}
pub type DynNode<'n, T> = &'n (dyn Node<'n, (), Output = T> + 'n);
pub type DynAnyNode<'n> = &'n (dyn Node<'n, (), Output = &'n dyn DynAny<'n>> + 'n);
pub trait DynamicInput<'n> {
fn set_kwarg_by_name(&mut self, name: &str, value: DynAnyNode<'n>);
fn set_arg_by_index(&mut self, index: usize, value: DynAnyNode<'n>);
}
pub trait AnyRef<'n, I: StaticType<'n>>: Node<'n, I> {
fn any(&'n self, input: &'n dyn DynAny<'n>) -> Self::Output;
}
impl<'n, T: Node<'n, I>, I: StaticType<'n>> AnyRef<'n, I> for T {
fn any(&'n self, input: &'n dyn DynAny<'n>) -> Self::Output {
self.eval(downcast_ref::<I>(input).unwrap_or_else(|| {
panic!(
"Node was evaluated with wrong input. The input has to be of type: {}",
std::any::type_name::<I>(),
)
}))
}
}

View File

@ -1,11 +1,11 @@
use std::{borrow::Borrow, marker::PhantomData};
use core::{borrow::Borrow, marker::PhantomData, ops::Add};
use crate::Node;
#[derive(Default)]
pub struct AddNode<T>(PhantomData<T>);
impl<'n, T: std::ops::Add + Copy + 'n> Node<'n, (T, T)> for AddNode<T> {
type Output = <T as std::ops::Add>::Output;
impl<'n, T: Add + Copy + 'n> Node<'n, (T, T)> for AddNode<T> {
type Output = <T as Add>::Output;
fn eval(&'n self, input: &'n (T, T)) -> T::Output {
let (ref a, ref b) = input.borrow();
*a + *b

View File

@ -1,4 +1,4 @@
use std::marker::PhantomData;
use core::marker::PhantomData;
use crate::Node;

View File

@ -1,4 +1,4 @@
use std::{any::Any, marker::PhantomData};
use core::marker::PhantomData;
use crate::Node;
@ -38,25 +38,6 @@ impl<T> DefaultNode<T> {
}
}
use dyn_any::{DynAny, StaticType};
pub struct AnyRefNode<'n, N: Node<'n, I, Output = &'n O>, I, O>(
&'n N,
PhantomData<&'n I>,
PhantomData<&'n O>,
);
impl<'n, N: Node<'n, I, Output = &'n O>, I, O: DynAny<'n>> Node<'n, I> for AnyRefNode<'n, N, I, O> {
type Output = &'n (dyn DynAny<'n>);
fn eval(&'n self, input: &'n I) -> Self::Output {
let value: &O = self.0.eval(input);
value
}
}
impl<'n, N: Node<'n, I, Output = &'n O>, I, O: 'static> AnyRefNode<'n, N, I, O> {
pub fn new(n: &'n N) -> AnyRefNode<'n, N, I, O> {
AnyRefNode(n, PhantomData, PhantomData)
}
}
pub struct DefaultRefNode<'n, T>(ValueNode<'n, T>);
impl<'n, T: 'n> Node<'n, ()> for DefaultRefNode<'n, T> {
type Output = &'n T;

View File

@ -1,4 +1,5 @@
//#![feature(generic_associated_types)]
pub mod value;
pub use graphene_core::{generic, ops, structural};
#[cfg(feature = "caching")]
pub mod caching;
@ -6,3 +7,28 @@ pub mod caching;
pub mod memo;
pub use graphene_core::*;
use dyn_any::{downcast_ref, DynAny, StaticType};
use std::any::Any;
pub type DynNode<'n, T> = &'n (dyn Node<'n, (), Output = T> + 'n);
pub type DynAnyNode<'n> = &'n (dyn Node<'n, (), Output = &'n dyn DynAny<'n>> + 'n);
pub trait DynamicInput<'n> {
fn set_kwarg_by_name(&mut self, name: &str, value: DynAnyNode<'n>);
fn set_arg_by_index(&mut self, index: usize, value: DynAnyNode<'n>);
}
pub trait AnyRef<'n, I: StaticType<'n>>: Node<'n, I> {
fn any(&'n self, input: &'n dyn DynAny<'n>) -> Self::Output;
}
impl<'n, T: Node<'n, I>, I: StaticType<'n>> AnyRef<'n, I> for T {
fn any(&'n self, input: &'n dyn DynAny<'n>) -> Self::Output {
self.eval(downcast_ref::<I>(input).unwrap_or_else(|| {
panic!(
"Node was evaluated with wrong input. The input has to be of type: {}",
std::any::type_name::<I>(),
)
}))
}
}

View File

@ -0,0 +1,22 @@
use core::marker::PhantomData;
pub use graphene_core::value::*;
use graphene_core::Node;
use dyn_any::{DynAny, StaticType};
pub struct AnyRefNode<'n, N: Node<'n, I, Output = &'n O>, I, O>(
&'n N,
PhantomData<&'n I>,
PhantomData<&'n O>,
);
impl<'n, N: Node<'n, I, Output = &'n O>, I, O: DynAny<'n>> Node<'n, I> for AnyRefNode<'n, N, I, O> {
type Output = &'n (dyn DynAny<'n>);
fn eval(&'n self, input: &'n I) -> Self::Output {
let value: &O = self.0.eval(input);
value
}
}
impl<'n, N: Node<'n, I, Output = &'n O>, I, O: 'static> AnyRefNode<'n, N, I, O> {
pub fn new(n: &'n N) -> AnyRefNode<'n, N, I, O> {
AnyRefNode(n, PhantomData, PhantomData)
}
}