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

IntoCtx and FromCtx for () #105

Open
Javagedes opened this issue Sep 12, 2024 · 2 comments
Open

IntoCtx and FromCtx for () #105

Javagedes opened this issue Sep 12, 2024 · 2 comments

Comments

@Javagedes
Copy link

Javagedes commented Sep 12, 2024

Hello, I was wondering if there was a particular reason why () does not implement IntoCtx and FromCtx?

I've created a situation where I have a trait with an associated type that is required to be <Try>IntoCtx, but () would also be valid in my use-case (The associated type is used as the return type of one of the trait functions). I wanted to see if you would be open to adding the trait impls for () or if I should take a different approach. I'd be happy to add the impls if you have no issue with them being added.

Let me know if you need any additional context.

Thanks!

@m4b
Copy link
Owner

m4b commented Oct 25, 2024

Sorry for late reply; I think () hasn't been implemented because scroll's roots were in effectively deserializing out simple structs like you'd see in binary formats like elf, pe, etc. In those cases, () doesn't have a clear meaning.

I guess I'm also trying to say it never really came up, but also, it avoids scroll having an "opinion" on serialization (though of course it does).

That being said, what do you propose:

let mut offset = 0;
bytes.gwrite::<()>(&mut offset)?

the value of offset is here; I assume you will say 0?

@m4b
Copy link
Owner

m4b commented Oct 25, 2024

So I ask, because that's a reasonable thing for a rust programmer to say, for example, because zero-sized types are a thing in rust and other languages, but for example, in C, an empty struct has a size of 1, e.g. this prints 1:

#include<assert.h>
#include<stdio.h>

struct Foo {
};

int main() {
    assert(sizeof(Foo) == 1);
    printf("%zu", sizeof(Foo));
    return 0;
}

However, I think it's probably safe to say that scroll already supports 0-sized structs, e.g., if i derive on an empty struct I assume it would gwrite nothing, and offset would be 0.

So I guess that's a roundabout way of saying I think it should be fine to add these implementations?

Another random thing, will this fail?

let mut bytes: [u8; 0] = [];
bytes.pwrite::<()>(0)?;

One could argue it's attempting to write at the first byte offset, i.e., 0, but since it won't ever write anything it shouldn't fail 🤷

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