prelude.rs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #[allow(dead_code)]
  2. pub type KResult<T> = Result<T, u32>;
  3. macro_rules! dont_check {
  4. ($arg:expr) => {
  5. match $arg {
  6. Ok(_) => (),
  7. Err(_) => (),
  8. }
  9. };
  10. }
  11. use alloc::sync::Arc;
  12. #[allow(unused_imports)]
  13. pub(crate) use dont_check;
  14. #[allow(unused_imports)]
  15. pub use crate::bindings::root as bindings;
  16. #[allow(unused_imports)]
  17. pub(crate) use crate::kernel::console::{
  18. print, println, println_debug, println_fatal, println_info, println_warn,
  19. };
  20. #[allow(unused_imports)]
  21. pub(crate) use crate::sync::might_sleep;
  22. #[allow(unused_imports)]
  23. pub(crate) use alloc::{boxed::Box, string::String, vec, vec::Vec};
  24. #[allow(unused_imports)]
  25. pub(crate) use core::{any::Any, fmt::Write, marker::PhantomData, str};
  26. use core::{mem::ManuallyDrop, ops::Deref};
  27. pub use crate::sync::{Mutex, RwSemaphore, Semaphore, Spin, Locked};
  28. pub struct BorrowedArc<'lt, T: ?Sized> {
  29. arc: ManuallyDrop<Arc<T>>,
  30. _phantom: PhantomData<&'lt ()>,
  31. }
  32. impl<'lt, T: ?Sized> BorrowedArc<'lt, T> {
  33. pub fn from_raw(ptr: *const T) -> Self {
  34. assert!(!ptr.is_null());
  35. Self {
  36. arc: ManuallyDrop::new(unsafe { Arc::from_raw(ptr) }),
  37. _phantom: PhantomData,
  38. }
  39. }
  40. pub fn new(ptr: &'lt *const T) -> Self {
  41. assert!(!ptr.is_null());
  42. Self {
  43. arc: ManuallyDrop::new(unsafe { Arc::from_raw(*ptr) }),
  44. _phantom: PhantomData,
  45. }
  46. }
  47. }
  48. impl<'lt, T: ?Sized> Deref for BorrowedArc<'lt, T> {
  49. type Target = Arc<T>;
  50. fn deref(&self) -> &Self::Target {
  51. &self.arc
  52. }
  53. }
  54. impl<'lt, T: ?Sized> AsRef<Arc<T>> for BorrowedArc<'lt, T> {
  55. fn as_ref(&self) -> &Arc<T> {
  56. &self.arc
  57. }
  58. }
  59. pub trait AsAny: Send + Sync {
  60. fn as_any(&self) -> &dyn Any;
  61. fn as_any_mut(&mut self) -> &mut dyn Any;
  62. }
  63. macro_rules! impl_any {
  64. ($t:ty) => {
  65. impl AsAny for $t {
  66. fn as_any(&self) -> &dyn Any {
  67. self
  68. }
  69. fn as_any_mut(&mut self) -> &mut dyn Any {
  70. self
  71. }
  72. }
  73. };
  74. }
  75. macro_rules! addr_of_mut_field {
  76. ($pointer:expr, $field:ident) => {
  77. core::ptr::addr_of_mut!((*$pointer).$field)
  78. };
  79. }
  80. pub(crate) use {addr_of_mut_field, impl_any};