smessmer / binary-layout

The binary-layout library allows type-safe, inplace, zero-copy access to structured binary data. You define a custom data layout and give it a slice of binary data, and it will allow you to read and write the fields defined in the layout from the binary data without having to copy any of the data. It's similar to transmuting to/from a #[repr(packed)] struct, but much safer.
Apache License 2.0
66 stars 9 forks source link

Blanket `impl LayoutAs<T> for T`? #10

Closed ckaran closed 2 years ago

ckaran commented 2 years ago

This is more of mathematical pedantry than anything else. Feel free to take it or leave it as you wish.

For any type T, we can always lay it out as T. This isn't a particularly useful operation, but it may make derive macros simpler (maybe). Would it make sense to make a blanket implementation for all T like the following:

impl LayoutAs<T> for T {
    fn read(v: T) -> T;
    fn write(v: T) -> T;
}
smessmer commented 2 years ago

Not sure if it makes sense since I can't see a use case and it would add some complexity, but I don't have a strong opinion. If you want to implement it, I can merge it.

ckaran commented 2 years ago

Like I said, it's only mathematical pedantry, and possibly support for derive macros that are written outside of binary-layout. I'll toss it in once I get this other PR worked out.

ckaran commented 2 years ago

Yeah, so, this didn't work. Lots and lots of errors in compilation, followed by more and more hacking, turning the nice, clean code into an ugly, hacky mess. So I'm going to close this feature request without a PR, and pretend like I never thought of it...

smessmer commented 2 years ago

Ok

On October 22, 2021 12:44:56 PM Cem Karan @.***> wrote:

Closed #10. — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.