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

Expand slices to separate pointer and size #247

Closed
kvark opened this issue Nov 8, 2018 · 11 comments
Closed

Expand slices to separate pointer and size #247

kvark opened this issue Nov 8, 2018 · 11 comments

Comments

@kvark
Copy link
Contributor

kvark commented Nov 8, 2018

It would be really nice to support slices by splitting those into separate (pointer, size) values, e.g.

   objects: &[Object],

would be translated into:

   objects: *const Object,
   objects_length: size_t,

where the "_length" suffix is configured like all the other prefixes/suffixes

@emilio
Copy link
Collaborator

emilio commented Nov 9, 2018

Can you really rely on that layout and ABI?

@RReverser
Copy link
Contributor

Nope.

@eqrion
Copy link
Collaborator

eqrion commented Nov 9, 2018

We had a discussion about this in the gfx daily. It sounds like this doesn't have a defined layout currently, but might in the future. This is another thing, like strings, where it'd be nice to be able to autogenerate conversion thunks. But there's not a great way to add support for this in cbindgen today.

So I'll close this for now.

@Manishearth
Copy link

Would it be possible for cbindgen to come with a companion library containing wrappers like Slice, etc which are compatible? Otherwise folks have to do this per-project.

@emilio
Copy link
Collaborator

emilio commented Jan 15, 2021

Ideally we'd get rust to specify these and then just teach cbindgen about that.

But lacking them, I'd be fine with something like that. We can put it in this repo if it makes sense.

Gecko had a bunch of types for stuff like Box<str>, Box<[T]>, etc which should be copiable pretty much straight-away. For non-owned slices I think we've been mostly passing ptr+length by hand, but a new type so that it has the right ABI would be cool...

@Manishearth
Copy link

Yeah, to be clear, cbindgen doesn't need any changes itself, it just needs to maintain a second library that works well with cbindgen that does this.

@Manishearth
Copy link

Actually, I didn't realize that cbindgen can't generate types from dependencies at all. That does make things a little bit worse here :/

@emilio
Copy link
Collaborator

emilio commented Jan 15, 2021

It does generate types from dependencies, see parse_deps = true and extra_bindings if you need functions and constants from a dependent crate.

@petrochenkov
Copy link
Contributor

I use the cfg(feature = "cbindgen") trick for this

#[repr(C)]
struct S<'a> {
    #[cfg(not(feature = "cbindgen"))]
    slice: &'a [u8],

    #[cfg(feature = "cbindgen")]
    ptr: *const u8,
    #[cfg(feature = "cbindgen")]
    len: usize,
}

(which is pretty useful for workarounds in general, and helps with other things like bitfields).

@Manishearth
Copy link

ahhh thanks

@emilio
Copy link
Collaborator

emilio commented Jan 15, 2021

Right, but that's still relying on technically unspecified behavior, isn't it? I just want https://github.com/rust-lang/unsafe-code-guidelines/blob/master/reference/src/layout/pointers.md set on stone ;)

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

6 participants