Skip to content

Add trait for signed to unsigned conversion #391

Open
@sffc

Description

@sffc

I would like a trait such as

pub trait ToUnsigned {
    type UnsignedType: Unsigned;

    fn abs_unsigned(&self) -> Self::UnsignedType;
}

// Example for i32
impl ToUnsigned for i32 {
    type UnsignedType = u32;

    fn abs_unsigned(&self) -> Self::UnsignedType {
        self.abs() as Self::UnsignedType
    }
}

// Example for u32
impl ToUnsigned for u32 {
    type UnsignedType = u32;

    fn abs_unsigned(&self) -> Self::UnsignedType {
        *self
    }
}

Ideally, I would like the traits to "bubble" across the UnsignedType. For example, if my function accepts a T: ToUnsigned + Integer, then I would ideally want to be able to call .abs_unsigned() on T and get back something implementing both Unsigned and Integer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions