Skip to content
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

SmallVec can not coerce lifetimes of wrapped value #217

Open
Luro02 opened this issue Apr 28, 2020 · 0 comments
Open

SmallVec can not coerce lifetimes of wrapped value #217

Luro02 opened this issue Apr 28, 2020 · 0 comments

Comments

@Luro02
Copy link
Contributor

Luro02 commented Apr 28, 2020

I could not get the following code to compile with SmallVec (I heavily simplified the code):

use std::borrow::Cow;

pub struct SmallVec<T> {
    data: *mut T,
}

pub struct Example<'a>(SmallVec<[Cow<'a, str>; 2]>);

impl<'a> Example<'a> {
    pub fn into_owned(self) -> Example<'static> { unimplemented!() }
}

fn transform<'a>(input: Example<'a>) -> Example<'a> {
    input.into_owned()
}

fn main() {}

playground

error[E0308]: mismatched types
  --> src/main.rs:14:5
   |
14 |     input.into_owned()
   |     ^^^^^^^^^^^^^^^^^^ lifetime mismatch
   |
   = note: expected struct `Example<'a>`
              found struct `Example<'static>`
note: the lifetime `'a` as defined on the function body at 13:14...
  --> src/main.rs:13:14
   |
13 | fn transform<'a>(input: Example<'a>) -> Example<'a> {
   |              ^^
   = note: ...does not necessarily outlive the static lifetime

error: aborting due to previous error

which does compile when you replace SmallVec with Vec.

This works with Vec, because internally the pointer is wrapped in Unique<T>:

https://github.com/rust-lang/rust/blob/fb5615a4771ea3d54256f969dc84d2dfd38d812c/src/liballoc/raw_vec.rs#L47

whereas SmallVec contains a raw pointer:

Heap((*mut A::Item, usize)),

This issue can be solved by changing *mut A::Item to Option<NonNull<A::Item>>, because NonNull implements the CoerceUnsized trait.

This might be related to #146

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

No branches or pull requests

1 participant