ryo-ebata / fint Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
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)
異なる型間での乗算をサポートするためのエラーハンドリングが不十分です。パニックを引き起こす代わりに、エラーを返すか、結果をOption
またはResult
でラップすることを検討してください。
- _ => panic!("Cannot multiply different types"),
+ _ => return Err("Cannot multiply different types"),
Originally posted by @github-actions[bot] in #9 (comment)
こちらも同様に、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_
Rustはデフォルトでの型推論がi32, u32になるため、i64などの数値がオーバーフローを起こしてしまう。
現状では
Int::new(4000000000000i64)
と明示的にすると防げるのだが、
それは面倒。
Rustのコンパイラに対してIssueを出す必要あり
ゼロ除算の可能性がある場合、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)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.