Giter Site home page Giter Site logo

fint's People

Contributors

ryo-ebata avatar

Stargazers

 avatar  avatar

Watchers

 avatar

fint's Issues

型ごとに適切な変換メソッド(例えば`try_into`)を使用して、安全なキャストを行う

こちらも同様に、asキャストを使用した際にオーバーフローやサイレントなデータ損失が発生する可能性があります。asキャストの代わりに、型ごとに適切な変換メソッド(例えばtry_into)を使用して、安全なキャストを行うようにしてください。また、オーバーフローが発生した場合には、checked_addを使用して適切に処理することを検討してください。

31:                     match self {
32:                         Int::I8(a) => Int::new((a as i128).checked_add(other as i128).expect("Overflow occurred")),
33:                         Int::I16(a) => Int::new((a as i128).checked_add(other as i128).expect("Overflow occurred")),
34:                         Int::I32(a) => Int::new((a as i128).checked_add(other as i128).expect("Overflow occurred")),
35:                         Int::I64(a) => Int::new((a as i128).checked_add(other as i128).expect("Overflow occurred")),
36:                         Int::I128(a) => Int::new(a.checked_add(other as i128).expect("Overflow occurred")),
37:                         Int::U8(a) => Int::new((a as u128).checked_add(other as u128).expect("Overflow occurred")),
38:                         Int::U16(a) => Int::new((a as u


<!-- This is an auto-generated comment by OSS CodeRabbit -->

_Originally posted by @github-actions[bot] in https://github.com/ryo-ebata/optimal_int/pull/9#discussion_r1448904764_
            

異なる型間での乗算をサポート

異なる型間での乗算をサポートするためのエラーハンドリングが不十分です。パニックを引き起こす代わりに、エラーを返すか、結果をOptionまたはResultでラップすることを検討してください。

- _ => panic!("Cannot multiply different types"),
+ _ => return Err("Cannot multiply different types"),

Originally posted by @github-actions[bot] in #9 (comment)

Int::new関数内でオーバーフローチェック

impl Mul for Int {
    type Output = Self;

    fn mul(self, other: Self) -> Self {
        match (self, other) {
            (Int::I8(a), Int::I8(b)) => Int::new(a * b),
            (Int::I16(a), Int::I16(b)) => Int::new(a * b),
            (Int::I32(a), Int::I32(b)) => Int::new(a * b),
            (Int::I64(a), Int::I64(b)) => Int::new(a * b),
            (Int::I128(a), Int::I128(b)) => Int::new(a * b),
            (Int::U8(a), Int::U8(b)) => Int::new(a * b),
            (Int::U16(a), Int::U16(b)) => Int::new(a * b),
            (Int::U32(a), Int::U32(b)) => Int::new(a * b),
            (Int::U64(a), Int::U64(b)) => Int::new(a * b),
            (Int::U128(a), Int::U128(b)) => Int::new(a * b),
            _ => panic!("Cannot multiply different types"),

整数の乗算においてオーバーフローが発生する可能性があります。Int::new関数内でオーバーフローチェックを行うか、またはchecked_mulメソッドを使用して安全な乗算を保証する必要があります。

- Int::new(a * b)
+ Int::new(a.checked_mul(b).expect("Multiplication overflow"))

Originally posted by @github-actions[bot] in #9 (comment)

ゼロ除算の可能性がある場合、`panic!`を使用せず代わりにエラーを返すか、オプション型を使用する

ゼロ除算の可能性がある場合、panic!を使用するのは適切ではありません。代わりにエラーを返すか、オプション型を使用することで、より安全な実装にすることができます。

-            _ => panic!("Cannot divide different types or divide by zero"),
+            _ => return Err("Cannot divide different types or divide by zero"),

この変更により、Divトレイトの実装の戻り値の型もResult<Self, &'static str>に変更する必要があります。

Originally posted by @github-actions[bot] in #9 (comment)

i32, u32以上の数値に関してもサポートしたい

Rustはデフォルトでの型推論がi32, u32になるため、i64などの数値がオーバーフローを起こしてしまう。
現状では

Int::new(4000000000000i64)

と明示的にすると防げるのだが、
それは面倒。

Rustのコンパイラに対してIssueを出す必要あり

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.