Replies: 1 comment
-
Hi @m-haug, thanks for trying out CGP and uncovering the issue! The problem with the For the issue with your code, it doesn't seem to be an issue with CGP, as you also get the same error with the plain Rust trait by removing the use of First of all, constraints in the Secondly, the lifetime errors for associated types arise because Rust don't know whether the associated type contains lifetime that is shorter than I am not sure of the intention of your design for #[cgp_component(RunningBalanceComputer)]
pub trait CanComputeRunningBalance<TS: ?Sized>: HasTransactionType + HasBalanceType {
fn compute_balance<'a>(
&self,
transactions: &'a TS,
) -> impl Iterator<Item = (&'a Self::Transaction, Self::Balance)>
where
Self::Transaction: 'a;
} With this, you can have a context-generic provider that implements the component using #[cgp_new_provider]
impl<Context, TS: ?Sized> RunningBalanceComputer<Context, TS> for RunIntoIterator
where
Context: HasTransactionType + HasBalanceType,
for<'a> &'a TS: IntoIterator<Item = (&'a Context::Transaction, Context::Balance)>,
{
fn compute_balance<'a>(
_context: &Context,
transactions: &'a TS,
) -> impl Iterator<Item = (&'a Context::Transaction, Context::Balance)>
where
Context::Transaction: 'a,
{
transactions.into_iter()
}
} Note that you don't necessarily need a separate trait to get the iterator, as you could also include them as a #[cgp_component(RunningBalanceComputer)]
pub trait CanComputeRunningBalance<TS: ?Sized>: HasBalanceType {
fn compute_balance(&self, transactions: &TS) -> Self::Balance;
}
#[cgp_new_provider]
impl<Context, TS: ?Sized> RunningBalanceComputer<Context, TS> for RunIntoIterator
where
Context: HasTransactionType + HasBalanceType,
for<'a> &'a TS: IntoIterator<Item = &'a Context::Transaction>,
{
fn compute_balance(_context: &Context, transactions: &TS) -> Context::Balance {
for transaction in transactions.into_iter() {
// do something
}
todo!()
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to implement a context-generic version of the
Bank Account Kata
. For this purpose, I am trying to implement a component for computing a running balance for a set of transactions. In trying to keep things generic, I was looking to accept any type that implementsIntoIterator
:When implementing the provider for this trait, however, I ran into an issue because I was trying to use
[Context::Transaction]
as a type parameter, which isn'tSized
. When I tried to loosen the bound (CanComputeRunningBalance<TS: ?Sized>
), the macro keeps failing due toIsProviderFor
not supporting this use case.For now, I've switched to naming a concrete type (
&[Self::Transaction]
) in the trait, which works. I would like to keep this part of the trait generic, however.What's the best way to model this in CGP?
I've tried to find a similar case in
hermes-sdk
, but did come up empty. If someone could point me to a comparable case, I'd appreciate it too.Beta Was this translation helpful? Give feedback.
All reactions