Skip to content

Add c_bool to libc. #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Add c_bool to libc. #125

wants to merge 1 commit into from

Conversation

jimblandy
Copy link
Contributor

This is a fix for #116.

The libc crate is supposed to provide a Rust type corresponding to each C type that might appear in a public API, for use in making foreign calls. However, libc doesn't provide any type designated to correspond to a C bool.

In practice, Rust's bool and the bool of every C ABI I'm aware of are the same, so using the Rust bool directly in foreign function interfaces will work fine on these systems. But the libc crate's principle is to provide types that match by definition, which isn't true of Rust bool, so libc::c_bool is still valuable.

I considered whether it was better to let c_bool be u8 or bool. In both C and C++, converting a value to a bool forces it to be either zero or one. Using Rust bool for c_bool mimics this behavior, whereas u8 would allow Rust code to pass other values. I think forcing a C bool to a value other than zero or one is undefined behavior, but I can't find the language to that effect right now. Either way, it seems like Rust's bool is the better match.

This is for issue 116.

The `libc` crate is supposed to provide a Rust type corresponding to each C type that might appear in a public API, for use in making foreign calls. However, `libc` doesn't provide any type designated to correspond to a C `bool`.

In practice, Rust's `bool` and the `bool` of every C ABI I'm aware of are the same, so using the Rust `bool` directly in foreign function interfaces will work fine on these systems. But the `libc` crate's principle is to provide types that match *by definition*, which isn't true of Rust `bool`, so `libc::c_bool` is still valuable.

I considered whether it was better to let `c_bool` be `u8` or `bool`. In both C and C++, converting a value to a `bool` forces it to be either zero or one. Using Rust `bool` for `c_bool` mimics this behavior, whereas `u8` would allow Rust code to pass other values. I *think* forcing a C `bool` to a value other than zero or one is undefined behavior, but I can't find the language to that effect right now. Either way, it seems like Rust's `bool` is the better match.
@jimblandy
Copy link
Contributor Author

I forgot to #include <stdbool.h>. But it seems like libc-test has a bunch of problems on my system, so I'm going to try to address those first...

@jimblandy
Copy link
Contributor Author

I revised this change on a hand-made branch, not named 'patch-1'. It doesn't seem like GitHub lets you change the branch to which a given pull request is bound, so I'm closing this pull request and opening a new one.

@jimblandy jimblandy closed this Jan 3, 2016
@jimblandy jimblandy mentioned this pull request Jan 3, 2016
@steveklabnik
Copy link
Member

Yeah Github wants you to force push to a branch to update a PR; basically a PR is always for a specific branch.

On Jan 2, 2016, 19:45 -0500, Jim Blandynotifications@github.com, wrote:

I revised this change on a hand-made branch, not named 'patch-1'. It doesn't seem like GitHub lets you change the branch to which a given pull request is bound, so I'm closing this pull request and opening a new one.


Reply to this email directly orview it on GitHub(#125 (comment)).

@jimblandy
Copy link
Contributor Author

Opened new pull request #128.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants