From 09deee0c4dd5f1d5a663c2a29dee96ad3448a2d3 Mon Sep 17 00:00:00 2001 From: Dennis Date: Fri, 22 Apr 2022 15:15:39 +0200 Subject: [PATCH] Make graphene_core no_std --- node-graph/Cargo.lock | 7 ++++--- node-graph/Cargo.toml | 1 + node-graph/borrow_stack/Cargo.toml | 8 ++++++++ node-graph/borrow_stack/src/lib.rs | 20 ++++++++++++++++++++ node-graph/gcore/Cargo.toml | 5 ++++- node-graph/gcore/src/generic.rs | 2 +- node-graph/gcore/src/lib.rs | 28 ++-------------------------- node-graph/gcore/src/ops.rs | 6 +++--- node-graph/gcore/src/structural.rs | 2 +- node-graph/gcore/src/value.rs | 21 +-------------------- node-graph/gstd/src/lib.rs | 28 +++++++++++++++++++++++++++- node-graph/gstd/src/value.rs | 22 ++++++++++++++++++++++ 12 files changed, 94 insertions(+), 56 deletions(-) create mode 100644 node-graph/borrow_stack/Cargo.toml create mode 100644 node-graph/borrow_stack/src/lib.rs create mode 100644 node-graph/gstd/src/value.rs diff --git a/node-graph/Cargo.lock b/node-graph/Cargo.lock index 53d3da95..25d2d7ef 100644 --- a/node-graph/Cargo.lock +++ b/node-graph/Cargo.lock @@ -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" diff --git a/node-graph/Cargo.toml b/node-graph/Cargo.toml index 994bae30..9314040d 100644 --- a/node-graph/Cargo.toml +++ b/node-graph/Cargo.toml @@ -4,4 +4,5 @@ members = [ "proc-macro", "gcore", "gstd", + "borrow_stack" ] diff --git a/node-graph/borrow_stack/Cargo.toml b/node-graph/borrow_stack/Cargo.toml new file mode 100644 index 00000000..ae134cdb --- /dev/null +++ b/node-graph/borrow_stack/Cargo.toml @@ -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] diff --git a/node-graph/borrow_stack/src/lib.rs b/node-graph/borrow_stack/src/lib.rs new file mode 100644 index 00000000..20be5a9b --- /dev/null +++ b/node-graph/borrow_stack/src/lib.rs @@ -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 { + data: S, +} + +#[cfg(test)] +mod tests { + #[test] + fn it_works() { + let result = 2 + 2; + assert_eq!(result, 4); + } +} diff --git a/node-graph/gcore/Cargo.toml b/node-graph/gcore/Cargo.toml index 692742dc..f7eb0ebf 100644 --- a/node-graph/gcore/Cargo.toml +++ b/node-graph/gcore/Cargo.toml @@ -7,6 +7,9 @@ authors = ["Dennis Kobert "] # 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} diff --git a/node-graph/gcore/src/generic.rs b/node-graph/gcore/src/generic.rs index c361ad9d..7d4e9c73 100644 --- a/node-graph/gcore/src/generic.rs +++ b/node-graph/gcore/src/generic.rs @@ -1,4 +1,4 @@ -use std::{borrow::Borrow, marker::PhantomData}; +use core::{borrow::Borrow, marker::PhantomData}; use crate::Node; pub struct FnNode O, In, O>(T, PhantomData, PhantomData); diff --git a/node-graph/gcore/src/lib.rs b/node-graph/gcore/src/lib.rs index 17e9d5cf..3b9d6a3c 100644 --- a/node-graph/gcore/src/lib.rs +++ b/node-graph/gcore/src/lib.rs @@ -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::(input).unwrap_or_else(|| { - panic!( - "Node was evaluated with wrong input. The input has to be of type: {}", - std::any::type_name::(), - ) - })) - } -} diff --git a/node-graph/gcore/src/ops.rs b/node-graph/gcore/src/ops.rs index 2da62e04..75145e73 100644 --- a/node-graph/gcore/src/ops.rs +++ b/node-graph/gcore/src/ops.rs @@ -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(PhantomData); -impl<'n, T: std::ops::Add + Copy + 'n> Node<'n, (T, T)> for AddNode { - type Output = ::Output; +impl<'n, T: Add + Copy + 'n> Node<'n, (T, T)> for AddNode { + type Output = ::Output; fn eval(&'n self, input: &'n (T, T)) -> T::Output { let (ref a, ref b) = input.borrow(); *a + *b diff --git a/node-graph/gcore/src/structural.rs b/node-graph/gcore/src/structural.rs index dffbfd1d..d1acf51b 100644 --- a/node-graph/gcore/src/structural.rs +++ b/node-graph/gcore/src/structural.rs @@ -1,4 +1,4 @@ -use std::marker::PhantomData; +use core::marker::PhantomData; use crate::Node; diff --git a/node-graph/gcore/src/value.rs b/node-graph/gcore/src/value.rs index f8c010c4..4d711201 100644 --- a/node-graph/gcore/src/value.rs +++ b/node-graph/gcore/src/value.rs @@ -1,4 +1,4 @@ -use std::{any::Any, marker::PhantomData}; +use core::marker::PhantomData; use crate::Node; @@ -38,25 +38,6 @@ impl DefaultNode { } } -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; diff --git a/node-graph/gstd/src/lib.rs b/node-graph/gstd/src/lib.rs index 62abb18b..067ff34d 100644 --- a/node-graph/gstd/src/lib.rs +++ b/node-graph/gstd/src/lib.rs @@ -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::(input).unwrap_or_else(|| { + panic!( + "Node was evaluated with wrong input. The input has to be of type: {}", + std::any::type_name::(), + ) + })) + } +} diff --git a/node-graph/gstd/src/value.rs b/node-graph/gstd/src/value.rs new file mode 100644 index 00000000..f76364d0 --- /dev/null +++ b/node-graph/gstd/src/value.rs @@ -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) + } +}