Skip to content

BitStringRef that works with Cow #2298

@tarcieri

Description

@tarcieri

In #1998 the lifetime was removed from OctetStringRef by making it a slice newtype, which to simplify looks something like pub struct OctetStringRef([u8]), and in practice used as &OctetStringRef.

What's really nice about this it means we can impl Borrow<OctetStringRef> for OctetString and ToOwned for OctetStringRef, and with that we can use Cow<OctetStringRef>.

We discussed several times whether it would be possible to do this for BitStringRef too and at the time I couldn't figure out a way. I looked into trying to put the unused bits in a tagged pointer but that won't work since we need to construct them from &[u8]. However the other place we can stick the information is in the length part of the fat pointer by using it to store the length in bits instead of bytes.

Concretely that might look like pub struct BitStringRef([UnsafeCell<()>]) where the length of that inner slice represents a number of bits.

Here's a POC of the idea:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=e162511e707fe74c8f9f7731972acd32

cc @baloo @dishmaker

(Thanks to #dark-arts for some help here)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions