Translate char as libc::c_char in unsafe#194
Conversation
|
@nunoplopes this is ready for review |
|
What's the advantage of using c_char vs u8? The code seems a lot more verbose and less idiomatic. Refcount should not use c_char. |
u8 is incompatible with We can use It's ok for refcount to use c_char. core::ffi::c_char does not depend on libc and c_char is the correct and portable solution, consider the following code: #include <cassert>
int main() {
char a = -2;
char b = 1;
assert(a + b == -1);
return 0;
}With the u8 translation this fails. With the c_char translation, this is ok. |
|
I disagree. I don't think I don't know why the translation of that example fails, but |
Then for unsafe I will continue with c_char so that I can get rid of IsCharPointerFieldFromLibc and IsCharArrayFieldFromLibc. For refcount we keep u8 instead of c_char because we don't expect to interact with libc.
On a platform where char is signed, in fn main_0() -> i32 {
let a: Value<u8> = Rc::new(RefCell::new((-2_i32 as u8)));
let b: Value<u8> = Rc::new(RefCell::new(1_u8));
assert!(((((*a.borrow()) as i32) + ((*b.borrow()) as i32)) == -1_i32));
return 0;
} |
Translates char as c_char instead of u8 in both unsafe. Refcount is unchanged.
This contains:
c""syntax to define c_char compatible string literals0 as c_char(correct) instead of0_c_char(incorrect)The big advantage of this PR is 4. IsCharPointerFieldFromLibc and IsCharArrayFieldFromLibc were temporary solutions to interact between our u8 and libc's c_char. After this PR, both functions are deleted.