-
Notifications
You must be signed in to change notification settings - Fork 365
Decoupling new type mappings from the cxx
crate
#251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was thinking that one day, |
We support something like this already, which would work for WTF::String. My codebase uses it for Folly strings. https://docs.rs/cxx/0.3.4/cxx/trait.ExternType.html unsafe impl ExternType for WtfString {
type Id = type_id!("WTF::String");
} It doesn't handle generic types at the moment but I am hopeful that it would be possible to make that work and be equivalently easy to use. unsafe impl<K: ??, V: ??> ExternType for BoostFlatMap<K, V> {
type Id = type_id!("boost::container::flat_map<K, V>");
} It also doesn't handle types without a compatible Rust definition yet, i.e. some kind of bindgen-based definition of the type in Rust is required. We'll want to make some alternative approach available there. #[repr(transparent)]
pub struct WtfString(cxx::OpaqueType);
unsafe impl ExternType for WtfString {
type Id = type_id!("WTF::String");
const OPAQUE: bool = true;
} I think the above would be able to cover all the types you named. Roughly that's types for which the goal is just shuffling values or references from language A to B. There is another tier of types that require some deeper integration with the code generator. So far I think the ones I know are just Result-style types ( |
To respond to your questions more specifically:
|
Is it possible today to provide a UniquePtr-like implementation of a custom smart pointer (Eg for boost::scoped_ptr)? I understand you mentioned that template support is needed (is there an issue tracking that?) but is there anything else beyond that required to properly communicate resource ownership (ie to implement the Clone trait to do the right thing if relevant for that type). |
@vlovich that should be possible already with the feature set of cxx 1.0. See #524 for runnable example code. Generic cxx/scoped_ptr_demo/include/demo.h Line 10 in bb4fda0
cxx/scoped_ptr_demo/src/main.rs Line 24 in bb4fda0
|
It's not clear to me that's sufficient for the use-case I'm thinking. Specifically I'm thinking of it to wrap the cap'n'proto RPC C++ library rather than trying to reimplement it in Rust (while the So the goal would be to codegen the C++ interfaces for use with the underlying library but come up with an efficient way to access the C++ library idiomatically in a way that's safe. Most things there deal with a custom It would seem like in this situation it would be expected to move |
For Zcash we really really need, in the relatively near term, to have good interoperation between some Result-style type on the C++ side (e.g. tl::expected) and Rust's Our issue for this is zcash/zcash#4816 (comment) Edit: oh I guess we could name every instantiation with different T, E that we actually use? I'll have to look at how much boilerplate that adds in practice. We might try doing that for now with a view to migrating it when generics are supported (since we won't be exposing these type aliases in any public-facing APIs). |
Hello,
Thank you for all the work on the
cxx
crate. I am sorry for a not very specific nor constructive issue, but I was wondering what it would take to decouple individual type mappings (e.g.std::vector<T>
toCxxVector<T>
,std::map<K, V>
toTBD<K, V>
, etc.) from thecxx
crate itself. In particular, some projects have their own container implementations (e.g. Boost''s flat_map or Chromium's small_map or WebKit's WTF::Vector) or string implementations (e.g. WebKit's 16-bit String). It would be great if such projects could extendcxx
with the extra mappings, without having to modify thecxx
crate itself. AFAIUcxx
already supports translatingstd::string
andstd::vector
and plans to supportstd::map
andstd::unordered_map
, but obviously would probably dislike having knowledge of Boost's, Chromium's or WebKit's types.Could you please offer any feedback / thoughts on the above?
It is unclear to me how one would declare that a given C++ type should be recognized and wrapped in a specific way in Rust. Specifying the C++ type via its namespace-qualified name is definitely one possible way, but maybe some form of duck-typing is also desirable (handwaving: if a C++ type has
begin
andend
methods, then it is wrapped asstd::iter::Iterator
or asIntoIterator
; if it quacks like a drop-in replacement for std::map, then it is wrapped asCxxMap
; etc);It is unclear to me exactly which C++ => Rust type mappings would need to remain in the core
cxx
(even if they don't need to be incxx
and could be decoupled for design/hygiene reasons, we might still want to keep all thestd
library types close tocxx
, to promote one canonicalstd
=> Rust type mapping).UniquePtr
would remain in the core.CxxString
andCxxVector
.It is unclear to me how extensible type mappings impact with the safety guarantees of
cxx
and Rust./cc @adetaylor
The text was updated successfully, but these errors were encountered: