Skip to content

Commit

Permalink
Introduce cgp-runtime crate (#50)
Browse files Browse the repository at this point in the history
* Add cgp-runtime crate with HasRuntime trait

* Add RuntimeGetter trait

* Add HasAsyncRuntime trait
  • Loading branch information
soareschen authored Jan 5, 2025
1 parent 42f6e77 commit ff8dc01
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 2 deletions.
7 changes: 7 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"crates/cgp-error-eyre",
"crates/cgp-error-std",
"crates/cgp-run",
"crates/cgp-runtime",
"crates/cgp-inner",
]

Expand Down
4 changes: 2 additions & 2 deletions crates/cgp-error/src/traits/has_async_error_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ use cgp_async::Async;

use crate::traits::HasErrorType;

pub trait HasAsyncErrorType: HasErrorType<Error: Async> {}
pub trait HasAsyncErrorType: Async + HasErrorType<Error: Async> {}

impl<Context> HasAsyncErrorType for Context where Context: HasErrorType<Error: Async> {}
impl<Context> HasAsyncErrorType for Context where Context: Async + HasErrorType<Error: Async> {}
15 changes: 15 additions & 0 deletions crates/cgp-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "cgp-runtime"
version = "0.2.0"
edition = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
authors = { workspace = true }
rust-version = { workspace = true }
keywords = { workspace = true }
description = """
Context-generic programming core component traits
"""

[dependencies]
cgp-core = { version = "0.2.0" }
3 changes: 3 additions & 0 deletions crates/cgp-runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod traits;

pub use traits::*;
7 changes: 7 additions & 0 deletions crates/cgp-runtime/src/traits/has_async_runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use cgp_core::Async;

use crate::HasRuntimeType;

pub trait HasAsyncRuntimeType: Async + HasRuntimeType<Runtime: Async> {}

impl<Context> HasAsyncRuntimeType for Context where Context: Async + HasRuntimeType<Runtime: Async> {}
24 changes: 24 additions & 0 deletions crates/cgp-runtime/src/traits/has_runtime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use core::marker::PhantomData;

use cgp_core::component::WithProvider;
use cgp_core::field::FieldGetter;
use cgp_core::prelude::*;

use crate::HasRuntimeType;

#[cgp_component {
provider: RuntimeGetter,
}]
pub trait HasRuntime: HasRuntimeType {
fn runtime(&self) -> &Self::Runtime;
}

impl<Context, Provider, Runtime> RuntimeGetter<Context> for WithProvider<Provider>
where
Context: HasRuntimeType<Runtime = Runtime>,
Provider: FieldGetter<Context, RuntimeGetterComponent, Value = Runtime>,
{
fn runtime(context: &Context) -> &Runtime {
Provider::get_field(context, PhantomData)
}
}
20 changes: 20 additions & 0 deletions crates/cgp-runtime/src/traits/has_runtime_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use cgp_core::component::WithProvider;
use cgp_core::prelude::*;
use cgp_core::types::traits::ProvideType;

#[cgp_component {
name: RuntimeTypeComponent,
provider: ProvideRuntimeType,
}]
pub trait HasRuntimeType {
type Runtime;
}

pub type RuntimeOf<Context> = <Context as HasRuntimeType>::Runtime;

impl<Context, Provider, Runtime> ProvideRuntimeType<Context> for WithProvider<Provider>
where
Provider: ProvideType<Context, RuntimeTypeComponent, Type = Runtime>,
{
type Runtime = Runtime;
}
7 changes: 7 additions & 0 deletions crates/cgp-runtime/src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub mod has_async_runtime;
pub mod has_runtime;
pub mod has_runtime_type;

pub use has_async_runtime::*;
pub use has_runtime::*;
pub use has_runtime_type::*;

0 comments on commit ff8dc01

Please sign in to comment.