Comments (10)
I would like start the discussion again how a sharable Option type could be implemented. The problem with Rust's Option
seems to be that the memory layout is not stable.
How about using an custom option type with repr(C, u8)
like so:
#[repr(C, u8)]
pub enum CxxOptional<T> {
None,
Some(T),
}
This has the advantage that it is still a rust enum but it has a defined memory layout. Moreover, it should have the same memory layout as std::optional
. According to the rust representation doc this is equivalent to:
#[repr(C)]
struct CxxOptionalRepr {
tag: CxxOptionalDiscriminant,
payload: CxxOptionalFields,
}
// This is the discriminant enum.
#[repr(u8)]
enum CxxOptionalDiscriminant { None, Some }
// This is the variant union.
#[repr(C)]
union CxxOptionalFields<T> {
None: Dummy,
Some: T
}
#[repr(C)]
struct Dummy;
The corresponding C++ type looks like:
struct CxxOptional{
enum class Tag {
None,
Some,
};
struct Some_Body {
T _0;
};
Tag tag;
union {
Some_Body some;
};
};
The tag field is of type unsigned char
which should make it equivalent to std::optional
which uses a bool
. This should work because c++ interprets 0
as false
and every other number as true
.
What do you think about it?
from cxx.
I'd be happy with a solution that always copies initially.
from cxx.
I'm trying an implementation of this, following the code patterns for Vec and CxxVector
from cxx.
#44 is required for this, along with an extra flag_if_supported("/std:c++17")
, which I will add in the PR for this issue. Otherwise the MSVC compiler complains:
The contents of <optional> are available only with C++17 or later.
from cxx.
Most work needed for this is clear. I can follow the RustVec and CxxVector work.
There is one difference between the already-supported types like Box
, Vec
and String
that I'm not sure how best to tackle -- the already-supported types offer a function to get a (sometimes mut
) pointer to the underlying contents, such as as_mut_ptr()
. Rust Option
doesn't.
So, for example, we have:
Line 260 in 32439da
RustOption
type that needs to be implemented?from cxx.
We don't use Vec::as_mut_ptr. That line is calling MaybeUninit::as_mut_ptr.
from cxx.
@philipcraig Are you still planning on working on this, or could you use help? I'm finding myself in a situation where I could really use Option, for a result-like type on an interface.
from cxx.
Hi @sbrocket, I got stalled when I realised late on that there is not a fixed repr for Rust Option, so that we will have to copy bits. It should all be doable but my approach needs to be thrown away and re-done. In short, if you would like to tackle it, please do.
My work in progress is at https://github.com/philipcraig/cxx/tree/support_option_and_std_optional. It doesn't build and has the wrong assumptions about the repr of rust Option, but some of it may be useful
from cxx.
when I realised late on that there is not a fixed repr for Rust Option
rust-lang/rust#75454 if you hadn't come across it.
from cxx.
Sadly, it turns out that the fields of std::optional
are arranged the other way around:
struct optional{
union {
T some;
};
bool _engaged;
};
from cxx.
Related Issues (20)
- Extern C++ Enums HOT 5
- Advice/example on docs.rs strategy HOT 2
- Feature Request: Add support for nested Vector types HOT 1
- Building two CXX C++ bindings with the same symbols but in different namespaces. HOT 2
- Including bridge from another crate
- Error in configuring crates_universe when included as a Bazel module HOT 6
- Error compiling `cxx.cc` on Windows HOT 1
- Proposal: implementing native C++ std::error_code
- Link failure when using CXX-backed function from another crate HOT 1
- Unsupported element type of Vec<[u64; 3]>
- How to include C++ files whose directory is given by an environment variable? HOT 1
- Error compiling demo to wasm on macOS HOT 5
- Is there a way to expose a C++ class that is itself a wrapper on `std::shared_ptr`?
- How can I `include!` a file in OUT_DIR
- How can I return a vector of non-trivial type of C++ to Rust?
- LNK2019 unresolved symbol for Vec<SharedStruct> using MSVC
- Using CXX in multiple DLLs always results in undefined behavior?
- Does the core safety claim need rephrasing? HOT 6
- Example of using cxx in an external mixed bazel C++ & Rust environment?
- Undefined symbols for architecture arm64: "rust::cxxbridge1::String::String(char const *)" HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cxx.