Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Use with slices? #7

Open
rw opened this issue May 19, 2020 · 8 comments
Open

Use with slices? #7

rw opened this issue May 19, 2020 · 8 comments

Comments

@rw
Copy link

rw commented May 19, 2020

Is there a good way to use this with slices? I see that there are tests for arrays. Maybe by asserting the length at runtime, this becomes safe?

I'm interested in using typic to unsafe code in flatbuffers (some UB that was reported recently, so I'm on a hunt to remove all unsafe google/flatbuffers#5913).

@joshlf
Copy link

joshlf commented May 19, 2020

Hi @rw, have you seen the zerocopy crate? It currently supports slices. I'm the author, and I am hoping that typic will get to the point of supporting slices (and eventually take over zerocopy entirely), but for the time being, if you need something immediately, I'd check it out.

@rw
Copy link
Author

rw commented May 19, 2020

@joshlf Very cool, thank you!

@cwhan-yonsei
Copy link

Is there any update regarding the topic? I need would like to transmute some struct to byte array and construct it from by trade for serial communication. is the zerocopy the only solution at this point?

@joshlf
Copy link

joshlf commented Dec 14, 2022

I don't believe typic is under active development. I'd check out the zerocopy or bytemuck crates. If you can describe in a bit more detail what you're trying to do, I can suggest specific APIs to look at.

@cwhan-yonsei
Copy link

cwhan-yonsei commented Dec 15, 2022

I don't believe typic is under active development. I'd check out the zerocopy or bytemuck crates. If you can describe in a bit more detail what you're trying to do, I can suggest specific APIs to look at.

@joshlf
I appreciate your kind suggestion. I'd like to construct a message for SPI communication semantically and also check the received message in a semantic manner. So I end up doing something below with zerocopy crate.

let inc_reading_tx = FullReg {
            cmd_or_dummy_byte: inc_reading_byte,
            adc_data: u24::new(),
           
           ...
           
            reserved2: 0u16,
            crc_cfg: 0u16,
        };

let mut read_buffer = FullReg::new_zeroed();
spi.transfer(read_buffer.as_bytes_mut(), inc_reading_tx.as_bytes())?;

Would there be any equivalent solution with typic? Or would it be correct to use zerocopy in such purpose?

@joshlf
Copy link

joshlf commented Dec 20, 2022

What is the API of the spi.transfer method? Does it read a response from the SPI peripheral into read_buffer.as_mut_bytes()? If so, then yeah I think sticking with zerocopy should work fine for you.

@cwhan-yonsei
Copy link

@joshlf
Yes, Indeed you are right. I used as_byte_mut for the read_buffer and it works as expected. Thank you.

@joshlf
Copy link

joshlf commented Dec 21, 2022

You're welcome!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants