mirror of
https://github.com/topjohnwu/cxx.git
synced 2025-02-24 01:52:27 +00:00
commit
e3bd6abf48
@ -298,10 +298,10 @@ of functions.
|
||||
|
||||
<table>
|
||||
<tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr>
|
||||
<tr><td>String</td><td>cxxbridge::RustString</td><td></td></tr>
|
||||
<tr><td>&str</td><td>cxxbridge::RustStr</td><td></td></tr>
|
||||
<tr><td>String</td><td>rust::String</td><td></td></tr>
|
||||
<tr><td>&str</td><td>rust::Str</td><td></td></tr>
|
||||
<tr><td><a href="https://docs.rs/cxx/0.1/cxx/struct.CxxString.html">CxxString</a></td><td>std::string</td><td><sup><i>cannot be passed by value</i></sup></td></tr>
|
||||
<tr><td>Box<T></td><td>cxxbridge::RustBox<T></td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
|
||||
<tr><td>Box<T></td><td>rust::Box<T></td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
|
||||
<tr><td><a href="https://docs.rs/cxx/0.1/cxx/struct.UniquePtr.html">UniquePtr<T></a></td><td>std::unique_ptr<T></td><td><sup><i>cannot hold opaque Rust type</i></sup></td></tr>
|
||||
<tr><td></td><td></td><td></td></tr>
|
||||
</table>
|
||||
|
@ -3,13 +3,13 @@
|
||||
#include <iostream>
|
||||
|
||||
namespace org {
|
||||
namespace rust {
|
||||
namespace example {
|
||||
|
||||
ThingC::ThingC(std::string appname) : appname(std::move(appname)) {}
|
||||
|
||||
ThingC::~ThingC() { std::cout << "done with ThingC" << std::endl; }
|
||||
|
||||
std::unique_ptr<ThingC> make_demo(cxxbridge::RustStr appname) {
|
||||
std::unique_ptr<ThingC> make_demo(rust::Str appname) {
|
||||
return std::unique_ptr<ThingC>(new ThingC(appname));
|
||||
}
|
||||
|
||||
@ -17,5 +17,5 @@ const std::string &get_name(const ThingC &thing) { return thing.appname; }
|
||||
|
||||
void do_thing(SharedThing state) { print_r(*state.y); }
|
||||
|
||||
} // namespace rust
|
||||
} // namespace example
|
||||
} // namespace org
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
|
||||
namespace org {
|
||||
namespace rust {
|
||||
namespace example {
|
||||
|
||||
class ThingC {
|
||||
public:
|
||||
@ -16,9 +16,9 @@ public:
|
||||
|
||||
struct SharedThing;
|
||||
|
||||
std::unique_ptr<ThingC> make_demo(cxxbridge::RustStr appname);
|
||||
std::unique_ptr<ThingC> make_demo(rust::Str appname);
|
||||
const std::string &get_name(const ThingC &thing);
|
||||
void do_thing(SharedThing state);
|
||||
|
||||
} // namespace rust
|
||||
} // namespace example
|
||||
} // namespace org
|
||||
|
@ -1,4 +1,4 @@
|
||||
#[cxx::bridge(namespace = org::rust)]
|
||||
#[cxx::bridge(namespace = org::example)]
|
||||
mod ffi {
|
||||
struct SharedThing {
|
||||
z: i32,
|
||||
|
66
gen/write.rs
66
gen/write.rs
@ -21,21 +21,11 @@ pub(super) fn gen(namespace: Vec<String>, apis: &[Api], types: &Types, header: b
|
||||
write_includes(out, types);
|
||||
write_include_cxxbridge(out, types);
|
||||
|
||||
if !header {
|
||||
out.next_section();
|
||||
write_namespace_alias(out, types);
|
||||
}
|
||||
|
||||
out.next_section();
|
||||
for name in &namespace {
|
||||
writeln!(out, "namespace {} {{", name);
|
||||
}
|
||||
|
||||
if header {
|
||||
out.next_section();
|
||||
write_namespace_alias(out, types);
|
||||
}
|
||||
|
||||
out.next_section();
|
||||
for api in apis {
|
||||
match api {
|
||||
@ -125,7 +115,8 @@ fn write_include_cxxbridge(out: &mut OutFile, types: &Types) {
|
||||
}
|
||||
}
|
||||
|
||||
out.begin_block("namespace cxxbridge01");
|
||||
out.begin_block("namespace rust");
|
||||
out.begin_block("inline namespace cxxbridge01");
|
||||
if needs_rust_box {
|
||||
writeln!(out, "// #include \"cxxbridge.h\"");
|
||||
for line in include::get("CXXBRIDGE01_RUST_BOX").lines() {
|
||||
@ -135,20 +126,7 @@ fn write_include_cxxbridge(out: &mut OutFile, types: &Types) {
|
||||
}
|
||||
}
|
||||
out.end_block();
|
||||
}
|
||||
|
||||
fn write_namespace_alias(out: &mut OutFile, types: &Types) {
|
||||
let mut needs_namespace_alias = false;
|
||||
for ty in types {
|
||||
if let Type::RustBox(_) = ty {
|
||||
needs_namespace_alias = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if needs_namespace_alias {
|
||||
writeln!(out, "namespace cxxbridge = cxxbridge01;");
|
||||
}
|
||||
out.end_block();
|
||||
}
|
||||
|
||||
fn write_struct(out: &mut OutFile, strct: &Struct) {
|
||||
@ -365,7 +343,7 @@ fn write_extern_return_type(out: &mut OutFile, ty: &Option<Type>, types: &Types)
|
||||
write_type(out, &ty.inner);
|
||||
write!(out, " *");
|
||||
}
|
||||
Some(Type::Str(_)) => write!(out, "::cxxbridge::RustStr::Repr "),
|
||||
Some(Type::Str(_)) => write!(out, "::rust::Str::Repr "),
|
||||
Some(ty) if types.needs_indirect_abi(ty) => write!(out, "void "),
|
||||
_ => write_return_type(out, ty),
|
||||
}
|
||||
@ -377,7 +355,7 @@ fn write_extern_arg(out: &mut OutFile, arg: &Var, types: &Types) {
|
||||
write_type_space(out, &ty.inner);
|
||||
write!(out, "*");
|
||||
}
|
||||
Type::Str(_) => write!(out, "::cxxbridge::RustStr::Repr "),
|
||||
Type::Str(_) => write!(out, "::rust::Str::Repr "),
|
||||
_ => write_type_space(out, &arg.ty),
|
||||
}
|
||||
if types.needs_indirect_abi(&arg.ty) {
|
||||
@ -401,11 +379,11 @@ fn write_type(out: &mut OutFile, ty: &Type) {
|
||||
Some(I64) => write!(out, "int64_t"),
|
||||
Some(Isize) => write!(out, "ssize_t"),
|
||||
Some(CxxString) => write!(out, "::std::string"),
|
||||
Some(RustString) => write!(out, "::cxxbridge::RustString"),
|
||||
Some(RustString) => write!(out, "::rust::String"),
|
||||
None => write!(out, "{}", ident),
|
||||
},
|
||||
Type::RustBox(ty) => {
|
||||
write!(out, "::cxxbridge::RustBox<");
|
||||
write!(out, "::rust::Box<");
|
||||
write_type(out, &ty.inner);
|
||||
write!(out, ">");
|
||||
}
|
||||
@ -422,7 +400,7 @@ fn write_type(out: &mut OutFile, ty: &Type) {
|
||||
write!(out, " &");
|
||||
}
|
||||
Type::Str(_) => {
|
||||
write!(out, "::cxxbridge::RustStr");
|
||||
write!(out, "::rust::Str");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -458,7 +436,8 @@ fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
|
||||
}
|
||||
out.end_block();
|
||||
|
||||
out.begin_block("namespace cxxbridge01");
|
||||
out.begin_block("namespace rust");
|
||||
out.begin_block("inline namespace cxxbridge01");
|
||||
for ty in types {
|
||||
if let Type::RustBox(ty) = ty {
|
||||
if let Type::Ident(inner) = &ty.inner {
|
||||
@ -467,6 +446,7 @@ fn write_generic_instantiations(out: &mut OutFile, types: &Types) {
|
||||
}
|
||||
}
|
||||
out.end_block();
|
||||
out.end_block();
|
||||
}
|
||||
|
||||
fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
|
||||
@ -482,27 +462,27 @@ fn write_rust_box_extern(out: &mut OutFile, ident: &Ident) {
|
||||
writeln!(out, "#define CXXBRIDGE01_RUST_BOX_{}", instance);
|
||||
writeln!(
|
||||
out,
|
||||
"void cxxbridge01$rust_box${}$uninit(::cxxbridge::RustBox<{}> *ptr) noexcept;",
|
||||
"void cxxbridge01$rust_box${}$uninit(::rust::Box<{}> *ptr) noexcept;",
|
||||
instance, inner,
|
||||
);
|
||||
writeln!(
|
||||
out,
|
||||
"void cxxbridge01$rust_box${}$set_raw(::cxxbridge::RustBox<{}> *ptr, {} *raw) noexcept;",
|
||||
"void cxxbridge01$rust_box${}$set_raw(::rust::Box<{}> *ptr, {} *raw) noexcept;",
|
||||
instance, inner, inner
|
||||
);
|
||||
writeln!(
|
||||
out,
|
||||
"void cxxbridge01$rust_box${}$drop(::cxxbridge::RustBox<{}> *ptr) noexcept;",
|
||||
"void cxxbridge01$rust_box${}$drop(::rust::Box<{}> *ptr) noexcept;",
|
||||
instance, inner,
|
||||
);
|
||||
writeln!(
|
||||
out,
|
||||
"const {} *cxxbridge01$rust_box${}$deref(const ::cxxbridge::RustBox<{}> *ptr) noexcept;",
|
||||
"const {} *cxxbridge01$rust_box${}$deref(const ::rust::Box<{}> *ptr) noexcept;",
|
||||
inner, instance, inner,
|
||||
);
|
||||
writeln!(
|
||||
out,
|
||||
"{} *cxxbridge01$rust_box${}$deref_mut(::cxxbridge::RustBox<{}> *ptr) noexcept;",
|
||||
"{} *cxxbridge01$rust_box${}$deref_mut(::rust::Box<{}> *ptr) noexcept;",
|
||||
inner, instance, inner,
|
||||
);
|
||||
writeln!(out, "#endif // CXXBRIDGE01_RUST_BOX_{}", instance);
|
||||
@ -518,7 +498,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
|
||||
let instance = inner.replace("::", "$");
|
||||
|
||||
writeln!(out, "template <>");
|
||||
writeln!(out, "void RustBox<{}>::uninit() noexcept {{", inner);
|
||||
writeln!(out, "void Box<{}>::uninit() noexcept {{", inner);
|
||||
writeln!(
|
||||
out,
|
||||
" return cxxbridge01$rust_box${}$uninit(this);",
|
||||
@ -529,7 +509,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
|
||||
writeln!(out, "template <>");
|
||||
writeln!(
|
||||
out,
|
||||
"void RustBox<{}>::set_raw({} *raw) noexcept {{",
|
||||
"void Box<{}>::set_raw({} *raw) noexcept {{",
|
||||
inner, inner,
|
||||
);
|
||||
writeln!(
|
||||
@ -540,7 +520,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
|
||||
writeln!(out, "}}");
|
||||
|
||||
writeln!(out, "template <>");
|
||||
writeln!(out, "void RustBox<{}>::drop() noexcept {{", inner);
|
||||
writeln!(out, "void Box<{}>::drop() noexcept {{", inner);
|
||||
writeln!(
|
||||
out,
|
||||
" return cxxbridge01$rust_box${}$drop(this);",
|
||||
@ -551,7 +531,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
|
||||
writeln!(out, "template <>");
|
||||
writeln!(
|
||||
out,
|
||||
"const {} *RustBox<{}>::deref() const noexcept {{",
|
||||
"const {} *Box<{}>::deref() const noexcept {{",
|
||||
inner, inner,
|
||||
);
|
||||
writeln!(
|
||||
@ -562,11 +542,7 @@ fn write_rust_box_impl(out: &mut OutFile, ident: &Ident) {
|
||||
writeln!(out, "}}");
|
||||
|
||||
writeln!(out, "template <>");
|
||||
writeln!(
|
||||
out,
|
||||
"{} *RustBox<{}>::deref_mut() noexcept {{",
|
||||
inner, inner,
|
||||
);
|
||||
writeln!(out, "{} *Box<{}>::deref_mut() noexcept {{", inner, inner);
|
||||
writeln!(
|
||||
out,
|
||||
" return cxxbridge01$rust_box${}$deref_mut(this);",
|
||||
|
@ -4,21 +4,19 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
namespace cxxbridge01 {}
|
||||
namespace cxxbridge = cxxbridge01;
|
||||
namespace rust {
|
||||
inline namespace cxxbridge01 {
|
||||
|
||||
namespace cxxbridge01 {
|
||||
|
||||
class RustString final {
|
||||
class String final {
|
||||
public:
|
||||
RustString() noexcept;
|
||||
RustString(const RustString &other) noexcept;
|
||||
RustString(RustString &&other) noexcept;
|
||||
RustString(const char *s);
|
||||
RustString(const std::string &s);
|
||||
RustString &operator=(const RustString &other) noexcept;
|
||||
RustString &operator=(RustString &&other) noexcept;
|
||||
~RustString() noexcept;
|
||||
String() noexcept;
|
||||
String(const String &other) noexcept;
|
||||
String(String &&other) noexcept;
|
||||
String(const char *s);
|
||||
String(const std::string &s);
|
||||
String &operator=(const String &other) noexcept;
|
||||
String &operator=(String &&other) noexcept;
|
||||
~String() noexcept;
|
||||
operator std::string() const;
|
||||
|
||||
// Note: no null terminator.
|
||||
@ -31,14 +29,14 @@ private:
|
||||
std::array<uintptr_t, 3> repr;
|
||||
};
|
||||
|
||||
class RustStr final {
|
||||
class Str final {
|
||||
public:
|
||||
RustStr() noexcept;
|
||||
RustStr(const char *s);
|
||||
RustStr(const std::string &s);
|
||||
RustStr(std::string &&s) = delete;
|
||||
RustStr(const RustStr &other) noexcept;
|
||||
RustStr &operator=(RustStr other) noexcept;
|
||||
Str() noexcept;
|
||||
Str(const char *s);
|
||||
Str(const std::string &s);
|
||||
Str(std::string &&s) = delete;
|
||||
Str(const Str &other) noexcept;
|
||||
Str &operator=(Str other) noexcept;
|
||||
operator std::string() const;
|
||||
|
||||
// Note: no null terminator.
|
||||
@ -54,7 +52,7 @@ public:
|
||||
const char *ptr;
|
||||
size_t len;
|
||||
};
|
||||
RustStr(Repr repr) noexcept;
|
||||
Str(Repr repr) noexcept;
|
||||
operator Repr() noexcept;
|
||||
|
||||
private:
|
||||
@ -63,15 +61,15 @@ private:
|
||||
|
||||
#ifndef CXXBRIDGE01_RUST_BOX
|
||||
#define CXXBRIDGE01_RUST_BOX
|
||||
template <typename T> class RustBox final {
|
||||
template <typename T> class Box final {
|
||||
public:
|
||||
RustBox(const RustBox &other) : RustBox(*other) {}
|
||||
RustBox(RustBox &&other) noexcept : repr(other.repr) { other.repr = 0; }
|
||||
RustBox(const T &val) {
|
||||
Box(const Box &other) : Box(*other) {}
|
||||
Box(Box &&other) noexcept : repr(other.repr) { other.repr = 0; }
|
||||
Box(const T &val) {
|
||||
this->uninit();
|
||||
::new (this->deref_mut()) T(val);
|
||||
}
|
||||
RustBox &operator=(const RustBox &other) {
|
||||
Box &operator=(const Box &other) {
|
||||
if (this != &other) {
|
||||
if (this->repr) {
|
||||
**this = *other;
|
||||
@ -82,7 +80,7 @@ public:
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
RustBox &operator=(RustBox &&other) noexcept {
|
||||
Box &operator=(Box &&other) noexcept {
|
||||
if (this->repr) {
|
||||
this->drop();
|
||||
}
|
||||
@ -90,7 +88,7 @@ public:
|
||||
other.repr = 0;
|
||||
return *this;
|
||||
}
|
||||
~RustBox() noexcept {
|
||||
~Box() noexcept {
|
||||
if (this->repr) {
|
||||
this->drop();
|
||||
}
|
||||
@ -103,8 +101,8 @@ public:
|
||||
|
||||
// Important: requires that `raw` came from an into_raw call. Do not pass a
|
||||
// pointer from `new` or any other source.
|
||||
static RustBox from_raw(T *raw) noexcept {
|
||||
RustBox box;
|
||||
static Box from_raw(T *raw) noexcept {
|
||||
Box box;
|
||||
box.set_raw(raw);
|
||||
return box;
|
||||
}
|
||||
@ -116,7 +114,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
RustBox() noexcept {}
|
||||
Box() noexcept {}
|
||||
void uninit() noexcept;
|
||||
void set_raw(T *) noexcept;
|
||||
T *get_raw() noexcept;
|
||||
@ -127,7 +125,8 @@ private:
|
||||
};
|
||||
#endif // CXXBRIDGE01_RUST_BOX
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const RustString &s);
|
||||
std::ostream &operator<<(std::ostream &os, const RustStr &s);
|
||||
std::ostream &operator<<(std::ostream &os, const String &s);
|
||||
std::ostream &operator<<(std::ostream &os, const Str &s);
|
||||
|
||||
} // namespace cxxbridge01
|
||||
} // inline namespace cxxbridge01
|
||||
} // namespace rust
|
||||
|
@ -3,8 +3,6 @@
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace cxxbridge = cxxbridge01;
|
||||
|
||||
extern "C" {
|
||||
const char *cxxbridge01$cxx_string$data(const std::string &s) noexcept {
|
||||
return s.data();
|
||||
@ -14,56 +12,57 @@ size_t cxxbridge01$cxx_string$length(const std::string &s) noexcept {
|
||||
return s.length();
|
||||
}
|
||||
|
||||
// RustString
|
||||
void cxxbridge01$rust_string$new(cxxbridge::RustString *self) noexcept;
|
||||
void cxxbridge01$rust_string$clone(cxxbridge::RustString *self,
|
||||
const cxxbridge::RustString &other) noexcept;
|
||||
bool cxxbridge01$rust_string$from(cxxbridge::RustString *self, const char *ptr,
|
||||
// rust::String
|
||||
void cxxbridge01$rust_string$new(rust::String *self) noexcept;
|
||||
void cxxbridge01$rust_string$clone(rust::String *self,
|
||||
const rust::String &other) noexcept;
|
||||
bool cxxbridge01$rust_string$from(rust::String *self, const char *ptr,
|
||||
size_t len) noexcept;
|
||||
void cxxbridge01$rust_string$drop(cxxbridge::RustString *self) noexcept;
|
||||
void cxxbridge01$rust_string$drop(rust::String *self) noexcept;
|
||||
const char *
|
||||
cxxbridge01$rust_string$ptr(const cxxbridge::RustString *self) noexcept;
|
||||
size_t cxxbridge01$rust_string$len(const cxxbridge::RustString *self) noexcept;
|
||||
cxxbridge01$rust_string$ptr(const rust::String *self) noexcept;
|
||||
size_t cxxbridge01$rust_string$len(const rust::String *self) noexcept;
|
||||
|
||||
// RustStr
|
||||
// rust::Str
|
||||
bool cxxbridge01$rust_str$valid(const char *ptr, size_t len) noexcept;
|
||||
} // extern "C"
|
||||
|
||||
namespace cxxbridge01 {
|
||||
namespace rust {
|
||||
inline namespace cxxbridge01 {
|
||||
|
||||
RustString::RustString() noexcept { cxxbridge01$rust_string$new(this); }
|
||||
String::String() noexcept { cxxbridge01$rust_string$new(this); }
|
||||
|
||||
RustString::RustString(const RustString &other) noexcept {
|
||||
String::String(const String &other) noexcept {
|
||||
cxxbridge01$rust_string$clone(this, other);
|
||||
}
|
||||
|
||||
RustString::RustString(RustString &&other) noexcept {
|
||||
String::String(String &&other) noexcept {
|
||||
this->repr = other.repr;
|
||||
cxxbridge01$rust_string$new(&other);
|
||||
}
|
||||
|
||||
RustString::RustString(const char *s) {
|
||||
String::String(const char *s) {
|
||||
auto len = strlen(s);
|
||||
if (!cxxbridge01$rust_string$from(this, s, len)) {
|
||||
throw std::invalid_argument("data for RustString is not utf-8");
|
||||
throw std::invalid_argument("data for rust::String is not utf-8");
|
||||
}
|
||||
}
|
||||
|
||||
RustString::RustString(const std::string &s) {
|
||||
String::String(const std::string &s) {
|
||||
auto ptr = s.data();
|
||||
auto len = s.length();
|
||||
if (!cxxbridge01$rust_string$from(this, ptr, len)) {
|
||||
throw std::invalid_argument("data for RustString is not utf-8");
|
||||
throw std::invalid_argument("data for rust::String is not utf-8");
|
||||
}
|
||||
}
|
||||
|
||||
RustString::~RustString() noexcept { cxxbridge01$rust_string$drop(this); }
|
||||
String::~String() noexcept { cxxbridge01$rust_string$drop(this); }
|
||||
|
||||
RustString::operator std::string() const {
|
||||
String::operator std::string() const {
|
||||
return std::string(this->data(), this->size());
|
||||
}
|
||||
|
||||
RustString &RustString::operator=(const RustString &other) noexcept {
|
||||
String &String::operator=(const String &other) noexcept {
|
||||
if (this != &other) {
|
||||
cxxbridge01$rust_string$drop(this);
|
||||
cxxbridge01$rust_string$clone(this, other);
|
||||
@ -71,7 +70,7 @@ RustString &RustString::operator=(const RustString &other) noexcept {
|
||||
return *this;
|
||||
}
|
||||
|
||||
RustString &RustString::operator=(RustString &&other) noexcept {
|
||||
String &String::operator=(String &&other) noexcept {
|
||||
if (this != &other) {
|
||||
cxxbridge01$rust_string$drop(this);
|
||||
this->repr = other.repr;
|
||||
@ -80,65 +79,66 @@ RustString &RustString::operator=(RustString &&other) noexcept {
|
||||
return *this;
|
||||
}
|
||||
|
||||
const char *RustString::data() const noexcept {
|
||||
const char *String::data() const noexcept {
|
||||
return cxxbridge01$rust_string$ptr(this);
|
||||
}
|
||||
|
||||
size_t RustString::size() const noexcept {
|
||||
size_t String::size() const noexcept {
|
||||
return cxxbridge01$rust_string$len(this);
|
||||
}
|
||||
|
||||
size_t RustString::length() const noexcept {
|
||||
size_t String::length() const noexcept {
|
||||
return cxxbridge01$rust_string$len(this);
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const RustString &s) {
|
||||
std::ostream &operator<<(std::ostream &os, const String &s) {
|
||||
os.write(s.data(), s.size());
|
||||
return os;
|
||||
}
|
||||
|
||||
RustStr::RustStr() noexcept
|
||||
Str::Str() noexcept
|
||||
: repr(Repr{reinterpret_cast<const char *>(this), 0}) {}
|
||||
|
||||
RustStr::RustStr(const char *s) : repr(Repr{s, strlen(s)}) {
|
||||
Str::Str(const char *s) : repr(Repr{s, strlen(s)}) {
|
||||
if (!cxxbridge01$rust_str$valid(this->repr.ptr, this->repr.len)) {
|
||||
throw std::invalid_argument("data for RustStr is not utf-8");
|
||||
throw std::invalid_argument("data for rust::Str is not utf-8");
|
||||
}
|
||||
}
|
||||
|
||||
RustStr::RustStr(const std::string &s) : repr(Repr{s.data(), s.length()}) {
|
||||
Str::Str(const std::string &s) : repr(Repr{s.data(), s.length()}) {
|
||||
if (!cxxbridge01$rust_str$valid(this->repr.ptr, this->repr.len)) {
|
||||
throw std::invalid_argument("data for RustStr is not utf-8");
|
||||
throw std::invalid_argument("data for rust::Str is not utf-8");
|
||||
}
|
||||
}
|
||||
|
||||
RustStr::RustStr(const RustStr &) noexcept = default;
|
||||
Str::Str(const Str &) noexcept = default;
|
||||
|
||||
RustStr &RustStr::operator=(RustStr other) noexcept {
|
||||
Str &Str::operator=(Str other) noexcept {
|
||||
this->repr = other.repr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
RustStr::operator std::string() const {
|
||||
Str::operator std::string() const {
|
||||
return std::string(this->data(), this->size());
|
||||
}
|
||||
|
||||
const char *RustStr::data() const noexcept { return this->repr.ptr; }
|
||||
const char *Str::data() const noexcept { return this->repr.ptr; }
|
||||
|
||||
size_t RustStr::size() const noexcept { return this->repr.len; }
|
||||
size_t Str::size() const noexcept { return this->repr.len; }
|
||||
|
||||
size_t RustStr::length() const noexcept { return this->repr.len; }
|
||||
size_t Str::length() const noexcept { return this->repr.len; }
|
||||
|
||||
RustStr::RustStr(Repr repr_) noexcept : repr(repr_) {}
|
||||
Str::Str(Repr repr_) noexcept : repr(repr_) {}
|
||||
|
||||
RustStr::operator Repr() noexcept { return this->repr; }
|
||||
Str::operator Repr() noexcept { return this->repr; }
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const RustStr &s) {
|
||||
std::ostream &operator<<(std::ostream &os, const Str &s) {
|
||||
os.write(s.data(), s.size());
|
||||
return os;
|
||||
}
|
||||
|
||||
} // namespace cxxbridge01
|
||||
} // inline namespace cxxbridge01
|
||||
} // namespace rust
|
||||
|
||||
extern "C" {
|
||||
void cxxbridge01$unique_ptr$std$string$null(
|
||||
|
@ -302,10 +302,10 @@
|
||||
//!
|
||||
//! <table>
|
||||
//! <tr><th>name in Rust</th><th>name in C++</th><th>restrictions</th></tr>
|
||||
//! <tr><td>String</td><td>cxxbridge::RustString</td><td></td></tr>
|
||||
//! <tr><td>&str</td><td>cxxbridge::RustStr</td><td></td></tr>
|
||||
//! <tr><td>String</td><td>rust::String</td><td></td></tr>
|
||||
//! <tr><td>&str</td><td>rust::Str</td><td></td></tr>
|
||||
//! <tr><td><a href="https://docs.rs/cxx/0.1/cxx/struct.CxxString.html">CxxString</a></td><td>std::string</td><td><sup><i>cannot be passed by value</i></sup></td></tr>
|
||||
//! <tr><td>Box<T></td><td>cxxbridge::RustBox<T></td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
|
||||
//! <tr><td>Box<T></td><td>rust::Box<T></td><td><sup><i>cannot hold opaque C++ type</i></sup></td></tr>
|
||||
//! <tr><td><a href="https://docs.rs/cxx/0.1/cxx/struct.UniquePtr.html">UniquePtr<T></a></td><td>std::unique_ptr<T></td><td><sup><i>cannot hold opaque Rust type</i></sup></td></tr>
|
||||
//! <tr><td></td><td></td><td></td></tr>
|
||||
//! </table>
|
||||
|
@ -19,12 +19,12 @@ std::unique_ptr<C> c_return_unique_ptr() {
|
||||
|
||||
const size_t &c_return_ref(const Shared &shared) { return shared.z; }
|
||||
|
||||
cxxbridge::RustStr c_return_str(const Shared &shared) {
|
||||
rust::Str c_return_str(const Shared &shared) {
|
||||
(void)shared;
|
||||
return "2020";
|
||||
}
|
||||
|
||||
cxxbridge::RustString c_return_rust_string() { return "2020"; }
|
||||
rust::String c_return_rust_string() { return "2020"; }
|
||||
|
||||
std::unique_ptr<std::string> c_return_unique_ptr_string() {
|
||||
return std::unique_ptr<std::string>(new std::string("2020"));
|
||||
@ -42,7 +42,7 @@ void c_take_shared(Shared shared) {
|
||||
}
|
||||
}
|
||||
|
||||
void c_take_box(cxxbridge::RustBox<R> r) {
|
||||
void c_take_box(rust::Box<R> r) {
|
||||
(void)r;
|
||||
cxx_test_suite_set_correct();
|
||||
}
|
||||
@ -61,13 +61,13 @@ void c_take_ref_c(const C &c) {
|
||||
}
|
||||
}
|
||||
|
||||
void c_take_str(cxxbridge::RustStr s) {
|
||||
void c_take_str(rust::Str s) {
|
||||
if (std::string(s) == "2020") {
|
||||
cxx_test_suite_set_correct();
|
||||
}
|
||||
}
|
||||
|
||||
void c_take_rust_string(cxxbridge::RustString s) {
|
||||
void c_take_rust_string(rust::String s) {
|
||||
if (std::string(s) == "2020") {
|
||||
cxx_test_suite_set_correct();
|
||||
}
|
||||
@ -99,8 +99,8 @@ extern "C" const char *cxx_run_test() noexcept {
|
||||
r_take_shared(Shared{2020});
|
||||
r_take_unique_ptr(std::unique_ptr<C>(new C{2020}));
|
||||
r_take_ref_c(C{2020});
|
||||
r_take_str(cxxbridge::RustStr("2020"));
|
||||
// TODO r_take_rust_string(cxxbridge::RustString("2020"));
|
||||
r_take_str(rust::Str("2020"));
|
||||
// TODO r_take_rust_string(rust::String("2020"));
|
||||
r_take_unique_ptr_string(
|
||||
std::unique_ptr<std::string>(new std::string("2020")));
|
||||
|
||||
|
@ -19,21 +19,21 @@ private:
|
||||
|
||||
size_t c_return_primitive();
|
||||
Shared c_return_shared();
|
||||
cxxbridge::RustBox<R> c_return_box();
|
||||
rust::Box<R> c_return_box();
|
||||
std::unique_ptr<C> c_return_unique_ptr();
|
||||
const size_t &c_return_ref(const Shared &shared);
|
||||
cxxbridge::RustStr c_return_str(const Shared &shared);
|
||||
cxxbridge::RustString c_return_rust_string();
|
||||
rust::Str c_return_str(const Shared &shared);
|
||||
rust::String c_return_rust_string();
|
||||
std::unique_ptr<std::string> c_return_unique_ptr_string();
|
||||
|
||||
void c_take_primitive(size_t n);
|
||||
void c_take_shared(Shared shared);
|
||||
void c_take_box(cxxbridge::RustBox<R> r);
|
||||
void c_take_box(rust::Box<R> r);
|
||||
void c_take_unique_ptr(std::unique_ptr<C> c);
|
||||
void c_take_ref_r(const R &r);
|
||||
void c_take_ref_c(const C &c);
|
||||
void c_take_str(cxxbridge::RustStr s);
|
||||
void c_take_rust_string(cxxbridge::RustString s);
|
||||
void c_take_str(rust::Str s);
|
||||
void c_take_rust_string(rust::String s);
|
||||
void c_take_unique_ptr_string(std::unique_ptr<std::string> s);
|
||||
|
||||
} // namespace tests
|
||||
|
Loading…
x
Reference in New Issue
Block a user