Skip to content

Commit

Permalink
Move State to ravel core crate
Browse files Browse the repository at this point in the history
  • Loading branch information
kmicklas committed Jun 17, 2024
1 parent bbc1101 commit 29873b5
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 41 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
Expand Up @@ -18,6 +18,6 @@ gloo-utils = "0.2.0"
log = "0.4.21"
paste = "1.0.15"
ravel = { version = "0.2.0", path = "./ravel" }
ravel-web = { version = "0.2.0", path = "./ravel-web" }
ravel-web = { version = "0.3.0", path = "./ravel-web" }
wasm-bindgen-futures = "0.4.42"
web-sys = "0.3.69"
2 changes: 1 addition & 1 deletion ravel-web/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ravel-web"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
description = "An experimental approach to UI in Rust with a focus on ergonomics, efficiency, and simplicity."
license = "MIT"
Expand Down
3 changes: 2 additions & 1 deletion ravel-web/src/any.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{any::Any, marker::PhantomData, ops::DerefMut};

use ravel::State;
use web_sys::wasm_bindgen::UnwrapThrowExt as _;

use crate::{
dom::{clear, Position},
BuildCx, Builder, RebuildCx, State, View, ViewMarker, Web,
BuildCx, Builder, RebuildCx, View, ViewMarker, Web,
};

/// Trait for upcasting to [`Any`], implemented automatically.
Expand Down
4 changes: 2 additions & 2 deletions ravel-web/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use std::marker::PhantomData;

use ravel::Builder;
use ravel::{Builder, State};
use web_sys::wasm_bindgen::UnwrapThrowExt as _;

use crate::{BuildCx, RebuildCx, State, Web};
use crate::{BuildCx, RebuildCx, Web};

/// Trait to identify attribute types.
pub trait AttrKind: 'static {
Expand Down
4 changes: 2 additions & 2 deletions ravel-web/src/collections/btree_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use std::{
cmp::Ordering, collections::BTreeMap, marker::PhantomData, ops::Bound,
};

use ravel::{with, Token};
use ravel::{with, State, Token};
use web_sys::wasm_bindgen::UnwrapThrowExt;

use crate::{
dom::{clear, Position},
BuildCx, Builder, Cx, RebuildCx, State, Web,
BuildCx, Builder, Cx, RebuildCx, Web,
};

pub struct BTreeMapBuilder<'data, K, V, RenderItem, S> {
Expand Down
4 changes: 2 additions & 2 deletions ravel-web/src/collections/slice.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{cmp::Ordering, marker::PhantomData};

use ravel::{with, Token};
use ravel::{with, State, Token};
use web_sys::wasm_bindgen::UnwrapThrowExt;

use crate::{
dom::{clear, Position},
BuildCx, Builder, Cx, RebuildCx, State, Web,
BuildCx, Builder, Cx, RebuildCx, Web,
};

pub struct SliceBuilder<'data, T, RenderItem, S> {
Expand Down
5 changes: 2 additions & 3 deletions ravel-web/src/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

use std::marker::PhantomData;

use ravel::State;
use web_sys::wasm_bindgen::{JsValue, UnwrapThrowExt};

use crate::{
dom::Position, BuildCx, Builder, RebuildCx, State, ViewMarker, Web,
};
use crate::{dom::Position, BuildCx, Builder, RebuildCx, ViewMarker, Web};

/// Trait to identify element types.
pub trait ElKind: 'static {
Expand Down
3 changes: 2 additions & 1 deletion ravel-web/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

use std::{cell::RefCell, marker::PhantomData, ops::DerefMut, rc::Rc};

use ravel::State;
use web_sys::wasm_bindgen::JsValue;

use crate::{BuildCx, Builder, RebuildCx, State, Web};
use crate::{BuildCx, Builder, RebuildCx, Web};

/// Trait to identify event types.
pub trait EventKind: 'static {
Expand Down
27 changes: 5 additions & 22 deletions ravel-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,11 @@ pub struct RebuildCx<'cx> {
waker: &'cx Arc<AtomicWaker>,
}

/// Trait for the state of a [`Web`] component.
pub trait State<Output>: AsAny {
/// Processes a "frame".
///
/// This method can respond to externally triggered events by changing the
/// `Output`.
fn run(&mut self, output: &mut Output);
}

/// A marker trait for the [`State`] types of a [`trait@View`].
/// A marker trait for the [`ravel::State`] types of a [`trait@View`].
pub trait ViewMarker {}

