-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize cgp-core re-exports (#18)
* Reorganize re-exports in cgp-core * Add DelegateErrorRaiser * Reformat imports
- Loading branch information
1 parent
89293a9
commit b76801f
Showing
8 changed files
with
74 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use cgp_async::Async; | ||
use cgp_component::{derive_component, DelegateComponent, HasComponents}; | ||
|
||
use crate::has_error_type::HasErrorType; | ||
|
||
/** | ||
Used for injecting external error types into [`Self::Error`](HasErrorType::Error). | ||
As an example, if `Context: CanRaiseError<ParseIntError>`, then we would be | ||
able to call `Context::raise_error(err)` for an error value | ||
[`err: ParseIntError`](core::num::ParseIntError) and get back | ||
a [`Context::Error`](HasErrorType::Error) value. | ||
*/ | ||
#[derive_component(ErrorRaiserComponent, ErrorRaiser<Context>)] | ||
pub trait CanRaiseError<E>: HasErrorType { | ||
fn raise_error(e: E) -> Self::Error; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use core::marker::PhantomData; | ||
|
||
use cgp_component::DelegateComponent; | ||
|
||
use crate::{ErrorRaiser, HasErrorType}; | ||
|
||
pub struct DelegateErrorRaiser<Components>(pub PhantomData<Components>); | ||
|
||
impl<Context, Error, Components, Delegate> ErrorRaiser<Context, Error> | ||
for DelegateErrorRaiser<Components> | ||
where | ||
Context: HasErrorType, | ||
Components: DelegateComponent<Error, Delegate = Delegate>, | ||
Delegate: ErrorRaiser<Context, Error>, | ||
{ | ||
fn raise_error(e: Error) -> Context::Error { | ||
Delegate::raise_error(e) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use core::fmt::Debug; | ||
|
||
use cgp_async::Async; | ||
use cgp_component::{derive_component, DelegateComponent, HasComponents}; | ||
|
||
/** | ||
This is used for contexts to declare that they have a _unique_ `Self::Error` type. | ||
Although it is possible for each context to declare their own associated | ||
`Error` type, doing so may result in having multiple ambiguous `Self::Error` types, | ||
if there are multiple associated types with the same name in different traits. | ||
As a result, it is better for context traits to include `HasError` as their | ||
parent traits, so that multiple traits can all refer to the same abstract | ||
`Self::Error` type. | ||
*/ | ||
#[derive_component(ErrorTypeComponent, ProvideErrorType<Context>)] | ||
pub trait HasErrorType: Async { | ||
/** | ||
The `Error` associated type is also required to implement [`Debug`]. | ||
This is to allow `Self::Error` to be used in calls like `.unwrap()`, | ||
as well as for simpler error logging. | ||
*/ | ||
type Error: Async + Debug; | ||
} | ||
|
||
pub type ErrorOf<Context> = <Context as HasErrorType>::Error; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,7 @@ | ||
use core::fmt::Debug; | ||
mod can_raise_error; | ||
mod delegate_error; | ||
mod has_error_type; | ||
|
||
use cgp_async::Async; | ||
use cgp_component::{derive_component, DelegateComponent, HasComponents}; | ||
|
||
/** | ||
This is used for contexts to declare that they have a _unique_ `Self::Error` type. | ||
Although it is possible for each context to declare their own associated | ||
`Error` type, doing so may result in having multiple ambiguous `Self::Error` types, | ||
if there are multiple associated types with the same name in different traits. | ||
As a result, it is better for context traits to include `HasError` as their | ||
parent traits, so that multiple traits can all refer to the same abstract | ||
`Self::Error` type. | ||
*/ | ||
#[derive_component(ErrorTypeComponent, ProvideErrorType<Context>)] | ||
pub trait HasErrorType: Async { | ||
/** | ||
The `Error` associated type is also required to implement [`Debug`]. | ||
This is to allow `Self::Error` to be used in calls like `.unwrap()`, | ||
as well as for simpler error logging. | ||
*/ | ||
type Error: Async + Debug; | ||
} | ||
|
||
/** | ||
Used for injecting external error types into [`Self::Error`](HasErrorType::Error). | ||
As an example, if `Context: CanRaiseError<ParseIntError>`, then we would be | ||
able to call `Context::raise_error(err)` for an error value | ||
[`err: ParseIntError`](core::num::ParseIntError) and get back | ||
a [`Context::Error`](HasErrorType::Error) value. | ||
*/ | ||
#[derive_component(ErrorRaiserComponent, ErrorRaiser<Context>)] | ||
pub trait CanRaiseError<E>: HasErrorType { | ||
fn raise_error(e: E) -> Self::Error; | ||
} | ||
pub use can_raise_error::*; | ||
pub use delegate_error::*; | ||
pub use has_error_type::*; |