Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // DDK API
- //
- /// A function which provides a DDK operation.
- ///
- /// An `Op<Context, Args, Return>` is the DDK's representation of a function
- /// which implements an operation, takes a `Context`, extra arguments `Args`,
- /// and returns a `Return`value. The `#[ddk_op]` attribute generates `Op` values
- /// for functions automatically, so a driver author should never need to
- /// construct an `Op`.
- ///
- /// For example, the following function has an op type of
- /// `Op<Context, (usize, usize), usize>`:
- ///
- /// ```rust,ignore
- /// #[ddk_op]
- /// fn multiply(ctx: &mut Context, a: usize, b: usize) -> usize { ... }
- /// ```
- pub struct Op<Context, Args, Return> {
- #[doc(hidden)]
- pub ptr: *const c_void,
- #[doc(hidden)]
- pub _marker: PhantomData<(Context, Args, Return)>,
- }
- /// A collection of operations that implement a protocol.
- ///
- /// A `ProtocolDevice` is a collection of operations that implement a protocol.
- /// All operations take the context type `C`. There is a single static instance
- /// of `ProtocolDevice` for each driver. For example, for a protocol with the
- /// `foo` and `bar` operations:
- ///
- /// ```rust,ignore
- /// static DEVICE_OPS: ProtocolDevice<Context> =
- /// ProtocolDevice::default().foo(foo).bar(bar);
- /// ```
- ///
- struct ProtocolDevice<C> { ... }
- impl<C> ProtocolDevice<C> {
- /// A default `ProtocolDevice` with all operations defaulted.
- ///
- /// `default` creates a `ProtocolDevice` in which every operation is set
- /// to the default, which returns `ZX_STATUS_ERR`.
- const fn default() -> ProtocolDevice<C> { ... }
- /// Add a `get_protocol` operation.
- ///
- /// `get_protocol` overrides the default `get_protocol` operation.
- const fn get_protocol(self, get_protocol: Op<C, (), ()>) -> ProtocolDevice { ... }
- }
- //
- // What they write
- //
- static DEVICE_OPS: ProtocolDevice = ProtocolDevice::default().get_protocol(get_protocol);
- struct Context;
- impl Context {
- #[ddk_op]
- fn get_protocol(&self) {}
- }
- //
- // What it desugars to
- //
- static DEVICE_OPS: ProtocolDevice = ProtocolDevice::default().get_protocol(get_protocol);
- struct Context;
- // TODO: can this be const instead of static?
- static get_protocol: Op<Context, (), ()> = Op { ptr: __get_protocol as *const c_void, _marker: PhantomData };
- impl Context {
- fn __get_protocol(&self) {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement