Skip to content

Support generic NonZero<T> #2255

Closed as not planned
Closed as not planned
@carlsverre

Description

@carlsverre

What is the name of your project?
An unreleased but in-development storage engine that will power SQLSync v2.

Please provide a link to your project (GitHub repository, crates.io page, etc).
https://github.com/orbitinghail/sqlsync
https://github.com/orbitinghail

What features would you like from zerocopy?
I'd like zerocopy to handle the new generic NonZero<T> type in the same way as it handles other NonZero types. I'm happy to take a crack at this if it's straightforward and doesn't have any soundness errors.

This feature is already listed here #1120 but I'd like to raise it as a more current issue in order to get it resolved (assuming it doesn't have any soundness issues).

My current workaround is definining a type like so:

// this is my base type
#[derive(...zerocopy derives...)]
#[repr(transparent)]
pub struct LSN(NonZero<u64>);

// this is my workaround type
#[derive(...zerocopy derives...)]
#[repr(transparent)]
pub struct MaybeLSN(Option<NonZero<u64>>);

impl From<MaybeLSN> for Option<LSN> {
    fn from(value: MaybeLSN) -> Self {
        value.0.map(LSN)
    }
}

impl From<Option<LSN>> for MaybeLSN {
    fn from(value: Option<LSN>) -> Self {
        Self(value.map(|lsn| lsn.0))
    }
}

The downside of the above is an additional wrapping/unwrapping layer and a lot of generated code. It probably gets compiled out of the actual binary, but it adds unneeded complexity.

Once this issue is resolved, I should be able to replace usages of MaybeLSN with Option<LSN>.

I'm happy to fix this myself if that would be acceptable and someone is available to help check the soundness of the implementation.

Thanks for an AMAZING project btw. I love zerocopy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions