This constructor maps to `String::from_utf8_lossy` or
`String::from_utf16_lossy`. It's useful in situations where producing a
slightly garbled string on invalid Unicode data is preferable to
crashing the process, e.g. when passing error messages from C++ to Rust.
Based on the model of #951, which added `Vec<T>::clear`. The `truncate`
method is the more general form of clear.
(If we wanted to, we could delete the binding for `clear` and implement
`clear` as a call to `truncate(0)` on the C++ side, but seemed worth
leaving in in case the Rust `clear` implementation one day gets
smarter.)
Starting with nightly-2021-07-20, this test annoyingly produces
different diagnostics depending on whether the `rust-src` Rustup
component is installed, which is annoying for contributors running the
ui test suite locally. It is not super high value to have this test.
This currently fails to compile.
error: `extern` block uses type `String`, which is not FFI-safe
--> tests/ffi/lib.rs:329:34
|
329 | impl CxxVector<SharedString> {}
| ^^ not FFI-safe
|
note: the lint level is defined here
--> tests/ffi/lib.rs:23:1
|
23 | #[cxx::bridge(namespace = "tests")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: consider adding a `#[repr(C)]` or `#[repr(transparent)]` attribute to this struct
= note: this struct has unspecified layout
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
Currently fails to build.
error[E0261]: use of undeclared lifetime name `'a`
--> tests/ffi/lib.rs:162:59
|
23 | #[cxx::bridge(namespace = "tests")]
| - lifetime `'a` is missing in item created through this procedural macro
...
162 | fn c_take_callback_ref_lifetime<'a>(callback: fn(&'a String));
| ^^ undeclared lifetime
|
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
= help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes
help: consider making the type lifetime-generic with a new `'a` lifetime
|
162 | fn c_take_callback_ref_lifetime<'a>(callback: for<'a> fn(&'a String));
| ^^^^^^^
warning: writing `&String` instead of `&str` involves a new object where a slice will do
--> tests/test.rs:207:24
|
207 | fn callback_ref(s: &String) {
| ^^^^^^^ help: change this to: `&str`
|
= note: `#[warn(clippy::ptr_arg)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#ptr_arg
Currently fails to build.
In file included from cxx-test-suite-32b8d00ca9482f5f/out/cxxbridge/crate/tests/ffi/tests.h:2,
from tests.cc:1:
cxx-test-suite-32b8d00ca9482f5f/out/cxxbridge/include/rust/cxx.h: In instantiation of ‘Ret rust::cxxbridge1::Fn<Ret(Args ...)>::operator()(Args ...) const [with Ret = void; Args = {rust::cxxbridge1::String&}]’:
tests.cc:504:18: required from here
cxx-test-suite-32b8d00ca9482f5f/out/cxxbridge/include/rust/cxx.h:478:29: error: cannot bind non-const lvalue reference of type ‘rust::cxxbridge1::String&’ to an rvalue of type ‘std::remove_reference<rust::cxxbridge1::String&>::type’ {aka ‘rust::cxxbridge1::String’}
478 | return (*this->trampoline)(std::move(args)..., this->fn);
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cxx-test-suite-32b8d00ca9482f5f/out/cxxbridge/include/rust/cxx.h:478:58: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
478 | return (*this->trampoline)(std::move(args)..., this->fn);
| ^
Currently fails to build:
error[E0308]: mismatched types
--> tests/ffi/module.rs:18:1
|
18 | #[cxx::bridge(namespace = "tests")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `Pin`, found `&mut _`
|
= note: expected struct `Pin<&mut other::E>`
found mutable reference `&mut _`
= note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)
because we are generating this incorrect code on the Rust side:
pub fn c_return_opaque_mut_pin(e: ::std::pin::Pin<&mut E>) -> ::std::pin::Pin<&mut E> {
extern "C" {
#[link_name = "tests$cxxbridge1$c_return_opaque_mut_pin"]
fn __c_return_opaque_mut_pin(e: *mut ::std::ffi::c_void)
-> *mut ::std::ffi::c_void;
}
unsafe {
&mut *__c_return_opaque_mut_pin(
::std::pin::Pin::into_inner_unchecked(e) as *mut E as *mut ::std::ffi::c_void
)
.cast()
}
}