/crates/system/lib.rs

https://gitlab.com/yogeshc/redox · Rust · 46 lines · 37 code · 7 blank · 2 comment · 6 complexity · 10595b8af9c04bea5d953a4384875315 MD5 · raw file

  1. #![crate_name="system"]
  2. #![crate_type="lib"]
  3. #![feature(asm)]
  4. #![feature(lang_items)]
  5. #![no_std]
  6. use core::{ptr, slice, str};
  7. pub mod error;
  8. #[cfg(target_os="redox")]
  9. pub mod externs;
  10. pub mod graphics;
  11. pub mod scheme;
  12. pub mod syscall;
  13. /// Helper function for handling C strings, please do not copy it or make it pub or change it
  14. pub fn c_string_to_slice<'a>(ptr: *const u8) -> &'a [u8] {
  15. if ptr > 0 as *const u8 {
  16. let mut len = 0;
  17. while unsafe { ptr::read(ptr.offset(len as isize)) } > 0 {
  18. len += 1;
  19. }
  20. unsafe { slice::from_raw_parts(ptr, len) }
  21. } else {
  22. &[]
  23. }
  24. }
  25. pub fn c_string_to_str<'a>(ptr: *const u8) -> &'a str {
  26. unsafe { str::from_utf8_unchecked(c_string_to_slice(ptr)) }
  27. }
  28. /// Helper function for handling C strings, please do not copy it or make it pub or change it
  29. pub fn c_array_to_slice<'a>(ptr: *const *const u8) -> &'a [*const u8] {
  30. if ptr > 0 as *const *const u8 {
  31. let mut len = 0;
  32. while unsafe { ptr::read(ptr.offset(len as isize)) } > 0 as *const u8 {
  33. len += 1;
  34. }
  35. unsafe { slice::from_raw_parts(ptr, len) }
  36. } else {
  37. &[]
  38. }
  39. }