Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. //
  2. // DDK API
  3. //
  4.  
  5. /// A function which provides a DDK operation.
  6. ///
  7. /// An `Op<Context, Args, Return>` is the DDK's representation of a function
  8. /// which implements an operation, takes a `Context`, extra arguments `Args`,
  9. /// and returns a `Return`value. The `#[ddk_op]` attribute generates `Op` values
  10. /// for functions automatically, so a driver author should never need to
  11. /// construct an `Op`.
  12. ///
  13. /// For example, the following function has an op type of
  14. /// `Op<Context, (usize, usize), usize>`:
  15. ///
  16. /// ```rust,ignore
  17. /// #[ddk_op]
  18. /// fn multiply(ctx: &mut Context, a: usize, b: usize) -> usize { ... }
  19. /// ```
  20. pub struct Op<Context, Args, Return> {
  21. #[doc(hidden)]
  22. pub ptr: *const c_void,
  23. #[doc(hidden)]
  24. pub _marker: PhantomData<(Context, Args, Return)>,
  25. }
  26.  
  27. /// A collection of operations that implement a protocol.
  28. ///
  29. /// A `ProtocolDevice` is a collection of operations that implement a protocol.
  30. /// All operations take the context type `C`. There is a single static instance
  31. /// of `ProtocolDevice` for each driver. For example, for a protocol with the
  32. /// `foo` and `bar` operations:
  33. ///
  34. /// ```rust,ignore
  35. /// static DEVICE_OPS: ProtocolDevice<Context> =
  36. /// ProtocolDevice::default().foo(foo).bar(bar);
  37. /// ```
  38. ///
  39. struct ProtocolDevice<C> { ... }
  40.  
  41. impl<C> ProtocolDevice<C> {
  42. /// A default `ProtocolDevice` with all operations defaulted.
  43. ///
  44. /// `default` creates a `ProtocolDevice` in which every operation is set
  45. /// to the default, which returns `ZX_STATUS_ERR`.
  46. const fn default() -> ProtocolDevice<C> { ... }
  47.  
  48. /// Add a `get_protocol` operation.
  49. ///
  50. /// `get_protocol` overrides the default `get_protocol` operation.
  51. const fn get_protocol(self, get_protocol: Op<C, (), ()>) -> ProtocolDevice { ... }
  52. }
  53.  
  54. //
  55. // What they write
  56. //
  57.  
  58. static DEVICE_OPS: ProtocolDevice = ProtocolDevice::default().get_protocol(get_protocol);
  59.  
  60. struct Context;
  61.  
  62. impl Context {
  63. #[ddk_op]
  64. fn get_protocol(&self) {}
  65. }
  66.  
  67. //
  68. // What it desugars to
  69. //
  70.  
  71. static DEVICE_OPS: ProtocolDevice = ProtocolDevice::default().get_protocol(get_protocol);
  72.  
  73. struct Context;
  74.  
  75. // TODO: can this be const instead of static?
  76. static get_protocol: Op<Context, (), ()> = Op { ptr: __get_protocol as *const c_void, _marker: PhantomData };
  77.  
  78. impl Context {
  79. fn __get_protocol(&self) {}
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement