From b8a6fb27559bf27c80e0a9f1ab899f8545002d8b Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 10 Apr 2020 11:17:28 -0700 Subject: [PATCH 1/2] Define rust::isize with Windows support --- gen/write.rs | 8 +++++++- include/cxx.h | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/gen/write.rs b/gen/write.rs index 217a59104..9d6ef6e45 100644 --- a/gen/write.rs +++ b/gen/write.rs @@ -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(_) => { @@ -124,6 +125,9 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) { Type::Fn(_) => { needs_rust_fn = true; } + ty if ty == Isize => { + needs_rust_isize = true; + } ty if ty == RustString => { out.include.array = true; out.include.cstdint = true; @@ -181,6 +185,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 @@ -199,6 +204,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 { @@ -689,7 +695,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"), diff --git a/include/cxx.h b/include/cxx.h index 5de479c4f..6fe55d04c 100644 --- a/include/cxx.h +++ b/include/cxx.h @@ -180,6 +180,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 &); From 59b5ba1f1433a89865f52e2cbaa231a0f7ad1940 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 10 Apr 2020 11:32:19 -0700 Subject: [PATCH 2/2] Include BaseTsd.h to get SSIZE_T --- gen/include.rs | 6 ++++++ gen/write.rs | 1 + include/cxx.h | 3 +++ 3 files changed, 10 insertions(+) diff --git a/gen/include.rs b/gen/include.rs index e3a9dd790..8f38fe385 100644 --- a/gen/include.rs +++ b/gen/include.rs @@ -38,6 +38,7 @@ pub struct Includes { pub string: bool, pub type_traits: bool, pub utility: bool, + pub base_tsd: bool, } impl Includes { @@ -88,6 +89,11 @@ impl Display for Includes { if self.utility { writeln!(f, "#include ")?; } + if self.base_tsd { + writeln!(f, "#if defined(_WIN32)")?; + writeln!(f, "#include ")?; + writeln!(f, "#endif")?; + } if *self != Self::default() { writeln!(f)?; } diff --git a/gen/write.rs b/gen/write.rs index 9d6ef6e45..d80e95795 100644 --- a/gen/write.rs +++ b/gen/write.rs @@ -126,6 +126,7 @@ fn write_include_cxxbridge(out: &mut OutFile, apis: &[Api], types: &Types) { needs_rust_fn = true; } ty if ty == Isize => { + out.include.base_tsd = true; needs_rust_isize = true; } ty if ty == RustString => { diff --git a/include/cxx.h b/include/cxx.h index 6fe55d04c..27034f09f 100644 --- a/include/cxx.h +++ b/include/cxx.h @@ -7,6 +7,9 @@ #include #include #include +#if defined(_WIN32) +#include +#endif namespace rust { inline namespace cxxbridge02 {