Skip to content

UniquePtr<CxxVector<T>> doesn't work for opaque C++ type T containing non-static borrows #759

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

Closed
dtolnay opened this issue Mar 25, 2021 · 1 comment · Fixed by #760
Closed

Comments

@dtolnay
Copy link
Owner

dtolnay commented Mar 25, 2021

There is a spurious 'static bound on the following UniquePtrTarget impl, which was inconsequential prior to #608, but now is quite limiting. We should find a way to avoid needing that bound.

cxx/src/unique_ptr.rs

Lines 236 to 238 in 3090721

unsafe impl<T> UniquePtrTarget for CxxVector<T>
where
T: VectorElement + 'static,

Example:

use cxx::{CxxVector, UniquePtr};

#[cxx::bridge]
mod ffi {
    extern "C++" {
        include!("example/include/example.h");

        type Thing<'a>;
    }

    impl<'a> CxxVector<Thing<'a>> {}
}

fn f<'a>(_: UniquePtr<CxxVector<ffi::Thing<'a>>>) {}
error[E0477]: the type `Thing<'a>` does not fulfill the required lifetime
  --> src/main.rs:14:13
   |
14 | fn f<'a>(_: UniquePtr<CxxVector<ffi::Thing<'a>>>) {}
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: type must satisfy the static lifetime
@dtolnay
Copy link
Owner Author

dtolnay commented Mar 25, 2021

Simply removing the static bound with no other code change:

error[E0310]: the parameter type `T` may not live long enough
   --> src/unique_ptr.rs:240:42
    |
236 | unsafe impl<T> UniquePtrTarget for CxxVector<T>
    |             - help: consider adding an explicit lifetime bound...: `T: 'static`
...
240 |     const __NAME: &'static dyn Display = &vector::TypeName::<T>::new();
    |                                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `TypeName<T>` will meet its required lifetime bounds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant