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

Component pattern improvements #24

Merged
merged 4 commits into from
Oct 21, 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
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

## Pre-Release

- Introduce `cgp-type` crate with various refactoring [#23](https://github.com/contextgeneric/cgp/pull/23)
- Component pattern improvements - [#24](https://github.com/contextgeneric/cgp/pull/24)
- Rename `DelegateTo` to `UseDelegate`.
- Implement `FieldGetter` for `UseContext`.
- Introduce `UseDelegatedType`.

- Introduce `cgp-type` crate with various refactoring - [#23](https://github.com/contextgeneric/cgp/pull/23)
- Introduce `cgp-type` crate, with the `HasType` component.
- Introduce `FieldGetter` as a manual provider trait for `HasField`.
- Introduce `HasFieldMut` trait to `cgp-field`, and auto derive it in `#[derive(HasField)]`.
Expand Down
2 changes: 1 addition & 1 deletion crates/cgp-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ pub mod types;

pub use cgp_component_macro::{define_components, delegate_components, derive_component};
pub use traits::{DelegateComponent, HasComponents};
pub use types::{DelegateTo, UseContext, WithContext, WithProvider};
pub use types::{UseContext, UseDelegate, WithContext, WithProvider};
3 changes: 0 additions & 3 deletions crates/cgp-component/src/types/delegate_to.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/cgp-component/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod delegate_to;
pub mod use_context;
pub mod use_delegate;
pub mod with_provider;

pub use delegate_to::DelegateTo;
pub use use_context::{UseContext, WithContext};
pub use use_delegate::UseDelegate;
pub use with_provider::WithProvider;
3 changes: 3 additions & 0 deletions crates/cgp-component/src/types/use_delegate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use core::marker::PhantomData;

pub struct UseDelegate<Components>(pub PhantomData<Components>);
4 changes: 2 additions & 2 deletions crates/cgp-error/src/can_raise_error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cgp_component::{derive_component, DelegateComponent, DelegateTo, HasComponents};
use cgp_component::{derive_component, DelegateComponent, HasComponents, UseDelegate};

use crate::has_error_type::HasErrorType;

Expand All @@ -15,7 +15,7 @@ pub trait CanRaiseError<E>: HasErrorType {
fn raise_error(e: E) -> Self::Error;
}

impl<Context, Error, Components, Delegate> ErrorRaiser<Context, Error> for DelegateTo<Components>
impl<Context, Error, Components, Delegate> ErrorRaiser<Context, Error> for UseDelegate<Components>
where
Context: HasErrorType,
Components: DelegateComponent<Error, Delegate = Delegate>,
Expand Down
13 changes: 13 additions & 0 deletions crates/cgp-field/src/traits/has_field.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use core::marker::PhantomData;
use core::ops::Deref;

use cgp_component::UseContext;

pub trait HasField<Tag> {
type Field;

Expand All @@ -24,3 +26,14 @@ where
self.deref().get_field(tag)
}
}

impl<Context, Tag, Field> FieldGetter<Context, Tag> for UseContext
where
Context: HasField<Tag, Field = Field>,
{
type Field = Field;

fn get_field(context: &Context, _tag: PhantomData<Tag>) -> &Self::Field {
context.get_field(PhantomData)
}
}
2 changes: 2 additions & 0 deletions crates/cgp-type/src/impls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod use_delegated_type;
pub mod use_type;

pub use use_delegated_type::*;
pub use use_type::*;
16 changes: 16 additions & 0 deletions crates/cgp-type/src/impls/use_delegated_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use core::marker::PhantomData;

use cgp_component::{DelegateComponent, WithProvider};

use crate::traits::ProvideType;

pub struct UseDelegatedType<Components>(pub PhantomData<Components>);

pub type WithDelegatedType<Components> = WithProvider<UseDelegatedType<Components>>;

impl<Context, Tag, Components, Type> ProvideType<Context, Tag> for UseDelegatedType<Components>
where
Components: DelegateComponent<Tag, Delegate = Type>,
{
type Type = Type;
}
4 changes: 2 additions & 2 deletions crates/cgp-type/src/traits/has_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cgp_component::{derive_component, DelegateComponent, DelegateTo, HasComponents, UseContext};
use cgp_component::{derive_component, DelegateComponent, HasComponents, UseContext, UseDelegate};

#[derive_component(TypeComponent, ProvideType<Context>)]
pub trait HasType<Tag> {
Expand All @@ -12,7 +12,7 @@ where
type Type = Context::Type;
}

impl<Context, Tag, Components, Type> ProvideType<Context, Tag> for DelegateTo<Components>
impl<Context, Tag, Components, Type> ProvideType<Context, Tag> for UseDelegate<Components>
where
Components: DelegateComponent<Tag>,
Components::Delegate: ProvideType<Context, Tag, Type = Type>,
Expand Down
Loading