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

How do you use this crate on no_std? #12

Open
fschutt opened this issue Feb 4, 2021 · 1 comment
Open

How do you use this crate on no_std? #12

fschutt opened this issue Feb 4, 2021 · 1 comment

Comments

@fschutt
Copy link

fschutt commented Feb 4, 2021

I've tried to decode some data from a &[u8] into a alloc::vec::Vec<u8>. For some reason, this crate requires std::io::Read - but that trait is not available on no_std. I've tried for the past two hours to decipher the unreadable mess that is this codebase in order to just write a function like this:

#![no_std]
extern crate alloc; // use standard allocator
fn decode_my_data(input: &[u8]) -> Result<alloc::vec::Vec<u8>, BrotliError>

I'm sorry if this comes off as rude, but the code is completely unreadable, there's no documentation and no examples for no_std use-cases, even though it seems that this crate does support no_std (it says so in the project description)? Maybe this is due to this crate being written before certain Rust features were available, but still - if you'd want custom allocators, why not just let users pass in malloc and free function pointers instead of this generic mess:

wtf

Effectively, this crate only compiles on std because the code is too much of a mess. It doesn't seem possible to run the decoding function without the buffer implementing std::io::Read - which is not available on no_std. I tried to port the decode function by copying the implementation in the Read trait, but for some reason the code uses things like input_buffer as an output buffer and does some copy-in-place tricks, which is confusing. Maybe it's possible to run it on no_std, but the code is simply too complex for me to know which traits / types I should use. I'd appreciate any help / examples.

@danielrh
Copy link
Collaborator

danielrh commented Feb 12, 2021

Hello--
you can't use the Read interface with no_std, so you have two choices
a) to fall back to the more basic C-like interface (or the C interface itself)
b) to use the CustomIO implementations, where you provide the nostd reader

match brotli_decompressor::BrotliDecompressCustomIo(&mut r, // pass in the Read-like reader
                                          &mut w, // pass in the output Write-like writer
                                          &mut alloc_u8.alloc_cell(buffer_size).slice_mut(), // pass in the input and output fixed buffers 
                                          &mut alloc_u8.alloc_cell(buffer_size).slice_mut(),
                                          alloc_u8, // pass in some allocators
                                          alloc_u32,
                                          alloc_hc,
                                          Error::new(ErrorKind::UnexpectedEof, "Unexpected EOF")) { // you also need to provide the error type
      Err(e) => Err(e),
      Ok(()) => {
... your data has been written to the writer
      },
   }

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