Skip to content

Commit

Permalink
Rename HasField::Field to HasField::Value (#35)
Browse files Browse the repository at this point in the history
* Rename HasField::Field to HasField::Value

* Add changelog
  • Loading branch information
soareschen authored Dec 7, 2024
1 parent 4771c19 commit 473260b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Pre-Release

- Rename `HasField::Field` to `HasField::Value` - [#35](https://github.com/contextgeneric/cgp/pull/35)

- Remove `Sized` constraint from `Async` trait - [#34](https://github.com/contextgeneric/cgp/pull/34)

- Component pattern improvements - [#24](https://github.com/contextgeneric/cgp/pull/24)
Expand Down
9 changes: 4 additions & 5 deletions crates/cgp-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
pub mod prelude;

pub use cgp_async::{async_trait, Async};
pub use cgp_component as component;
pub use cgp_error as error;
pub use cgp_field as field;
pub use cgp_inner as inner;
pub use cgp_type as types;
pub use {
cgp_component as component, cgp_error as error, cgp_field as field, cgp_inner as inner,
cgp_type as types,
};
18 changes: 8 additions & 10 deletions crates/cgp-field/src/impls/use_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,27 @@ pub type WithField<Tag> = WithProvider<UseField<Tag>>;

impl<Context, TypeTag, FieldTag, Field> ProvideType<Context, TypeTag> for UseField<FieldTag>
where
Context: HasField<FieldTag, Field = Field>,
Context: HasField<FieldTag, Value = Field>,
{
type Type = Field;
}

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

fn get_field(context: &Context, _tag: PhantomData<OutTag>) -> &Self::Field {
fn get_field(context: &Context, _tag: PhantomData<OutTag>) -> &Value {
context.get_field(PhantomData)
}
}

impl<Context, OutTag, Tag, Field> MutFieldGetter<Context, OutTag> for UseField<Tag>
impl<Context, OutTag, Tag, Value> MutFieldGetter<Context, OutTag> for UseField<Tag>
where
Context: HasFieldMut<Tag, Field = Field>,
Context: HasFieldMut<Tag, Value = Value>,
{
type Field = Field;

fn get_field_mut(context: &mut Context, _tag: PhantomData<OutTag>) -> &mut Self::Field {
fn get_field_mut(context: &mut Context, _tag: PhantomData<OutTag>) -> &mut Value {
context.get_field_mut(PhantomData)
}
}
22 changes: 11 additions & 11 deletions crates/cgp-field/src/traits/has_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ use core::ops::Deref;
use cgp_component::UseContext;

pub trait HasField<Tag> {
type Field;
type Value;

fn get_field(&self, tag: PhantomData<Tag>) -> &Self::Field;
fn get_field(&self, tag: PhantomData<Tag>) -> &Self::Value;
}

pub trait FieldGetter<Context, Tag> {
type Field;
type Value;

fn get_field(context: &Context, tag: PhantomData<Tag>) -> &Self::Field;
fn get_field(context: &Context, tag: PhantomData<Tag>) -> &Self::Value;
}

impl<Context, Tag, Target, Field> HasField<Tag> for Context
impl<Context, Tag, Target, Value> HasField<Tag> for Context
where
Context: Deref<Target = Target>,
Target: HasField<Tag, Field = Field> + 'static,
Target: HasField<Tag, Value = Value> + 'static,
{
type Field = Field;
type Value = Value;

fn get_field(&self, tag: PhantomData<Tag>) -> &Self::Field {
fn get_field(&self, tag: PhantomData<Tag>) -> &Self::Value {
self.deref().get_field(tag)
}
}

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

fn get_field(context: &Context, _tag: PhantomData<Tag>) -> &Self::Field {
fn get_field(context: &Context, _tag: PhantomData<Tag>) -> &Self::Value {
context.get_field(PhantomData)
}
}
15 changes: 7 additions & 8 deletions crates/cgp-field/src/traits/has_field_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ use core::marker::PhantomData;
use core::ops::DerefMut;

use crate::traits::has_field::HasField;
use crate::FieldGetter;

pub trait HasFieldMut<Tag>: HasField<Tag> {
fn get_field_mut(&mut self, tag: PhantomData<Tag>) -> &mut Self::Field;
fn get_field_mut(&mut self, tag: PhantomData<Tag>) -> &mut Self::Value;
}

pub trait MutFieldGetter<Context, Tag> {
type Field;

fn get_field_mut(context: &mut Context, tag: PhantomData<Tag>) -> &mut Self::Field;
pub trait MutFieldGetter<Context, Tag>: FieldGetter<Context, Tag> {
fn get_field_mut(context: &mut Context, tag: PhantomData<Tag>) -> &mut Self::Value;
}

impl<Context, Tag, Target, Field> HasFieldMut<Tag> for Context
impl<Context, Tag, Target, Value> HasFieldMut<Tag> for Context
where
Context: DerefMut<Target = Target>,
Target: HasFieldMut<Tag, Field = Field> + 'static,
Target: HasFieldMut<Tag, Value = Value> + 'static,
{
fn get_field_mut(&mut self, tag: PhantomData<Tag>) -> &mut Self::Field {
fn get_field_mut(&mut self, tag: PhantomData<Tag>) -> &mut Self::Value {
self.deref_mut().get_field_mut(tag)
}
}
3 changes: 1 addition & 2 deletions crates/cgp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub use cgp_core as core;
pub use cgp_core::prelude;
pub use cgp_extra as extra;
pub use {cgp_core as core, cgp_extra as extra};

0 comments on commit 473260b

Please sign in to comment.