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

Can one Borrow Vec<T, A> as [T]? #96

Closed
yanchith opened this issue Mar 10, 2022 · 4 comments
Closed

Can one Borrow Vec<T, A> as [T]? #96

yanchith opened this issue Mar 10, 2022 · 4 comments

Comments

@yanchith
Copy link

Hello, my favorite Rust WG :))

At least as of nightly-2022-02-20, Vec<T, A> does not borrow as [T], only Vec<T, Global> does.

From src/alloc/slice.rs:

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Borrow<[T]> for Vec<T> {
    fn borrow(&self) -> &[T] {
        &self[..]
    }
}

Which I guess could be:

#[stable(feature = "rust1", since = "1.0.0")]
impl<T, A: Allocator> Borrow<[T]> for Vec<T, A> {
    fn borrow(&self) -> &[T] {
        &self[..]
    }
}

Is this just a missing feature, or is there something fundamental afoot here?

(Need this for querying a hashmap with keys that are String<A> Vec<u8, A>... btw anything I can do to make String<A> happen?)

@Lokathor
Copy link

At the language level, yes this is absolutely possible. A slice has nothing to do with the allocator its memory might have come from, so Borrow can be generic over all allocators.

The fact that it isn't written this way yet is just that the standard library hasn't developed enough.

@SimonSapin
Copy link
Contributor

The implementation is based on Index<RangeFull>, which is implemented for all allocator types:

https://doc.rust-lang.org/1.59.0/src/alloc/vec/mod.rs.html#2515-2522

impl<T, I: SliceIndex<[T]>, A: Allocator> Index<I> for Vec<T, A> {

So I would expect no issue with your proposed changed. Feel free to send a PR.

@yanchith
Copy link
Author

Thanks! I haven't done any PRs to rust-lang/rust yet, but I'll try and make it happen in the near future :)

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 16, 2022
…r=Mark-Simulacrum

Borrow Vec<T, A> as [T]

Hello all,

When `Vec` was parametrized with `A`, the `Borrow` impls were omitted and currently `Vec<T, A>` can't be borrowed as `[T]`. This PR fixes that.

This was probably missed, because the `Borrow` impls are in a different file - `src/alloc/slice.rs`.

We briefly discussed this here: rust-lang/wg-allocators#96 and I was told to go ahead and make a PR :)

I tested this by building the toolchain and building my code that needed the `Borrow` impl against it, but let me know if I should add any tests to this PR.
@yanchith
Copy link
Author

This got merged a few hours ago, closing

workingjubilee pushed a commit to tcdi/postgrestd that referenced this issue Sep 15, 2022
…mulacrum

Borrow Vec<T, A> as [T]

Hello all,

When `Vec` was parametrized with `A`, the `Borrow` impls were omitted and currently `Vec<T, A>` can't be borrowed as `[T]`. This PR fixes that.

This was probably missed, because the `Borrow` impls are in a different file - `src/alloc/slice.rs`.

We briefly discussed this here: rust-lang/wg-allocators#96 and I was told to go ahead and make a PR :)

I tested this by building the toolchain and building my code that needed the `Borrow` impl against it, but let me know if I should add any tests to this PR.
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

3 participants