no_std_error.rs 1.4 KB

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