Refactor of type_cast.rs to be explicit
- Dominant language
- Rust
- Stars
- 5
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
While working on https://github.com/cennznet/cennznut-rs/issues/2, an issue has been identified that the blanket trait `IntoPact<'a, I>` doesn't expose impls correctly to throw a [E0277](https://doc.rust-lang.org/nightly/error-index.html#E0277) error like below:
```rust
388 | cennznut.validate(&module.name, &method.name, &args),
| ^^^^^^ the trait `pact::interpreter::type_cast::IntoPact<'_, u16>` is not implemented for `u16`
```
This is related to the drawbacks mentioned in https://codesandwich.github.io/overlapping_blanket_impls/:
>- Traits and functions start exposing useless generic parameters, they sometimes must be manually filled and dummy traits may show up. Both API and code get littered.
>- API users must understand this pattern or they will have bad time if their code doesn’t compile on the first try
There is no way to verify that trait with blanket impls is correctly parametrized
I think we should go for explicit casting for each type we support like:
```rust
pub trait IntoPact<'a> { // also remove type parameter I to avoid complexity
fn into_pact(self) -> Result, ()>;
}
impl<'a> IntoPact<'a> for u8 {
fn into_pact(self) -> Result, ()> {
Ok(PactType::Numeric(Numeric(self as u64)))
}
}
impl<'a> IntoPact<'a> for u16 {
fn into_pact(self) -> Result, ()> {
Ok(PactType::Numeric(Numeric(self as u64)))
}
}
...
impl<'a> IntoPact<'a> for &'a str {
fn into_pact(self) -> Result, ()> {
Ok(PactType::StringLike(StringLike(self.as_ref())))
}
}
...
```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start in type_cast.rs and trace the IntoPact blanket implementation and its callers, including the validate call shown in the error. Identify every currently supported input type, then assess the explicit per-type implementations proposed in the issue and the removal of the generic I parameter. Done means supported conversions expose useful compiler errors without the blanket-trait API complexity.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Refactor
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100