autocxx/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(nightly, feature(unsize))]
3#![cfg_attr(nightly, feature(dispatch_from_dyn))]
4
5// Copyright 2020 Google LLC
6//
7// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
10// option. This file may not be copied, modified, or distributed
11// except according to those terms.
12
13// The crazy macro_rules magic in this file is thanks to dtolnay@
14// and is a way of attaching rustdoc to each of the possible directives
15// within the include_cpp outer macro. None of the directives actually
16// do anything - all the magic is handled entirely by
17// autocxx_macro::include_cpp_impl.
18
19mod reference_wrapper;
20mod rvalue_param;
21pub mod subclass;
22mod value_param;
23
24pub use reference_wrapper::{AsCppMutRef, AsCppRef, CppMutRef, CppPin, CppRef, CppUniquePtrPin};
25
26#[cfg_attr(doc, aquamarine::aquamarine)]
27/// Include some C++ headers in your Rust project.
28///
29/// This macro allows you to include one or more C++ headers within
30/// your Rust code, and call their functions fairly naturally.
31///
32/// # Examples
33///
34/// C++ header (`input.h`):
35/// ```cpp
36/// #include <cstdint>
37///
38/// uint32_t do_math(uint32_t a);
39/// ```
40///
41/// Rust code:
42/// ```
43/// # use autocxx_macro::include_cpp_impl as include_cpp;
44/// include_cpp!(
45/// #   parse_only!()
46///     #include "input.h"
47///     generate!("do_math")
48///     safety!(unsafe)
49/// );
50///
51/// # mod ffi { pub fn do_math(a: u32) -> u32 { a+3 } }
52/// # fn main() {
53/// ffi::do_math(3);
54/// # }
55/// ```
56///
57/// The resulting bindings will use idiomatic Rust wrappers for types from the [cxx]
58/// crate, for example [`cxx::UniquePtr`] or [`cxx::CxxString`]. Due to the care and thought
59/// that's gone into the [cxx] crate, such bindings are pleasant and idiomatic to use
60/// from Rust, and usually don't require the `unsafe` keyword.
61///
62/// For full documentation, see [the manual](https://google.github.io/autocxx/).
63///
64/// # The [`include_cpp`] macro
65///
66/// Within the braces of the `include_cpp!{...}` macro, you should provide
67/// a list of at least the following:
68///
69/// * `#include "cpp_header.h"`: a header filename to parse and include
70/// * `generate!("type_or_function_name")`: a type or function name whose declaration
71///   should be made available to C++. (See the section on Allowlisting, below).
72/// * Optionally, `safety!(unsafe)` - see discussion of [`safety`].
73///
74/// Other directives are possible as documented in this crate.
75///
76/// Now, try to build your Rust project. `autocxx` may fail to generate bindings
77/// for some of the items you specified with [generate] directives: remove
78/// those directives for now, then see the next section for advice.
79///
80/// # Allowlisting
81///
82/// How do you inform autocxx which bindings to generate? There are three
83/// strategies:
84///
85/// * *Recommended*: provide various [`generate`] directives in the
86///   [`include_cpp`] macro. This can specify functions or types.
87/// * *Not recommended*: in your `build.rs`, call `Builder::auto_allowlist`.
88///   This will attempt to spot _uses_ of FFI bindings anywhere in your Rust code
89///   and build the allowlist that way. This is experimental and has known limitations.
90/// * *Strongly not recommended*: use [`generate_all`]. This will attempt to
91///   generate Rust bindings for _any_ C++ type or function discovered in the
92///   header files. This is generally a disaster if you're including any
93///   remotely complex header file: we'll try to generate bindings for all sorts
94///   of STL types. This will be slow, and some may well cause problems.
95///   Effectively this is just a debug option to discover such problems. Don't
96///   use it!
97///
98/// # Internals
99///
100/// For documentation on how this all actually _works_, see
101/// `IncludeCppEngine` within the `autocxx_engine` crate.
102#[macro_export]
103macro_rules! include_cpp {
104    (
105        $(#$include:ident $lit:literal)*
106        $($mac:ident!($($arg:tt)*))*
107    ) => {
108        $($crate::$include!{__docs})*
109        $($crate::$mac!{__docs})*
110        $crate::include_cpp_impl! {
111            $(#include $lit)*
112            $($mac!($($arg)*))*
113        }
114    };
115}
116
117/// Include a C++ header. A directive to be included inside
118/// [include_cpp] - see [include_cpp] for details
119#[macro_export]
120macro_rules! include {
121    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
122}
123
124/// Generate Rust bindings for the given C++ type or function.
125/// A directive to be included inside
126/// [include_cpp] - see [include_cpp] for general information.
127/// See also [generate_pod].
128#[macro_export]
129macro_rules! generate {
130    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
131}
132
133/// Generate as "plain old data" and add to allowlist.
134/// Generate Rust bindings for the given C++ type such that
135/// it can be passed and owned by value in Rust. This only works
136/// for C++ types which have trivial move constructors and no
137/// destructor - you'll encounter a compile error otherwise.
138/// If your type doesn't match that description, use [generate]
139/// instead, and own the type using [UniquePtr][cxx::UniquePtr].
140/// A directive to be included inside
141/// [include_cpp] - see [include_cpp] for general information.
142#[macro_export]
143macro_rules! generate_pod {
144    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
145}
146
147/// Generate Rust bindings for all C++ types and functions
148/// in a given namespace.
149/// A directive to be included inside
150/// [include_cpp] - see [include_cpp] for general information.
151/// See also [generate].
152#[macro_export]
153macro_rules! generate_ns {
154    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
155}
156
157/// Generate Rust bindings for all C++ types and functions
158/// found. Highly experimental and not recommended.
159/// A directive to be included inside
160/// [include_cpp] - see [include_cpp] for general information.
161/// See also [generate].
162#[macro_export]
163macro_rules! generate_all {
164    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
165}
166
167/// Generate as "plain old data". For use with [generate_all]
168/// and similarly experimental.
169#[macro_export]
170macro_rules! pod {
171    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
172}
173
174/// Skip the normal generation of a `make_string` function
175/// and other utilities which we might generate normally.
176/// A directive to be included inside
177/// [include_cpp] - see [include_cpp] for general information.
178#[macro_export]
179macro_rules! exclude_utilities {
180    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
181}
182
183/// Entirely block some type from appearing in the generated
184/// code. This can be useful if there is a type which is not
185/// understood by bindgen or autocxx, and incorrect code is
186/// otherwise generated.
187/// This is 'greedy' in the sense that any functions/methods
188/// which take or return such a type will _also_ be blocked.
189///
190/// A directive to be included inside
191/// [include_cpp] - see [include_cpp] for general information.
192#[macro_export]
193macro_rules! block {
194    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
195}
196
197/// Avoid generating implicit constructors for this type.
198/// The rules for when to generate C++ implicit constructors
199/// are complex, and if autocxx gets it wrong, you can block
200/// such constructors using this.
201///
202/// A directive to be included inside
203/// [include_cpp] - see [include_cpp] for general information.
204#[macro_export]
205macro_rules! block_constructors {
206    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
207}
208
209/// The name of the mod to be generated with the FFI code.
210/// The default is `ffi`.
211///
212/// A directive to be included inside
213/// [include_cpp] - see [include_cpp] for general information.
214#[macro_export]
215macro_rules! name {
216    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
217}
218
219/// A concrete type to make, for example
220/// `concrete!("Container<Contents>")`.
221/// All types must already be on the allowlist by having used
222/// `generate!` or similar.
223///
224/// A directive to be included inside
225/// [include_cpp] - see [include_cpp] for general information.
226#[macro_export]
227macro_rules! concrete {
228    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
229}
230
231/// Specifies a global safety policy for functions generated
232/// from these headers. By default (without such a `safety!`
233/// directive) all such functions are marked as `unsafe` and
234/// therefore can only be called within an `unsafe {}` block
235/// or some `unsafe` function which you create.
236///
237/// Alternatively, by specifying a `safety!` block you can
238/// declare that most generated functions are in fact safe.
239/// Specifically, you'd specify:
240/// `safety!(unsafe)`
241/// or
242/// `safety!(unsafe_ffi)`
243/// These two options are functionally identical. If you're
244/// unsure, simply use `unsafe`. The reason for the
245/// latter option is if you have code review policies which
246/// might want to give a different level of scrutiny to
247/// C++ interop as opposed to other types of unsafe Rust code.
248/// Maybe in your organization, C++ interop is less scary than
249/// a low-level Rust data structure using pointer manipulation.
250/// Or maybe it's more scary. Either way, using `unsafe` for
251/// the data structure and using `unsafe_ffi` for the C++
252/// interop allows you to apply different linting tools and
253/// policies to the different options.
254///
255/// Irrespective, C++ code is of course unsafe. It's worth
256/// noting that use of C++ can cause unexpected unsafety at
257/// a distance in faraway Rust code. As with any use of the
258/// `unsafe` keyword in Rust, *you the human* are declaring
259/// that you've analyzed all possible ways that the code
260/// can be used and you are guaranteeing to the compiler that
261/// no badness can occur. Good luck.
262///
263/// Generated C++ APIs which use raw pointers remain `unsafe`
264/// no matter what policy you choose.
265///
266/// There's an additional possible experimental safety
267/// policy available here:
268/// `safety!(unsafe_references_wrapped)`
269/// This policy treats C++ references as scary and requires
270/// them to be wrapped in a `CppRef` type: see [`CppRef`].
271/// This only works on nightly Rust because it
272/// depends upon an unstable feature
273/// (`arbitrary_self_types_pointers`). However, it should
274/// eliminate all undefined behavior related to Rust's
275/// stricter aliasing rules than C++.
276#[macro_export]
277macro_rules! safety {
278    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
279}
280
281/// Whether to avoid generating [`cxx::UniquePtr`] and [`cxx::Vector`]
282/// implementations. This is primarily useful for reducing test cases and
283/// shouldn't be used in normal operation.
284///
285/// A directive to be included inside
286/// [include_cpp] - see [include_cpp] for general information.
287#[macro_export]
288macro_rules! exclude_impls {
289    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
290}
291
292/// Indicates that a C++ type is not to be generated by autocxx in this case,
293/// but instead should refer to some pre-existing Rust type.
294///
295/// If you wish for the type to be POD, you can use a `pod!` directive too
296/// (but see the "requirements" section below).
297///
298/// The syntax is:
299/// `extern_cpp_type!("CppNameGoesHere", path::to::rust::type)`
300///
301/// Generally speaking, this should be used only to refer to types
302/// generated elsewhere by `autocxx` or `cxx` to ensure that they meet
303/// all the right requirements. It's possible - but fragile - to
304/// define such types yourself.
305///
306/// # Requirements for externally defined Rust types
307///
308/// It's generally expected that you would make such a type
309/// in Rust using a separate `include_cpp!` macro, or
310/// a manual `#[cxx::bridge]` directive somehwere. That is, this
311/// directive is intended mainly for use in cross-linking different
312/// sets of bindings in different mods, rather than truly to point to novel
313/// external types.
314///
315/// But with that in mind, here are the requirements you must stick to.
316///
317/// For non-POD external types:
318/// * The size and alignment of this type *must* be correct.
319///
320/// For POD external types:
321/// * As above
322/// * Your type must correspond to the requirements of
323///   [`cxx::kind::Trivial`]. In general, that means, no move constructor
324///   and no destructor. If you generate this type using `cxx` itself
325///   (or `autocxx`) this will be enforced using `static_assert`s
326///   within the generated C++ code. Without using those tools, you're
327///   on your own for determining this... and it's hard because the presence
328///   of particular fields or base classes may well result in your type
329///   violating those rules.
330///
331/// A directive to be included inside
332/// [include_cpp] - see [include_cpp] for general information.
333#[macro_export]
334macro_rules! extern_cpp_type {
335    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
336}
337
338/// Indicates that a C++ type is not to be generated by autocxx in this case,
339/// but instead should refer to some pre-existing Rust type. Unlike
340/// `extern_cpp_type!`, there's no need for the size and alignment of this
341/// type to be correct.
342///
343/// The syntax is:
344/// `extern_cpp_opaque_type!("CppNameGoesHere", path::to::rust::type)`
345///
346/// A directive to be included inside
347/// [include_cpp] - see [include_cpp] for general information.
348#[macro_export]
349macro_rules! extern_cpp_opaque_type {
350    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
351}
352
353/// Deprecated - use [`extern_rust_type`] instead.
354#[macro_export]
355#[deprecated]
356macro_rules! rust_type {
357    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
358}
359
360/// See [`extern_rust::extern_rust_type`].
361#[macro_export]
362macro_rules! extern_rust_type {
363    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
364}
365
366/// See [`subclass::subclass`].
367#[macro_export]
368macro_rules! subclass {
369    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
370}
371
372/// Indicates that a C++ type can definitely be instantiated. This has effect
373/// only in a very specific case:
374/// * the type is a typedef to something else
375/// * the 'something else' can't be fully inspected by autocxx, possibly
376///   becaue it relies on dependent qualified types or some other template
377///   arrangement that bindgen cannot fully understand.
378///
379/// In such circumstances, autocxx normally has to err on the side of caution
380/// and assume that some type within the 'something else' is itself a forward
381/// declaration. That means, the opaque typedef won't be storable within
382/// a [`cxx::UniquePtr`]. If you know that no forward declarations are involved,
383/// you can declare the typedef type is instantiable and then you'll be able to
384/// own it within Rust.
385///
386/// The syntax is:
387/// `instantiable!("CppNameGoesHere")`
388///
389/// A directive to be included inside
390/// [include_cpp] - see [include_cpp] for general information.
391#[macro_export]
392macro_rules! instantiable {
393    ($($tt:tt)*) => { $crate::usage!{$($tt)*} };
394}
395
396#[doc(hidden)]
397#[macro_export]
398macro_rules! usage {
399    (__docs) => {};
400    ($($tt:tt)*) => {
401        compile_error! {r#"usage:  include_cpp! {
402                   #include "path/to/header.h"
403                   generate!(...)
404                   generate_pod!(...)
405               }
406"#}
407    };
408}
409
410use std::pin::Pin;
411
412#[doc(hidden)]
413pub use autocxx_macro::include_cpp_impl;
414
415#[doc(hidden)]
416pub use autocxx_macro::cpp_semantics;
417
418macro_rules! ctype_wrapper {
419    ($r:ident, $c:expr, $d:expr) => {
420        #[doc=$d]
421        #[derive(Debug, Eq, Copy, Clone, PartialEq, Hash)]
422        #[allow(non_camel_case_types)]
423        #[repr(transparent)]
424        pub struct $r(pub ::std::os::raw::$r);
425
426        /// # Safety
427        ///
428        /// We assert that the namespace and type ID refer to a C++
429        /// type which is equivalent to this Rust type.
430        unsafe impl cxx::ExternType for $r {
431            type Id = cxx::type_id!($c);
432            type Kind = cxx::kind::Trivial;
433        }
434
435        impl From<::std::os::raw::$r> for $r {
436            fn from(val: ::std::os::raw::$r) -> Self {
437                Self(val)
438            }
439        }
440
441        impl From<$r> for ::std::os::raw::$r {
442            fn from(val: $r) -> Self {
443                val.0
444            }
445        }
446    };
447}
448
449ctype_wrapper!(
450    c_ulonglong,
451    "c_ulonglong",
452    "Newtype wrapper for an unsigned long long"
453);
454ctype_wrapper!(c_longlong, "c_longlong", "Newtype wrapper for a long long");
455ctype_wrapper!(c_ulong, "c_ulong", "Newtype wrapper for an unsigned long");
456ctype_wrapper!(c_long, "c_long", "Newtype wrapper for a long");
457ctype_wrapper!(
458    c_ushort,
459    "c_ushort",
460    "Newtype wrapper for an unsigned short"
461);
462ctype_wrapper!(c_short, "c_short", "Newtype wrapper for an short");
463ctype_wrapper!(c_uint, "c_uint", "Newtype wrapper for an unsigned int");
464ctype_wrapper!(c_int, "c_int", "Newtype wrapper for an int");
465ctype_wrapper!(c_uchar, "c_uchar", "Newtype wrapper for an unsigned char");
466
467/// Newtype wrapper for a C void. Only useful as a `*c_void`
468#[allow(non_camel_case_types)]
469#[repr(transparent)]
470pub struct c_void(pub ::std::os::raw::c_void);
471
472/// # Safety
473///
474/// We assert that the namespace and type ID refer to a C++
475/// type which is equivalent to this Rust type.
476unsafe impl cxx::ExternType for c_void {
477    type Id = cxx::type_id!(c_void);
478    type Kind = cxx::kind::Trivial;
479}
480
481/// A C++ `char16_t`
482#[allow(non_camel_case_types)]
483#[repr(transparent)]
484pub struct c_char16_t(pub u16);
485
486/// # Safety
487///
488/// We assert that the namespace and type ID refer to a C++
489/// type which is equivalent to this Rust type.
490unsafe impl cxx::ExternType for c_char16_t {
491    type Id = cxx::type_id!(c_char16_t);
492    type Kind = cxx::kind::Trivial;
493}
494
495/// autocxx couldn't generate these bindings.
496/// If you come across a method, type or function which refers to this type,
497/// it indicates that autocxx couldn't generate that binding. A documentation
498/// comment should be attached indicating the reason.
499#[allow(dead_code)]
500pub struct BindingGenerationFailure {
501    _unallocatable: [*const u8; 0],
502    _pinned: core::marker::PhantomData<core::marker::PhantomPinned>,
503}
504
505/// Tools to export Rust code to C++.
506// These are in a mod to avoid shadowing the definitions of the
507// directives above, which, being macro_rules, are unavoidably
508// in the crate root but must be function-style macros to keep
509// the include_cpp impl happy.
510pub mod extern_rust {
511
512    /// Declare that this is a Rust type which is to be exported to C++.
513    /// You can use this in two ways:
514    /// * as an attribute macro on a Rust type, for instance:
515    ///   ```
516    ///   # use autocxx_macro::extern_rust_type as extern_rust_type;
517    ///   #[extern_rust_type]
518    ///   struct Bar;
519    ///   ```
520    /// * as a directive within the [include_cpp] macro, in which case
521    ///   provide the type path in brackets:
522    ///   ```
523    ///   # use autocxx_macro::include_cpp_impl as include_cpp;
524    ///   include_cpp!(
525    ///   #   parse_only!()
526    ///       #include "input.h"
527    ///       extern_rust_type!(Bar)
528    ///       safety!(unsafe)
529    ///   );
530    ///   struct Bar;
531    ///   ```
532    /// These may be used within references in the signatures of C++ functions,
533    /// for instance. This will contribute to an `extern "Rust"` section of the
534    /// generated `cxx` bindings, and this type will appear in the C++ header
535    /// generated for use in C++.
536    ///
537    /// # Finding these bindings from C++
538    ///
539    /// You will likely need to forward-declare this type within your C++ headers
540    /// before you can use it in such function signatures. autocxx can't generate
541    /// headers (with this type definition) until it's parsed your header files;
542    /// logically therefore if your header files mention one of these types
543    /// it's impossible for them to see the definition of the type.
544    ///
545    /// If you're using multiple sets of `include_cpp!` directives, or
546    /// a mixture of `include_cpp!` and `#[cxx::bridge]` bindings, then you
547    /// may be able to `#include "cxxgen.h"` to refer to the generated C++
548    /// function prototypes. In this particular circumstance, you'll want to know
549    /// how exactly the `cxxgen.h` header is named, because one will be
550    /// generated for each of the sets of bindings encountered. The pattern
551    /// can be set manually using `autocxxgen`'s command-line options. If you're
552    /// using `autocxx`'s `build.rs` support, those headers will be named
553    /// `cxxgen.h`, `cxxgen1.h`, `cxxgen2.h` according to the order in which
554    /// the `include_cpp` or `cxx::bridge` bindings are encountered.
555    pub use autocxx_macro::extern_rust_type;
556
557    /// Declare that a given function is a Rust function which is to be exported
558    /// to C++. This is used as an attribute macro on a Rust function, for instance:
559    /// ```
560    /// # use autocxx_macro::extern_rust_function as extern_rust_function;
561    /// #[extern_rust_function]
562    /// pub fn call_me_from_cpp() { }
563    /// ```
564    ///
565    /// See [`extern_rust_type`] for details of how to find the generated
566    /// declarations from C++.
567    pub use autocxx_macro::extern_rust_function;
568}
569
570/// Equivalent to [`std::convert::AsMut`], but returns a pinned mutable reference
571/// such that cxx methods can be called on it.
572pub trait PinMut<T>: AsRef<T> {
573    /// Return a pinned mutable reference to a type.
574    fn pin_mut(&mut self) -> std::pin::Pin<&mut T>;
575}
576
577/// Provides utility functions to emplace any [`moveit::New`] into a
578/// [`cxx::UniquePtr`]. Automatically imported by the autocxx prelude
579/// and implemented by any (autocxx-related) [`moveit::New`].
580pub trait WithinUniquePtr {
581    type Inner: UniquePtrTarget + MakeCppStorage;
582    /// Create this item within a [`cxx::UniquePtr`].
583    fn within_unique_ptr(self) -> cxx::UniquePtr<Self::Inner>;
584}
585
586/// Provides utility functions to emplace any [`moveit::New`] into a
587/// [`Box`]. Automatically imported by the autocxx prelude
588/// and implemented by any (autocxx-related) [`moveit::New`].
589pub trait WithinBox {
590    type Inner;
591    /// Create this item inside a pinned box. This is a good option if you
592    /// want to own this object within Rust, and want to create Rust references
593    /// to it.
594    fn within_box(self) -> Pin<Box<Self::Inner>>;
595    /// Create this item inside a [`CppPin`]. This is a good option if you
596    /// want to own this option within Rust, but you want to create [`CppRef`]
597    /// C++ references to it. You'd only want to choose that option if you have
598    /// enabled the C++ reference wrapper support by using the
599    /// `safety!(unsafe_references_wrapped`) directive. If you haven't done
600    /// that, ignore this function.
601    fn within_cpp_pin(self) -> CppPin<Self::Inner>;
602}
603
604use cxx::kind::Trivial;
605use cxx::ExternType;
606use moveit::Emplace;
607use moveit::MakeCppStorage;
608
609impl<N, T> WithinUniquePtr for N
610where
611    N: New<Output = T>,
612    T: UniquePtrTarget + MakeCppStorage,
613{
614    type Inner = T;
615    fn within_unique_ptr(self) -> cxx::UniquePtr<T> {
616        UniquePtr::emplace(self)
617    }
618}
619
620impl<N, T> WithinBox for N
621where
622    N: New<Output = T>,
623{
624    type Inner = T;
625    fn within_box(self) -> Pin<Box<T>> {
626        Box::emplace(self)
627    }
628    fn within_cpp_pin(self) -> CppPin<Self::Inner> {
629        CppPin::from_pinned_box(Box::emplace(self))
630    }
631}
632
633/// Emulates the [`WithinUniquePtr`] trait, but for trivial (plain old data) types.
634/// This allows such types to behave identically if a type is changed from
635/// `generate!` to `generate_pod!`.
636///
637/// (Ideally, this would be the exact same trait as [`WithinUniquePtr`] but this runs
638/// the risk of conflicting implementations. Negative trait bounds would solve
639/// this!)
640pub trait WithinUniquePtrTrivial: UniquePtrTarget + Sized + Unpin {
641    fn within_unique_ptr(self) -> cxx::UniquePtr<Self>;
642}
643
644impl<T> WithinUniquePtrTrivial for T
645where
646    T: UniquePtrTarget + ExternType<Kind = Trivial> + Sized + Unpin,
647{
648    fn within_unique_ptr(self) -> cxx::UniquePtr<T> {
649        UniquePtr::new(self)
650    }
651}
652
653/// Emulates the [`WithinBox`] trait, but for trivial (plain old data) types.
654/// This allows such types to behave identically if a type is changed from
655/// `generate!` to `generate_pod!`.
656///
657/// (Ideally, this would be the exact same trait as [`WithinBox`] but this runs
658/// the risk of conflicting implementations. Negative trait bounds would solve
659/// this!)
660pub trait WithinBoxTrivial: Sized + Unpin {
661    fn within_box(self) -> Pin<Box<Self>>;
662}
663
664impl<T> WithinBoxTrivial for T
665where
666    T: ExternType<Kind = Trivial> + Sized + Unpin,
667{
668    fn within_box(self) -> Pin<Box<T>> {
669        Pin::new(Box::new(self))
670    }
671}
672
673use cxx::memory::UniquePtrTarget;
674use cxx::UniquePtr;
675use moveit::New;
676pub use rvalue_param::RValueParam;
677pub use rvalue_param::RValueParamHandler;
678pub use value_param::as_copy;
679pub use value_param::as_mov;
680pub use value_param::as_new;
681pub use value_param::ValueParam;
682pub use value_param::ValueParamHandler;
683
684/// Imports which you're likely to want to use.
685pub mod prelude {
686    pub use crate::as_copy;
687    pub use crate::as_mov;
688    pub use crate::as_new;
689    pub use crate::c_int;
690    pub use crate::c_long;
691    pub use crate::c_longlong;
692    pub use crate::c_short;
693    pub use crate::c_uchar;
694    pub use crate::c_uint;
695    pub use crate::c_ulong;
696    pub use crate::c_ulonglong;
697    pub use crate::c_ushort;
698    pub use crate::c_void;
699    pub use crate::cpp_semantics;
700    pub use crate::include_cpp;
701    pub use crate::AsCppMutRef;
702    pub use crate::AsCppRef;
703    pub use crate::CppMutRef;
704    pub use crate::CppPin;
705    pub use crate::CppRef;
706    pub use crate::CppUniquePtrPin;
707    pub use crate::PinMut;
708    pub use crate::RValueParam;
709    pub use crate::ValueParam;
710    pub use crate::WithinBox;
711    pub use crate::WithinBoxTrivial;
712    pub use crate::WithinUniquePtr;
713    pub use crate::WithinUniquePtrTrivial;
714    pub use cxx::UniquePtr;
715    pub use moveit::moveit;
716    pub use moveit::new::New;
717    pub use moveit::Emplace;
718}
719
720/// Re-export moveit for ease of consumers.
721pub use moveit;
722
723/// Re-export cxx such that clients can use the same version as
724/// us. This doesn't enable clients to avoid depending on the cxx
725/// crate too, unfortunately, since generated cxx::bridge code
726/// refers explicitly to ::cxx. See
727/// <https://github.com/google/autocxx/issues/36>
728pub use cxx;