Skip to content

Commit

Permalink
Merge pull request #97 from dtolnay/isize
Browse files Browse the repository at this point in the history
Define rust::isize with Windows support
  • Loading branch information
dtolnay authored Apr 10, 2020
2 parents 4b97272 + 59b5ba1 commit 59b7c12
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions gen/include.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub struct Includes {
pub string: bool,
pub type_traits: bool,
pub utility: bool,
pub base_tsd: bool,
}

impl Includes {
Expand Down Expand Up @@ -88,6 +89,11 @@ impl Display for Includes {
if self.utility {
writeln!(f, "#include <utility>")?;
}
if self.base_tsd {
writeln!(f, "#if defined(_WIN32)")?;
writeln!(f, "#include <BaseTsd.h>")?;
writeln!(f, "#endif")?;
}
if *self != Self::default() {
writeln!(f)?;
}
Expand Down
9 changes: 8 additions & 1 deletion gen/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
let mut needs_rust_str = false;
let mut needs_rust_box = false;
let mut needs_rust_fn = false;
let mut needs_rust_isize = false;
for ty in types {
match ty {
Type::RustBox(_) => {
Expand All @@ -124,6 +125,10 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
Type::Fn(_) => {
needs_rust_fn = true;
}
ty if ty == Isize => {
out.include.base_tsd = true;
needs_rust_isize = true;
}
ty if ty == RustString => {
out.include.array = true;
out.include.cstdint = true;
Expand Down Expand Up @@ -181,6 +186,7 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
|| needs_rust_box
|| needs_rust_fn
|| needs_rust_error
|| needs_rust_isize
|| needs_unsafe_bitcopy
|| needs_manually_drop
|| needs_maybe_uninit
Expand All @@ -199,6 +205,7 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) {
write_header_section(out, needs_rust_box, "CXXBRIDGE02_RUST_BOX");
write_header_section(out, needs_rust_fn, "CXXBRIDGE02_RUST_FN");
write_header_section(out, needs_rust_error, "CXXBRIDGE02_RUST_ERROR");
write_header_section(out, needs_rust_isize, "CXXBRIDGE02_RUST_ISIZE");
write_header_section(out, needs_unsafe_bitcopy, "CXXBRIDGE02_RUST_BITCOPY");

if needs_manually_drop {
Expand Down Expand Up @@ -689,7 +696,7 @@ fn write_type(out: &mut OutFile, ty: &Type) {
Some(I16) => write!(out, "int16_t"),
Some(I32) => write!(out, "int32_t"),
Some(I64) => write!(out, "int64_t"),
Some(Isize) => write!(out, "ssize_t"),
Some(Isize) => write!(out, "::rust::isize"),
Some(F32) => write!(out, "float"),
Some(F64) => write!(out, "double"),
Some(CxxString) => write!(out, "::std::string"),
Expand Down
12 changes: 12 additions & 0 deletions include/cxx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <string>
#include <type_traits>
#include <utility>
#if defined(_WIN32)
#include <BaseTsd.h>
#endif

namespace rust {
inline namespace cxxbridge02 {
Expand Down Expand Up @@ -180,6 +183,15 @@ class Error final : std::exception {
};
#endif // CXXBRIDGE02_RUST_ERROR

#ifndef CXXBRIDGE02_RUST_ISIZE
#define CXXBRIDGE02_RUST_ISIZE
#if defined(_WIN32)
using isize = SSIZE_T;
#else
using isize = ssize_t;
#endif
#endif // CXXBRIDGE02_RUST_ISIZE

std::ostream &operator<<(std::ostream &, const String &);
std::ostream &operator<<(std::ostream &, const Str &);

Expand Down

0 comments on commit 59b7c12

Please sign in to comment.