Skip to content

Commit a1e0091

Browse files
Allow Ptr::offset() to receive any value that can cast to isize
The rationale behind this change is to avoid unnecessary casts that may hurt readability, such as: `0_usize as isize`. With this change, we are allowed to omit the cast, as it will be performed inside the `offset` function. This also has the advantage of panicking whenever we try to cast a usize larger than `isize::MAX`.
1 parent d82d983 commit a1e0091

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

libcc2rs/src/rc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ impl<T> Ptr<T> {
296296
}
297297

298298
#[inline]
299-
pub fn offset(&self, offset: isize) -> Self {
299+
pub fn offset(&self, offset: impl TryInto<isize>) -> Self {
300+
let offset = match offset.try_into() {
301+
Ok(o) => o,
302+
Err(_) => panic!("the offset must fit in a isize"),
303+
};
300304
let step = self.elem_step();
301305
Self {
302306
kind: self.kind.clone(),

0 commit comments

Comments
 (0)