Skip to content

Commit 9b92578

Browse files
committed
Add support for Option<&String> and Option<&Vec>
1 parent da8358f commit 9b92578

19 files changed

+795
-111
lines changed

gen/src/write.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,8 @@ enum RustOption<'a> {
13771377
RustBox(&'a Ident),
13781378
Ref(&'a Ident),
13791379
MutRef(&'a Ident),
1380+
RefVec(&'a Ident),
1381+
MutRefVec(&'a Ident),
13801382
}
13811383

13821384
trait ToTypename {
@@ -1408,6 +1410,13 @@ impl<'a> ToTypename for RustOption<'a> {
14081410
}
14091411
RustOption::Ref(inner) => format!("const {}*", inner.to_typename(types)),
14101412
RustOption::MutRef(inner) => format!("{}*", inner.to_typename(types)),
1413+
RustOption::RefVec(inner) => format!(
1414+
"const ::rust::cxxbridge1::Vec<{}>*",
1415+
inner.to_typename(types)
1416+
),
1417+
RustOption::MutRefVec(inner) => {
1418+
format!("::rust::cxxbridge1::Vec<{}>*", inner.to_typename(types))
1419+
}
14111420
}
14121421
}
14131422
}
@@ -1445,6 +1454,14 @@ impl<'a> ToMangled for RustOption<'a> {
14451454
let symbol = symbol::join(&[&inner.to_mangled(types)]);
14461455
symbol
14471456
}
1457+
RustOption::RefVec(inner) => {
1458+
let symbol = symbol::join(&[&"const", &"Vec", &inner.to_mangled(types)]);
1459+
symbol
1460+
}
1461+
RustOption::MutRefVec(inner) => {
1462+
let symbol = symbol::join(&[&"Vec", &inner.to_mangled(types)]);
1463+
symbol
1464+
}
14481465
}
14491466
}
14501467
}
@@ -1572,6 +1589,18 @@ fn write_rust_option_extern(out: &mut OutFile, inner: OptionInner) {
15721589
}
15731590
RustOption::MutRef(key.rust)
15741591
}
1592+
OptionInner::RefVec(key) => {
1593+
if out.types.try_resolve(key.rust).is_none() {
1594+
return;
1595+
}
1596+
RustOption::RefVec(key.rust)
1597+
}
1598+
OptionInner::MutRefVec(key) => {
1599+
if out.types.try_resolve(key.rust).is_none() {
1600+
return;
1601+
}
1602+
RustOption::MutRefVec(key.rust)
1603+
}
15751604
};
15761605
let inner = element.to_typename(out.types);
15771606
let instance = element.to_mangled(out.types);
@@ -1738,6 +1767,18 @@ fn write_rust_option_impl(out: &mut OutFile, inner: OptionInner) {
17381767
}
17391768
RustOption::MutRef(key.rust)
17401769
}
1770+
OptionInner::RefVec(key) => {
1771+
if out.types.try_resolve(key.rust).is_none() {
1772+
return;
1773+
}
1774+
RustOption::RefVec(key.rust)
1775+
}
1776+
OptionInner::MutRefVec(key) => {
1777+
if out.types.try_resolve(key.rust).is_none() {
1778+
return;
1779+
}
1780+
RustOption::MutRefVec(key.rust)
1781+
}
17411782
};
17421783
let inner = element.to_typename(out.types);
17431784
let instance = element.to_mangled(out.types);

macro/src/expand.rs

Lines changed: 154 additions & 58 deletions
Large diffs are not rendered by default.

