FuelLabs / FuelLabs/sway

Implement `std::ops` traits to return arbitrary output defined as associated type

Open
#7,482 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
61.4k
Forks
5.4k
Avg merge
3h 33m
Merged PRs (30d)
4

Description

When `std::ops` traits were originally defined, we didn't support associated types. Therefore, all traits, like `Add` or `Sub` were defined as, e.g.:

```sway
pub trait Add {
fn add(self, rhs: Self) -> Self;
}
```

This restricts us in API flexibility. E.g., in `std::time::Time`, adding `Time` and `Duration` requires having `add` on `impl Time`.

The proposal is to migrate to Rust-like traits that have the result type specified as associated type. E.g.:

```sway
pub trait Add {
type Output = Self;

fn add(self, rhs: Rhs) -> Self::Output;
}
```

Before implementing this, we need support for default generic parameters and default associated types, as discussed in https://github.com/FuelLabs/sway/pull/7532#issuecomment-3880005812.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the std::ops trait definitions and the Rust-like design described in the issue, then read the default-generic and default-associated-type discussion in PR #7532. Done means those defaults are supported and the std::ops traits can define an associated Output and accept a distinct RHS, enabling operations such as Time plus Duration.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.