Skip to content

Commit

Permalink
refactor: seal hooks, update docs, rm deprecated fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ccbrown committed Sep 30, 2024
1 parent 5879712 commit 45e202d
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 20 deletions.
5 changes: 5 additions & 0 deletions packages/iocraft/src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
//!
//! They must be called in the same order every time, so calling them in any sort of conditional or
//! loop is not allowed. If you break the rules of hooks, you can expect a panic.
//!
//! # Note to Library Authors
//!
//! If you are writing a library that provides hooks, it's recommended that you seal your hook
//! traits so you can add new methods without breaking semver compatibility.
mod use_async_handler;
pub use use_async_handler::*;
Expand Down
7 changes: 6 additions & 1 deletion packages/iocraft/src/hooks/use_async_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ use std::{
task::{Context, Poll, Waker},
};

mod private {
pub trait Sealed {}
impl Sealed for crate::Hooks<'_, '_> {}
}

/// `UseFuture` is a hook that allows you to create a [`Handler`] which executes an asynchronous
/// task that is bound to the lifetime of the component.
///
/// If the component is dropped, all executing tasks will also be dropped.
pub trait UseAsyncHandler {
pub trait UseAsyncHandler: private::Sealed {
/// Returns a [`Handler`] which when invoked will execute the given function and drive the
/// resulting future to completion.
fn use_async_handler<T, Fun, Fut>(&mut self, f: Fun) -> Handler<'static, T>
Expand Down
7 changes: 6 additions & 1 deletion packages/iocraft/src/hooks/use_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ use std::{
cell::{Ref, RefMut},
};

mod private {
pub trait Sealed {}
impl Sealed for crate::Hooks<'_, '_> {}
}

/// `UseContext` provides methods for accessing context from a component.
///
/// With the exception of [`SystemContext`](crate::SystemContext), which is always available,
Expand All @@ -29,7 +34,7 @@ use std::{
/// }
/// }
/// ```
pub trait UseContext<'a> {
pub trait UseContext<'a>: private::Sealed {
/// Returns a reference to the context of the given type.
///
/// # Panics
Expand Down
7 changes: 6 additions & 1 deletion packages/iocraft/src/hooks/use_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use std::{
task::{Context, Poll},
};

mod private {
pub trait Sealed {}
impl Sealed for crate::Hooks<'_, '_> {}
}

/// `UseFuture` is a hook that allows you to spawn an async task which is bound to the lifetime of
/// the component.
///
Expand All @@ -30,7 +35,7 @@ use std::{
/// }
/// }
/// ```
pub trait UseFuture {
pub trait UseFuture: private::Sealed {
/// Spawns a future which is bound to the lifetime of the component. When the component is
/// dropped, the future will also be dropped.
///
Expand Down
7 changes: 6 additions & 1 deletion packages/iocraft/src/hooks/use_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ use std::{
task::{Context, Poll, Waker},
};

mod private {
pub trait Sealed {}
impl Sealed for crate::Hooks<'_, '_> {}
}

/// `UseOutput` is a hook that allows you to write to stdout and stderr from a component. The
/// output will be appended to stdout or stderr, above the rendered component output.
///
Expand Down Expand Up @@ -32,7 +37,7 @@ use std::{
/// }
/// }
/// ```
pub trait UseOutput {
pub trait UseOutput: private::Sealed {
/// Gets handles which can be used to write to stdout and stderr.
fn use_output(&mut self) -> (StdoutHandle, StderrHandle);
}
Expand Down
7 changes: 6 additions & 1 deletion packages/iocraft/src/hooks/use_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ use std::{
task::{Context, Poll, Waker},
};

mod private {
pub trait Sealed {}
impl Sealed for crate::Hooks<'_, '_> {}
}

/// `UseState` is a hook that allows you to store state in a component.
///
/// When the state changes, the component will be re-rendered.
Expand All @@ -33,7 +38,7 @@ use std::{
/// }
/// }
/// ```
pub trait UseState {
pub trait UseState: private::Sealed {
/// Creates a new state with its initial value computed by the given function.
///
/// When the state changes, the component will be re-rendered.
Expand Down
7 changes: 6 additions & 1 deletion packages/iocraft/src/hooks/use_terminal_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ use std::{
};
use taffy::{Point, Size};

mod private {
pub trait Sealed {}
impl Sealed for crate::Hooks<'_, '_> {}
}

/// `UseTerminalEvents` is a hook that allows you to listen for user input such as key strokes.
///
/// # Example
Expand Down Expand Up @@ -83,7 +88,7 @@ use taffy::{Point, Size};
/// }
/// }
/// ```
pub trait UseTerminalEvents {
pub trait UseTerminalEvents: private::Sealed {
/// Defines a callback to be invoked whenever a terminal event occurs.
///
/// This hook will be called for all terminal events, including those that occur outside of the
Expand Down
7 changes: 6 additions & 1 deletion packages/iocraft/src/hooks/use_terminal_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ use crate::{
};
use crossterm::terminal;

mod private {
pub trait Sealed {}
impl Sealed for crate::Hooks<'_, '_> {}
}

/// `UseTerminalSize` is a hook that returns the current terminal size.
pub trait UseTerminalSize {
pub trait UseTerminalSize: private::Sealed {
/// Returns the current terminal size as a tuple of `(width, height)`.
fn use_terminal_size(&mut self) -> (u16, u16);
}
Expand Down
15 changes: 2 additions & 13 deletions packages/iocraft/src/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use futures::{
};
use std::{
collections::VecDeque,
io::{self, stdout, IsTerminal, Write},
io::{self, stdout, Write},
mem,
pin::Pin,
sync::{Arc, Mutex, Weak},
Expand All @@ -22,6 +22,7 @@ use std::{
pub use crossterm::event::{KeyCode, KeyEventKind, KeyEventState, KeyModifiers, MouseEventKind};

/// An event fired when a key is pressed.
#[non_exhaustive]
#[derive(Clone, Debug)]
pub struct KeyEvent {
/// A code indicating the key that was pressed.
Expand Down Expand Up @@ -429,12 +430,6 @@ impl Write for Terminal {
}
}

/// Returns whether the standard output is a TTY terminal.
#[deprecated(note = "Users should use the `std::io::IsTerminal` trait instead.")]
pub fn stdout_is_tty() -> bool {
stdout().is_terminal()
}

#[cfg(test)]
mod tests {
use crate::prelude::*;
Expand All @@ -450,10 +445,4 @@ mod tests {
let canvas = Canvas::new(10, 1);
terminal.write_canvas(&canvas).unwrap();
}

#[test]
fn test_stdout_is_tty() {
#[allow(deprecated)]
let _ = stdout_is_tty();
}
}

0 comments on commit 45e202d

Please sign in to comment.