src/cxx.cc

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,29 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
804804
CXX_TYPE** cxxbridge1$rust_option$##RUST_TYPE##$value_ptr( \
805805
rust::Option<CXX_TYPE *> *ptr) noexcept; \
806806
void cxxbridge1$rust_option$##RUST_TYPE##$set( \
807-
rust::Option<CXX_TYPE *> *ptr, CXX_TYPE *&& value) noexcept;
807+
rust::Option<CXX_TYPE *> *ptr, CXX_TYPE *&& value) noexcept; \
808+
/* Vec<T> implementation */ \
809+
void cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$new( \
810+
rust::Option<rust::Vec<CXX_TYPE> const *> *ptr) noexcept; \
811+
void cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$drop( \
812+
rust::Option<rust::Vec<CXX_TYPE> const *> *ptr) noexcept; \
813+
bool cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$has_value( \
814+
const rust::Option<rust::Vec<CXX_TYPE> const *> *ptr) noexcept; \
815+
rust::Vec<CXX_TYPE> const ** cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$value_ptr( \
816+
rust::Option<rust::Vec<CXX_TYPE> const *> *ptr) noexcept; \
817+
void cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$set( \
818+
rust::Option<rust::Vec<CXX_TYPE> const *> *ptr, rust::Vec<CXX_TYPE> const *&& value) noexcept; \
819+
void cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$new( \
820+
rust::Option<rust::Vec<CXX_TYPE> *> *ptr) noexcept; \
821+
void cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$drop( \
822+
rust::Option<rust::Vec<CXX_TYPE> *> *ptr) noexcept; \
823+
bool cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$has_value( \
824+
const rust::Option<rust::Vec<CXX_TYPE> *> *ptr) noexcept; \
825+
rust::Vec<CXX_TYPE>** cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$value_ptr( \
826+
rust::Option<rust::Vec<CXX_TYPE> *> *ptr) noexcept; \
827+
void cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$set( \
828+
rust::Option<rust::Vec<CXX_TYPE> *> *ptr, rust::Vec<CXX_TYPE> *&& value) noexcept;
829+
808830

809831
#define RUST_OPTION_OPS(RUST_TYPE, CXX_TYPE) \
810832
template <> \
@@ -847,10 +869,53 @@ static_assert(sizeof(std::string) <= kMaxExpectedWordsInString * sizeof(void *),
847869
template <> \
848870
void Option<CXX_TYPE *>::set(CXX_TYPE *&& value) noexcept { \
849871
return cxxbridge1$rust_option$##RUST_TYPE##$set(this, std::move(value)); \
872+
} \
873+
/* Vec<T> impl */ \
874+
template <> \
875+
Option<rust::Vec<CXX_TYPE> const *>::Option() noexcept { \
876+
cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$new(this); \
877+
} \
878+
template <> \
879+
void Option<rust::Vec<CXX_TYPE> const *>::drop() noexcept { \
880+
cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$drop(this); \
881+
} \
882+
template <> \
883+
bool Option<rust::Vec<CXX_TYPE> const *>::has_value() const noexcept { \
884+
return cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$has_value(this); \
885+
} \
886+
template <> \
887+
rust::Vec<CXX_TYPE> const ** Option<rust::Vec<CXX_TYPE> const *>::value_ptr() noexcept { \
888+
return cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$value_ptr(this); \
889+
} \
890+
template <> \
891+
void Option<rust::Vec<CXX_TYPE> const *>::set(rust::Vec<CXX_TYPE> const *&& value) noexcept { \
892+
return cxxbridge1$rust_option$const$rust_vec$##RUST_TYPE##$set( \
893+
this, std::move(value)); \
894+
} \
895+
template <> \
896+
Option<rust::Vec<CXX_TYPE> *>::Option() noexcept { \
897+
cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$new(this); \
898+
} \
899+
template <> \
900+
void Option<rust::Vec<CXX_TYPE> *>::drop() noexcept { \
901+
cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$drop(this); \
902+
} \
903+
template <> \
904+
bool Option<rust::Vec<CXX_TYPE> *>::has_value() const noexcept { \
905+
return cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$has_value(this); \
906+
} \
907+
template <> \
908+
rust::Vec<CXX_TYPE>** Option<rust::Vec<CXX_TYPE> *>::value_ptr() noexcept { \
909+
return cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$value_ptr(this); \
910+
} \
911+
template <> \
912+
void Option<rust::Vec<CXX_TYPE> *>::set(rust::Vec<CXX_TYPE> *&& value) noexcept { \
913+
return cxxbridge1$rust_option$rust_vec$##RUST_TYPE##$set(this, std::move(value)); \
850914
}
851915

852916
#define FOR_EACH_RUST_OPTION(MACRO) \
853-
FOR_EACH_NUMERIC(MACRO)
917+
FOR_EACH_NUMERIC(MACRO) \
918+
MACRO(string, rust::String)
854919

855920
extern "C" {
856921
FOR_EACH_STD_VECTOR(STD_VECTOR_OPS)

0 commit comments

Comments
 (0)