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

Extract alloc feature (2.0 candidate) #281

Open
6 tasks
Tracked by #279
dj8yfo opened this issue Feb 26, 2024 · 1 comment
Open
6 tasks
Tracked by #279

Extract alloc feature (2.0 candidate) #281

dj8yfo opened this issue Feb 26, 2024 · 1 comment

Comments

@dj8yfo
Copy link
Collaborator

dj8yfo commented Feb 26, 2024

While working on an embedded app, it was discovered, that it's not possible to use borsh crate when global_allocator is not defined.
One possible solution in such cases is to define global_allocator , another is to extract alloc feature and to use borsh with default_features = false .

std = ["alloc"]
# Provide impls for types in the Rust core allocation and collections library
# including String, Box<T>, Vec<T>, and Cow<T>. This is a subset of std but may
# be enabled without depending on all of std.
alloc = []

Below is superficial description of changes required:

  • extract everything about alloc crate into separate feature
    • change borsh/src/nostd_io.rs or make 2 variants, depending on alloc feature
    • make BorshDeserialize trait method vec_from_reader depending on alloc feature
    • each usage of std::io::Error::new and borsh::io::Error::new implies depending on alloc feature, there's around 50 occurencies of it in borsh
    • no change required to BorshSerialize
    • make unstable__schema feature imply alloc feature
// from
enum Repr {
    Simple(ErrorKind),
    Custom(Custom),
}

impl Error {
    pub fn new<T: Into<String>>(kind: ErrorKind, error: T) -> Error {
        Self::_new(kind, error.into())
    }

    fn _new(kind: ErrorKind, error: String) -> Error {
        Error {
            repr: Repr::Custom(Custom { kind, error }),
        }
    }
}
// to
enum Repr {
    Simple(ErrorKind),
}
impl Error {
  
}

For reference an example of replicating a subset of borsh without dependency on alloc crate is provided.

@dj8yfo dj8yfo mentioned this issue Feb 26, 2024
4 tasks
@dj8yfo
Copy link
Collaborator Author

dj8yfo commented Jun 24, 2024

original motivation to create this issue was kind of resolved by LedgerHQ/ledger-device-rust-sdk#155, but the heap on the device was still not quite usable on the device with smallest capabilities, as mentioned in
comment , EDIT: support of which (smaller devices) was removed later

With the mentioned tiniest target platform being a concrete example of environment imposing specific requirements, most likely an endeavor to resolve this issue will only make sense together with changing the signature of BorshDeserialize:

fn deserialize(...) -> Result<Self>;
to 
fn deserialize(..., &mut self) -> Result<()>;

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

No branches or pull requests

1 participant