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

Provide #[archive(as = "...")] for users to provide their own archive types #128

Closed
djkoloski opened this issue May 6, 2021 · 7 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@djkoloski
Copy link
Collaborator

With the changes around ArchiveCopy (#119) the idea of a type being archiveable as itself needs to be changed too.

Adding #[archive(self)] to a type should:

  • Add T: Archive<Archived = Self> bounds for each of the fields
  • Error on any uses of #[archive_attr] or other attributes except #[archive(copy_safe)]
  • Ensure that enums have an appropriate repr
  • Not generate an archived version of the type
@djkoloski djkoloski added the enhancement New feature or request label May 6, 2021
@djkoloski djkoloski added this to the v0.7 milestone May 6, 2021
@djkoloski djkoloski self-assigned this May 6, 2021
@djkoloski
Copy link
Collaborator Author

Also provide the syntax #[archive(self = "...")] for these kinds of cases:

#[archive(self = "Self<T::Archived>")]
struct Example<T>(T);

Which currently generate an ArchivedExample<T> when they could just archive to Example<Archived<T>>. This conflicts with the bound T: Archive<Archived = Self> but I think it will be okay once project_struct gets switched to ptr::addr_of_mut because it will still cause a compiler error.

@djkoloski
Copy link
Collaborator Author

This should probably be reworked into #[archive(as = "Self")] and #[archive(as = "Example<T::Archived>")].

@djkoloski djkoloski changed the title Change #[archive(copy)] to #[archive(self)] Provide #[archive(as = "...")] for users to provide their own archive types May 6, 2021
@djkoloski
Copy link
Collaborator Author

Fixed by 818e9f0

@dvc94ch
Copy link

dvc94ch commented Jun 20, 2021

Fantastic, I was just looking for how to do this!

@dvc94ch
Copy link

dvc94ch commented Jun 20, 2021

I wonder if there is a way to implement AsRef<[u8]> for types that are #[archive(as = "Self")]?

@djkoloski
Copy link
Collaborator Author

You can implement AsRef the same as you normally would, this parameter just swaps out the generated archived struct for the one you specify. So if you implement AsRef<[u8]> for a struct, it will archive as itself with #[archive(as = "Self")].

@dvc94ch
Copy link

dvc94ch commented Jun 20, 2021

Yes, using the zerocopy crate to derive AsBytes for simple structs.

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

No branches or pull requests

2 participants