Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate distance between two raw pointers

Tags:

rust

ffi

Some C interfaces return pointer to end of buffer. So then I need to convert the range to a slice. But slice can only be created from pointer and count. So how do I get the count.

Writing end - start simply gives me error: binary operation `-` cannot be applied to type `*mut i8` and std::ptr::PtrExt only has offset method to calculate end from the offset, but not the inverse operation.

like image 795
Jan Hudec Avatar asked Mar 06 '15 05:03

Jan Hudec


1 Answers

A raw pointer can be cast to usize; you can then perform subtraction on those.

end as usize - start as usize
like image 153
Chris Morgan Avatar answered Sep 23 '22 16:09

Chris Morgan