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

Backend wrapper restricted by an explicit allow-list #113

Open
plaidfinch opened this issue Jun 2, 2021 · 0 comments
Open

Backend wrapper restricted by an explicit allow-list #113

plaidfinch opened this issue Jun 2, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@plaidfinch
Copy link
Contributor

plaidfinch commented Jun 2, 2021

A backend wrapper could be created that is parameterized by an allow-list which statically prevents things from being sent/received that shouldn't be.

First, this setup would let you define a new marker type and enumerate a specific set of types which are allowed on the "allow list" denoted by that type.

/// A marker type standing in for allowing every type to be serialized/deserialized.
pub struct AllowAll;

/// A marker trait that indicates whether or not the type `T` belongs to the allow-list `Self`.
pub trait Allow<T> {}

/// The `AllowAll` marker type allows all types.
impl<T> Allow<T> for AllowAll {}

You would use it like so:

pub struct OnlyByteAndBool;

impl Allow<u8> for OnlyByteAndBool {}
impl Allow<bool> for OnlyByteAndBool {}

Because of trait coherence rules, nobody outside the crate in which OnlyByteAndBool can add another impl for OnlyByteAndBool: Allow<T> for some new T.

Then, this abstraction can be used to implement a general restriction mechanism for backends:

#[repr(transparent)]
struct Restricted<T, List = AllowAll>{
    inner: T,
    list: PhantomData<fn() -> List>,
}

Then, we would implement all the backend traits, forwarding their implementations, for Restricted, except adding the clause where List: Allow<T> to each impl.

Edited from the comment posted by @kwf in #107 (comment)

@plaidfinch plaidfinch self-assigned this Jun 2, 2021
@plaidfinch plaidfinch added the enhancement New feature or request label Jun 2, 2021
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

1 participant