macro_rules! tuple_state {
($($a:ident),*) => {
#[allow(non_camel_case_types)]
impl<$($a,)* O> State<O> for ($($a,)*)
where
$($a: State<O>,)*
{
fn run(&mut self, _output: &mut O) {
let ($($a,)*) = self;
$($a.run(_output);)*
}
}

#[allow(non_camel_case_types)]
impl<$($a),*> ViewMarker for ($($a,)*)
where
Expand Down Expand Up @@ -109,6 +89,9 @@ where
pub trait Captures<'a> {}
impl<'a, T: ?Sized> Captures<'a> for T {}

#[doc(hidden)]
pub use ravel::State as ViewState;

/// A convenience macro for declaring a [`trait@View`] type.
///
/// The first parameter is the `Output` type of the [`trait@View`]'s
Expand All @@ -117,7 +100,7 @@ impl<'a, T: ?Sized> Captures<'a> for T {}
macro_rules! View {
($output:ty $(, $a:lifetime)*) => {
impl $crate::View<
ViewState = impl $crate::ViewMarker + $crate::State<$output>
ViewState = impl $crate::ViewMarker + $crate::ViewState<$output>
> $(+ $crate::Captures<$a>)*
};
}
3 changes: 2 additions & 1 deletion ravel-web/src/option.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use ravel::State;
use web_sys::wasm_bindgen::UnwrapThrowExt as _;

use crate::{
dom::{clear, Position},
BuildCx, Builder, RebuildCx, State, View, ViewMarker, Web,
BuildCx, Builder, RebuildCx, View, ViewMarker, Web,
};

impl<V: View> Builder<Web> for Option<V> {
Expand Down
4 changes: 2 additions & 2 deletions ravel-web/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
use std::sync::Arc;

use atomic_waker::AtomicWaker;
use ravel::{with, Builder, Token};
use ravel::{with, Builder, State, Token};
use web_sys::wasm_bindgen::JsValue;

use crate::{dom::Position, BuildCx, Cx, RebuildCx, State, Web};
use crate::{dom::Position, BuildCx, Cx, RebuildCx, Web};

/// Runs a component on an arbitrary [`web_sys::Element`].
///
Expand Down
4 changes: 2 additions & 2 deletions ravel-web/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::{
fmt::{Arguments, Write},
};

use ravel::Builder;
use ravel::{Builder, State};
use web_sys::wasm_bindgen::UnwrapThrowExt;

use crate::{BuildCx, RebuildCx, State, ViewMarker, Web};
use crate::{BuildCx, RebuildCx, ViewMarker, Web};

/// A text node.
pub struct Text<Value: ToString + AsRef<str>> {
Expand Down
14 changes: 14 additions & 0 deletions ravel/src/any.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::any::Any;

/// Trait for upcasting to [`Any`], implemented automatically.
///
/// This is a workaround until `trait_upcasting` is stabilized.
pub trait AsAny: Any {
fn as_mut_dyn_any(&mut self) -> &mut dyn Any;
}

impl<T: Any> AsAny for T {
fn as_mut_dyn_any(&mut self) -> &mut dyn Any {
self
}
}
38 changes: 38 additions & 0 deletions ravel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use std::{marker::PhantomData, mem::MaybeUninit};

use paste::paste;

mod any;

pub use any::*;

/// A dummy type which typically represents a "backend".
pub trait CxRep {
type BuildCx<'a>: Copy;
Expand Down Expand Up @@ -54,6 +58,40 @@ tuple_builder!(a, b, c, d, e, f);
tuple_builder!(a, b, c, d, e, f, g);
tuple_builder!(a, b, c, d, e, f, g, h);

/// Trait for the state of a [`Builder`].
pub trait State<Output>: AsAny {
/// Processes a "frame".
///
/// This method can respond to externally triggered events by changing the
/// `Output`.
fn run(&mut self, output: &mut Output);
}

macro_rules! tuple_state {
($($a:ident),*) => {
#[allow(non_camel_case_types)]
impl<$($a,)* O> State<O> for ($($a,)*)
where
$($a: State<O>,)*
{
fn run(&mut self, _output: &mut O) {
let ($($a,)*) = self;
$($a.run(_output);)*
}
}
};
}

tuple_state!();
tuple_state!(a);
tuple_state!(a, b);
tuple_state!(a, b, c);
tuple_state!(a, b, c, d);
tuple_state!(a, b, c, d, e);
tuple_state!(a, b, c, d, e, f);
tuple_state!(a, b, c, d, e, f, g);
tuple_state!(a, b, c, d, e, f, g, h);

/// Context provided by [`with`].
pub struct Cx<'cx, 'state, State, R: CxRep> {
inner: CxInner<'cx, 'state, State, R>,
Expand Down

0 comments on commit 29873b5

Please sign in to comment.