Skip to content

Commit

Permalink
revert generic over Node
Browse files Browse the repository at this point in the history
  • Loading branch information
cksac committed Dec 24, 2024
1 parent 6bf18ca commit cfa966f
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 201 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "compose-rt"
version = "0.18.0"
version = "0.19.0"
edition = "2021"
authors = ["cksac <[email protected]>"]
description = "A positional memoization runtime similar to Jetpack Compose Runtime."
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ A positional memoization runtime similar to Jetpack Compose Runtime.
```rust
use std::env;

use compose_rt::node::{Node, NodeData};
use compose_rt::{Composer, Root};
use compose_rt::{ComposeNode, Composer, Root};

#[derive(Debug)]
pub struct Data(String);
Expand All @@ -29,11 +28,11 @@ impl From<&str> for Data {
}
}

impl NodeData for Data {
impl ComposeNode for Data {
type Context = ();
}

type Scope<S> = compose_rt::Scope<S, Node<Data>>;
type Scope<S> = compose_rt::Scope<S, Data>;

pub struct Div;
pub struct Button;
Expand Down
3 changes: 1 addition & 2 deletions benches/basic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::hint::black_box;

use compose_rt::node::Node;
use compose_rt::{Composer, Root};
use criterion::{criterion_group, criterion_main, Criterion};

type Scope<S> = compose_rt::Scope<S, Node<()>>;
type Scope<S> = compose_rt::Scope<S, ()>;

pub struct Div;
pub struct Button;
Expand Down
7 changes: 3 additions & 4 deletions examples/any_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::any::Any;
use std::env;
use std::fmt::Debug;

use compose_rt::node::{Node, NodeData};
use compose_rt::{AnyData, Composer, Root};
use compose_rt::{AnyData, ComposeNode, Composer, Root};

pub trait Data: Debug + 'static {
fn as_any(&self) -> &dyn Any;
Expand All @@ -27,11 +26,11 @@ where
}
}

impl NodeData for Box<dyn Data> {
impl ComposeNode for Box<dyn Data> {
type Context = ();
}

type Scope<S> = compose_rt::Scope<S, Node<Box<dyn Data>>>;
type Scope<S> = compose_rt::Scope<S, Box<dyn Data>>;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Div;
Expand Down
7 changes: 3 additions & 4 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::env;

use compose_rt::node::{Node, NodeData};
use compose_rt::{Composer, Root};
use compose_rt::{ComposeNode, Composer, Root};

#[derive(Debug)]
pub struct Data(String);
Expand All @@ -18,11 +17,11 @@ impl From<&str> for Data {
}
}

impl NodeData for Data {
impl ComposeNode for Data {
type Context = ();
}

type Scope<S> = compose_rt::Scope<S, Node<Data>>;
type Scope<S> = compose_rt::Scope<S, Data>;

pub struct Div;
pub struct Button;
Expand Down
7 changes: 3 additions & 4 deletions examples/mini_demo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt::Debug;

use compose_rt::node::{Node, NodeData};
use compose_rt::{Composer, Loc, Root};
use compose_rt::{ComposeNode, Composer, Loc, Root};

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Data(Loc);
Expand All @@ -26,11 +25,11 @@ impl Debug for Data {
}
}

impl NodeData for Data {
impl ComposeNode for Data {
type Context = usize;
}

type Scope<S> = compose_rt::Scope<S, Node<Data>>;
type Scope<S> = compose_rt::Scope<S, Data>;

pub struct Container;
pub struct Left;
Expand Down
9 changes: 4 additions & 5 deletions examples/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt::Debug;

use compose_rt::node::{Node, NodeData};
use compose_rt::{Composer, Loc, Root};
use compose_rt::{ComposeNode, Composer, Loc, Root};

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Data(Loc);
Expand All @@ -26,12 +25,12 @@ impl Debug for Data {
}
}

impl NodeData for Data {
impl ComposeNode for Data {
type Context = ();
}

type Scope<S> = compose_rt::Scope<S, Node<Data>>;
type State<T> = compose_rt::State<T, Node<Data>>;
type Scope<S> = compose_rt::Scope<S, Data>;
type State<T> = compose_rt::State<T, Data>;

pub struct Container;
pub struct Left;
Expand Down
7 changes: 3 additions & 4 deletions examples/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::fmt::Debug;

use compose_rt::node::{Node, NodeData};
use compose_rt::{Composer, Loc, Root};
use compose_rt::{ComposeNode, Composer, Loc, Root};

#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Data(Loc);
Expand All @@ -26,11 +25,11 @@ impl Debug for Data {
}
}

impl NodeData for Data {
impl ComposeNode for Data {
type Context = usize;
}

type Scope<S> = compose_rt::Scope<S, Node<Data>>;
type Scope<S> = compose_rt::Scope<S, Data>;

pub struct Container;
pub struct Left;
Expand Down
75 changes: 37 additions & 38 deletions src/composer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::any::Any;
use std::fmt::{Debug, Formatter};
use std::ops::RangeBounds;
use std::vec::Drain;

use generational_box::{AnyStorage, UnsyncStorage};
use slab::Slab;
Expand Down Expand Up @@ -35,29 +33,10 @@ impl Clone for Box<dyn Composable> {

pub trait ComposeNode: 'static {
type Context;
type Data;

fn new(scope_id: ScopeId, parent: NodeKey) -> Self;
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;

fn scope_id(&self) -> ScopeId;
fn set_scope_id(&mut self, scope_id: ScopeId);

fn parent(&self) -> NodeKey;
fn set_parent(&mut self, parent: NodeKey);

fn data(&self) -> Option<&Self::Data>;
fn data_mut(&mut self) -> Option<&mut Self::Data>;
fn set_data(&mut self, data: Self::Data);
}

fn children(&self) -> &[NodeKey];
fn children_mut(&mut self) -> &mut [NodeKey];
fn children_push(&mut self, node_key: NodeKey);
fn children_len(&self) -> usize;
fn children_drain<R>(&mut self, range: R) -> Drain<NodeKey>
where
R: RangeBounds<usize>;
impl ComposeNode for () {
type Context = ();
}

pub trait AnyData<T> {
Expand Down Expand Up @@ -88,12 +67,32 @@ where

pub type NodeKey = usize;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node<T> {
pub scope_id: ScopeId,
pub parent: NodeKey,
pub children: Vec<NodeKey>,
pub data: Option<T>,
}

impl<T> Node<T> {
#[inline(always)]
pub fn new(scope_id: ScopeId, parent: NodeKey) -> Self {
Self {
scope_id,
parent,
children: Vec::new(),
data: None,
}
}
}

pub struct Composer<N>
where
N: ComposeNode,
{
pub context: N::Context,
pub nodes: Slab<N>,
pub nodes: Slab<Node<N>>,
pub(crate) initialized: bool,
pub(crate) root_node_key: NodeKey,
pub(crate) composables: Map<NodeKey, Box<dyn Composable>>,
Expand Down Expand Up @@ -203,7 +202,7 @@ where
#[inline(always)]
pub(crate) fn start_root(&mut self, scope_id: ScopeId) {
let parent_node_key = 0;
let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
let node_key = self.nodes.insert(Node::new(scope_id, parent_node_key));
self.child_idx_stack.push(0);
self.current_node_key = node_key;
}
Expand All @@ -212,7 +211,7 @@ where
pub(crate) fn end_root(&mut self) {
let child_count = self.child_idx_stack.pop().unwrap();
assert_eq!(1, child_count, "Root scope must have exactly one child");
self.root_node_key = self.nodes[self.current_node_key].children()[0];
self.root_node_key = self.nodes[self.current_node_key].children[0];
}

#[inline(always)]
Expand All @@ -221,27 +220,27 @@ where
let child_idx = self.child_idx_stack.last().cloned();
if let Some(child_idx) = child_idx {
let parent_node = &mut self.nodes[parent_node_key];
if child_idx < parent_node.children_len() {
let child_key = parent_node.children()[child_idx];
if child_idx < parent_node.children.len() {
let child_key = parent_node.children[child_idx];
let child_node = &mut self.nodes[child_key];
if child_node.scope_id() == scope_id {
if child_node.scope_id == scope_id {
// reuse existing node
self.current_node_key = child_key;
self.mount_nodes.insert(child_key);
self.child_idx_stack.push(0);
} else {
// replace existing node
let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
self.nodes[parent_node_key].children_mut()[child_idx] = node_key;
let node_key = self.nodes.insert(Node::new(scope_id, parent_node_key));
self.nodes[parent_node_key].children[child_idx] = node_key;
self.unmount_nodes.insert(child_key);
self.mount_nodes.insert(node_key);
self.current_node_key = node_key;
self.child_idx_stack.push(0);
}
} else {
// append new node
let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
self.nodes[parent_node_key].children_push(node_key);
let node_key = self.nodes.insert(Node::new(scope_id, parent_node_key));
self.nodes[parent_node_key].children.push(node_key);
self.mount_nodes.insert(node_key);
self.current_node_key = node_key;
self.child_idx_stack.push(0);
Expand All @@ -252,8 +251,8 @@ where
}
} else {
// first compose
let node_key = self.nodes.insert(N::new(scope_id, parent_node_key));
self.nodes[parent_node_key].children_push(node_key);
let node_key = self.nodes.insert(Node::new(scope_id, parent_node_key));
self.nodes[parent_node_key].children.push(node_key);
self.current_node_key = node_key;
self.child_idx_stack.push(0);
}
Expand All @@ -263,9 +262,9 @@ where
pub(crate) fn end_node(&mut self, parent_node_key: NodeKey) {
let child_count = self.child_idx_stack.pop().unwrap();
let node = &mut self.nodes[self.current_node_key];
let old_child_count = node.children_len();
let old_child_count = node.children.len();
if child_count < old_child_count {
let unmount_nodes = node.children_drain(child_count..);
let unmount_nodes = node.children.drain(child_count..);
self.unmount_nodes.extend(unmount_nodes);
}
if let Some(parent_child_count) = self.child_idx_stack.last_mut() {
Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod loc;
pub use loc::Loc;

mod composer;
pub use composer::{AnyData, Composable, ComposeNode, Composer, NodeKey};
pub use composer::{AnyData, Composable, ComposeNode, Composer, Node, NodeKey};

mod recomposer;
pub use recomposer::Recomposer;
Expand All @@ -18,5 +18,3 @@ pub use scope::{Root, Scope, ScopeId};
pub mod utils;

mod map;

pub mod node;
Loading

0 comments on commit cfa966f

Please sign in to comment.