Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a type-level equivalent for div_ceil? #215

Open
newpavlov opened this issue Nov 11, 2024 · 1 comment
Open

Add a type-level equivalent for div_ceil? #215

newpavlov opened this issue Nov 11, 2024 · 1 comment

Comments

@newpavlov
Copy link

newpavlov commented Nov 11, 2024

This can be emulated by calculating (N + M - 1) / M, but it results in bloated where blocks which obscure the original intention.

@kgullion
Copy link

use core::ops::{Add, Div, Sub};
use typenum::op;

pub type DivCeil<X, Y> = <X as DivCeiling<Y>>::Output;
pub trait DivCeiling<Denom> {
    type Output;
}
impl<X: Integer, Y: Integer + NonZero> DivCeiling<Y> for X
where
    X: Add<Y, Output: Sub<U1, Output: Div<Y>>>,
{
    type Output = op!((X + Y - U1) / Y);
}

now you should be able to use just DivCeiling as your bounds:

trait Example<Y> {
    type Output;
}
impl<X: DivCeiling<Y>, Y> Example<Y> for X {
    type Output = DivCeil<X, Y>;
}

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

No branches or pull requests

2 participants