xref: /DragonOS/kernel/crates/rbpf/src/no_std_error.rs (revision 55e6f0b65f91b32638fd56581f711a816eccdcd1)
1 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
2 
3 //! This module provides a simple implementation of the Error struct that is
4 //! used as a drop-in replacement for `std::io::Error` when using `rbpf` in `no_std`.
5 
6 use alloc::string::String;
7 
8 /// Implementation of Error for no_std applications.
9 /// Ensures that the existing code can use it with the same interface
10 /// as the Error from std::io::Error.
11 #[derive(Debug)]
12 pub struct Error {
13     #[allow(dead_code)]
14     kind: ErrorKind,
15     #[allow(dead_code)]
16     error: String,
17 }
18 
19 impl Error {
20     /// New function exposing the same signature as `std::io::Error::new`.
21     #[allow(dead_code)]
22     pub fn new<S: Into<String>>(kind: ErrorKind, error: S) -> Error {
23         Error {
24             kind,
25             error: error.into(),
26         }
27     }
28 }
29 
30 /// The current version of `rbpf` only uses the [`Other`](ErrorKind::Other) variant
31 /// from the [std::io::ErrorKind] enum. If a dependency on other variants were
32 /// introduced in the future, this enum needs to be updated accordingly to maintain
33 /// compatibility with the real `ErrorKind`. The reason all available variants
34 /// aren't included in the first place is that [std::io::ErrorKind] exposes
35 /// 40 variants, and not all of them are meaningful under `no_std`.
36 #[derive(Debug)]
37 pub enum ErrorKind {
38     /// The no_std code only uses this variant.
39     #[allow(dead_code)]
40     Other,
41 }
42