Skip to content

Conversation

@cuviper
Copy link
Member

@cuviper cuviper commented Dec 16, 2025

trait PrimitiveNumber {
    // ...

    /// Creates a number using a type cast, `value as Self`.
    fn as_from<T>(value: T) -> Self
    where
        Self: PrimitiveNumberAs<T>;

    /// Returns this number converted with a type cast, `self as T`.
    fn as_to<T>(self) -> T
    where
        Self: PrimitiveNumberAs<T>;
}

The helper PrimitiveNumberAs is also added as a supertrait for all
concrete primitive numbers, so the methods can be used without any
additional bounds if you're converting to/from specific types.

```rust
trait PrimitiveNumber {
    // ...

    /// Creates a number using a type cast, `value as Self`.
    fn as_from<T>(value: T) -> Self
    where
        Self: PrimitiveNumberAs<T>;

    /// Returns this number converted with a type cast, `self as T`.
    fn as_to<T>(self) -> T
    where
        Self: PrimitiveNumberAs<T>;
}
```

The helper `PrimitiveNumberAs` is also added as a supertrait for all
concrete primitive numbers, so the methods can be used without any
additional bounds if you're converting to/from specific types.
@cuviper cuviper linked an issue Dec 16, 2025 that may be closed by this pull request
@cuviper cuviper added this pull request to the merge queue Dec 16, 2025
Merged via the queue into rust-num:main with commit 4a34b7e Dec 16, 2025
9 checks passed
@cuviper cuviper deleted the as branch December 16, 2025 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider exposing as type-casting

1 participant