Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for tokio #353

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ When running `cargo test`, the TypeScript bindings will be exported to the file
| heapless-impl | Implement `TS` for types from *heapless* |
| semver-impl | Implement `TS` for types from *semver* |
| smol_str-impl | Implement `TS` for types from *smol_str* |
| tokio-impl | Implement `TS` for types from *tokio* |

<br/>

Expand Down
6 changes: 4 additions & 2 deletions ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ smol_str-impl = ["smol_str"]
serde-json-impl = ["serde_json"]
no-serde-warnings = ["ts-rs-macros/no-serde-warnings"]
import-esm = []
tokio-impl = ["tokio"]

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
chrono = { version = "0.4", features = ["serde"] }
tokio = { version = "1.40", features = ["sync", "rt"] }

[dependencies]
heapless = { version = ">= 0.7, < 0.9", optional = true }
Expand All @@ -54,10 +56,10 @@ bson = { version = "2", optional = true }
bytes = { version = "1", optional = true }
url = { version = "2", optional = true }
semver = { version = "1", optional = true }
smol_str = { version = "0.3", optional = true }
smol_str = { version = "0.3", optional = true }
thiserror = "1"
indexmap = { version = "2", optional = true }
ordered-float = { version = ">= 3, < 5", optional = true }
serde_json = { version = "1", optional = true }
lazy_static = { version = "1", default-features = false }

tokio = { version = "1", features = ["sync"], optional = true }
6 changes: 6 additions & 0 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
//! | heapless-impl | Implement `TS` for types from *heapless* |
//! | semver-impl | Implement `TS` for types from *semver* |
//! | smol_str-impl | Implement `TS` for types from *smol_str* |
//! | tokio-impl | Implement `TS` for types from *tokio* |
//!
//! <br/>
//!
Expand Down Expand Up @@ -138,6 +139,8 @@ mod chrono;
mod export;
#[cfg(feature = "serde-json-impl")]
mod serde_json;
#[cfg(feature = "tokio-impl")]
mod tokio;

/// A type which can be represented in TypeScript.
/// Most of the time, you'd want to derive this trait instead of implementing it manually.
Expand Down Expand Up @@ -987,6 +990,7 @@ impl_wrapper!(impl<'a, T: TS + ToOwned + ?Sized> TS for std::borrow::Cow<'a, T>)
impl_wrapper!(impl<T: TS> TS for std::cell::Cell<T>);
impl_wrapper!(impl<T: TS> TS for std::cell::RefCell<T>);
impl_wrapper!(impl<T: TS> TS for std::sync::Mutex<T>);
impl_wrapper!(impl<T: TS> TS for std::sync::RwLock<T>);
impl_wrapper!(impl<T: TS + ?Sized> TS for std::sync::Weak<T>);
impl_wrapper!(impl<T: TS> TS for std::marker::PhantomData<T>);

Expand Down Expand Up @@ -1053,6 +1057,8 @@ impl_primitives! {
pub(crate) use impl_primitives;
#[rustfmt::skip]
pub(crate) use impl_shadow;
#[rustfmt::skip]
pub(crate) use impl_wrapper;

#[doc(hidden)]
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)]
Expand Down
7 changes: 7 additions & 0 deletions ts-rs/src/tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use tokio::sync::{Mutex, OnceCell, RwLock};

use super::{impl_wrapper, TypeVisitor, TS};

impl_wrapper!(impl<T: TS> TS for Mutex<T>);
impl_wrapper!(impl<T: TS> TS for OnceCell<T>);
impl_wrapper!(impl<T: TS> TS for RwLock<T>);
1 change: 1 addition & 0 deletions ts-rs/tests/integration/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mod skip;
mod slices;
mod struct_rename;
mod struct_tag;
mod tokio;
mod top_level_type_as;
mod top_level_type_override;
mod tuple;
Expand Down
21 changes: 21 additions & 0 deletions ts-rs/tests/integration/tokio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![cfg(feature = "tokio-impl")]

use tokio::sync::{Mutex, OnceCell, RwLock};
use ts_rs::TS;

#[derive(TS)]
#[ts(export, export_to = "tokio/")]
#[ts(concrete(T = i32))]
struct Tokio<T: 'static> {
mutex: Mutex<T>,
once_cell: OnceCell<T>,
rw_lock: RwLock<T>,
}

#[test]
fn tokio() {
assert_eq!(
Tokio::<String>::decl(),
"type Tokio = { mutex: number, once_cell: number, rw_lock: number, };"
)
}