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

Add ArrayVecCopy #280

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Add ArrayVecCopy #280

wants to merge 9 commits into from

Conversation

tbu-
Copy link
Collaborator

@tbu- tbu- commented Oct 17, 2024

Also add infrastructure to generate src/arrayvec_copy.rs. It works by replacing a couple of strings in src/arrayvec.rs.

Check that the transformation is up-to-date in CI.

Fixes #32.
Closes #261.

@bluss
Copy link
Owner

bluss commented Oct 17, 2024

hi, intriguing. I'm skeptical of the maintainability, but it could work, it's a nice accomplishment and not a bad flow you have set up. The more sed patterns needed the worse of a solution this is, I guess?
In particular it's interesting how hard it will be to add something to ArrayVec after this change.

@tbu- tbu- force-pushed the pr_arrayvec_copy branch 2 times, most recently from 03fb484 to abf40d5 Compare October 17, 2024 20:50
It works by replacing a couple of strings in `src/arrayvec.rs` and then
applying a small patch.

Check that the patch is up-to-date in CI.
They don't work on Rust 1.51

```
error[E0723]: trait bounds other than `Sized` on const fn parameters are unstable
  --> src/arrayvec_copy.rs:59:6
   |
59 | impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
   |      ^
   |
   = note: see issue #57563 <rust-lang/rust#57563> for more information
   = help: add `#![feature(const_fn)]` to the crate attributes to enable
```
@tbu-
Copy link
Collaborator Author

tbu- commented Oct 17, 2024

The more sed patterns needed the worse of a solution this is, I guess?

Yeah. :( I wanted to document the sed patterns but couldn't find a way to do that easily.

They are, in that order:
1: rename ArrayVecArrayVecCopy
2: rename ArrayVecVisitorArrayVecCopyVisitor
3, 4: transform impl<T> to impl<T: Copy>
5, 6: transform struct Name<T> to struct Name<T: Copy>
7, 8: transform fn name<T> to fn name<T: Copy>
9: change all const fn to normal fn (see last commit).

@tbu-
Copy link
Collaborator Author

tbu- commented Oct 17, 2024

In particular it's interesting how hard it will be to add something to ArrayVec after this change.

Theoretically, it should be as simple as making the change, then deleting src/arrayvec_copy.rs and running ./generate_arrayvec_copy. If you don't do that, you'll get an error in the check-generated CI check, but everything else will continue to work.

@bluss
Copy link
Owner

bluss commented Oct 19, 2024

It's promising.

I'm suggesting this creative way of not having to have a patch at all. Instead insert our own directives into the code using comments, and use cfg(never_true) to remove some items from ArrayVecCopy.

https://github.com/tbu-/arrayvec/pull/2

@bluss bluss mentioned this pull request Oct 19, 2024
Invent this little hack, insert directives like this in ArrayVec:

```rust
// DIRECTIVE ArrayVecCopy #[cfg(not_in_arrayvec_copy)]
```

And replace them as follows in ArrayVecCopy:

```rust
 #[cfg(not_in_arrayvec_copy)]
```
It's a type nested in a function, does not need renaming.
@bluss
Copy link
Owner

bluss commented Oct 21, 2024

It all came together very nicely. How to add Copy-specific features is going to be figured out when that bridge needs to be crossed.

I think the PR is really good now except that ArrayVecCopy is not actually Copy 😆 which I hope we take with some humor. How do I know, well I'm just adding a basic test for that before merging..

I'm pushing a fix for that and a basic test now.

@bluss
Copy link
Owner

bluss commented Oct 21, 2024

Have to think more about the module structure. Maybe ArrayVecCopy should just live inside arrayvec::copy. It would be somewhat consistent when the iterator structs need to have that namespace anyway.

@tbu-
Copy link
Collaborator Author

tbu- commented Oct 22, 2024

I think the PR is really good now except that ArrayVecCopy is not actually Copy 😆 which I hope we take with some humor.

Oh my god…

How do I know, well I'm just adding a basic test for that before merging..

Thanks for remembering to add tests, which I conveniently forgot…

Maybe ArrayVecCopy should just live inside arrayvec::copy.

It'd make it more inconvenient to import ArrayVecCopy. The standard library also exports HashMap inside of std::collections (but also in std::collections::hash). I skipped exporting ArrayVecCopy in arrayvec::copy so that we have canonical import paths.

@bluss
Copy link
Owner

bluss commented Oct 22, 2024

Maybe the editing here is missing "the outside perspective from cargo doc" part of the review. Generate doc, look at it, we see stuff we need to add -> module doc for copy and main module doc needs to mention ArrayVecCopy because it's strange if it doesn't.

I'm still leaning towards that ArrayVecCopy should be down in its copy module, it's more consistent that way, and we have the chance to do it right now. copy is such a short name it feels easy to do.

@tbu-
Copy link
Collaborator Author

tbu- commented Oct 23, 2024

I'm still leaning towards that ArrayVecCopy should be down in its copy module, it's more consistent that way, and we have the chance to do it right now. copy is such a short name it feels easy to do.

Can you explain what your proposed module structure is?

  1. Everything related to ArrayVecCopy in the copy module and nothing outside of it.
  2. Everything related to ArrayVecCopy in the copy module but also ArrayVecCopy in the root.
  3. Everything related to ArrayVecCopy in the copy module except ArrayVecCopy, which is solely in the root.

If I understand you correctly, you propose 1. I implemented it as 3.

I think the choice of 1 would lead to more (duplicate!) typing because the name specifies "copy" twice: arrayvec::copy::ArrayVecCopy. Perhaps we should instead call it arrayvec::copy::ArrayVec in this case?

I'd prefer 2 or 3 over 1, I think the standard library does 2, and I'd do 3 so that we only have one possible import path for ArrayVecCopy.

@bluss
Copy link
Owner

bluss commented Oct 29, 2024

I would prefer (1) or (2). I think (1) is on balance the better choice? I don't mind the duplicate Copy at all, I think it should be there.

@tbu-
Copy link
Collaborator Author

tbu- commented Oct 29, 2024

Implemented (1).

@tbu-
Copy link
Collaborator Author

tbu- commented Oct 29, 2024

This is asymmetrical towards ArrayString, that's also not in a submodule.

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 this pull request may close these issues.

Feature Request: ArrayVec that is Copy
3 participants