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

Implement core::error::Error for ParsingError #27

Closed
jlxip opened this issue Jan 10, 2023 · 1 comment
Closed

Implement core::error::Error for ParsingError #27

jlxip opened this issue Jan 10, 2023 · 1 comment

Comments

@jlxip
Copy link
Contributor

jlxip commented Jan 10, 2023

First, let me say that I have no idea what I'm talking about. I've been learning Rust since the past month or so, so I don't understand the intricate details of it.

I'm writing a kernel and I'm using this project as the ELF parser. My program loader returns a Result with a runtime error type, since it can be either ParsingError or any other thing (such as an issue during page allocation). I can't unwrap() the Result of minimal_parse() since it's not always desirable to throw a kernel panic.

pub fn load(addr: u64, size: usize) -> Result<PID, Box<dyn error::Error>> {
    // ... stuff ...
}

Apparently I can't do:

let file = ElfBytes::<AnyEndian>::minimal_parse(slice)?;

since in !#[no_std] environments, where the std external crate isn't present, but core is, according to this line, the error::Error trait is not implemented, so ParsingError can't downcast to it.

If I understood everything correctly, std::error::Error is just a re-export of core::error::Error, which doesn't really need any std-ish functionality. Would it be possible to substitute the impl of std::error::Error trait for the core one, and thus remove the need for the std feature to be enabled in the crate?

Thanks a lot, and sorry if I'm not understanding something fundamental.

@cole14
Copy link
Owner

cole14 commented Jan 11, 2023

Ahh yes, that seems like what should be happening there. I don't see a reason for the behavior that's implemented today and I think I just overlooked that detail when putting together the no_std support.

Like you suggested, it looks like the impl should be changed to core::error::Error with the cfg line removed on that impl block. If you change that with a local checkout of the elf source, does it let you do what you're hoping for? If so, would you be interested in putting together a PR for this?

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