Skip to content

Commit

Permalink
fix!: fix Data impl for &T and add docs to use_local_task
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Dec 9, 2024
1 parent 21c016f commit 63d89ed
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ unsafe impl<T: Data> Data for Vec<T> {}

unsafe impl<T: Data, U: Data, S: 'static> Data for HashMap<T, U, S> {}

unsafe impl<T: Data> Data for &T {}
unsafe impl<T: 'static> Data for &T {}

unsafe impl<T: Data> Data for Option<T> {}

Expand Down
46 changes: 46 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,52 @@ pub fn use_drop<'a>(cx: ScopeState<'a>, f: impl FnOnce() + 'a) {
/// Use a local task that runs on the current thread.
///
/// This will run on the window event loop, polling the task until it completes.
///
/// # Examples
///
/// Sending child state to parents.
///
/// ```
/// use actuate::prelude::*;
/// use tokio::sync::mpsc;
/// use std::cell::Cell;
///
/// #[derive(Data)]
/// struct Child<'a> {
/// idx: usize,
/// tx: &'a mpsc::UnboundedSender<usize>,
/// }
///
/// impl Compose for Child<'_> {
/// fn compose(cx: Scope<Self>) -> impl Compose {
/// cx.me().tx.send(cx.me().idx).unwrap();
/// }
/// }
///
/// #[derive(Data)]
/// struct App;
///
/// impl Compose for App {
/// fn compose(cx: Scope<Self>) -> impl Compose {
/// let (tx, ref rx_cell) = use_ref(&cx, || {
/// let (tx, rx) = mpsc::unbounded_channel();
/// (tx, Cell::new(Some(rx)))
/// });
///
/// use_local_task(&cx, move || async move {
/// let mut rx = rx_cell.take().unwrap();
/// while let Some(id) = rx.recv().await {
/// dbg!("Composed: {}", id);
/// }
/// });
///
/// (
/// Child { idx: 0, tx },
/// Child { idx: 1, tx }
/// )
/// }
/// }
/// ```
pub fn use_local_task<'a, F>(cx: ScopeState<'a>, make_task: impl FnOnce() -> F)
where
F: Future<Output = ()> + 'a,
Expand Down

0 comments on commit 63d89ed

Please sign in to comment.