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

Subspace as a const #175

Open
Defman opened this issue Feb 18, 2020 · 4 comments
Open

Subspace as a const #175

Defman opened this issue Feb 18, 2020 · 4 comments

Comments

@Defman
Copy link

Defman commented Feb 18, 2020

Would it be possible to change Supsace such that you are able to initialize it as a const? ie.

const SPACE: &Subspace = &Subspace::from(&"space");

I'm far from an expert, more like a beginner; but from what I'm aware only tuple structs are able to be consts and no allocation is allowed. Since it should be easy to convert any static value into a byte array at compile time.

@Defman
Copy link
Author

Defman commented Feb 18, 2020

pack would also have to be a const fn.

@Speedy37
Copy link
Contributor

Rust doesn't support const fn for complex expressions. pack is far too complex to be marked const fn. There is work in progress to support for expressions, even allocations, but it's far from done and even further away from stable.

The only way to make Subspace "const" right now would be to change it's internals to support a &'static T variant and lazily pack T when necessary.
Either that or change Vec<u8> to Cow<'static, [u8]>, and provided a prepacked const fn cstor.

I'm open to suggestion, feel free to provide a PR, even WIP if you need guidance so we can discuss on a more concrete ground.

An example of your use case might be great too.

@Defman
Copy link
Author

Defman commented Feb 23, 2020

This is definitely an enhancement/ergonomic request. It would improve performance slightly, due to the serialization of any type to byte array would be done at compile time.

fn myspace() -> Subspace {
    Subspace::from(&"myspace");
}

Would become

const MYSPACE: &Subspace  = &Subspace::from(&"myspace");

lazy_static

@Speedy37
Copy link
Contributor

Doing pack at compile is possible, but would requires some complex macro setup to work.
rustc const might be able one day to do it, but I don't think it will be possible for at least a year or two.

I'm wondering about changing the Subspace definition so it can be copy free:

pub struct Subspace<'a> {
    prefix: Cow<'a, [u8]>,
}

Either that or:

pub struct Subspace<'a> {
    prefix: Prefix<'a>,
}
enum Prefix<'a> {
    Owned(Box<[u8]>),
    Borrowed(&'a[u8]),
    Value(&'a dyn TuplePack),
}

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

No branches or pull requests

2 participants