Browse Source

add docs for emit! macro (#1692)

Paul 3 years ago
parent
commit
5194de04b1
2 changed files with 23 additions and 1 deletions
  1. 22 0
      lang/attribute/event/src/lib.rs
  2. 1 1
      lang/src/lib.rs

+ 22 - 0
lang/attribute/event/src/lib.rs

@@ -7,6 +7,8 @@ use syn::parse_macro_input;
 /// [emit!](./macro.emit.html) so that programs can log significant events in
 /// their programs that clients can subscribe to. Currently, this macro is for
 /// structs only.
+///
+/// See the [`emit!` macro](emit!) for an example.
 #[proc_macro_attribute]
 pub fn event(
     _args: proc_macro::TokenStream,
@@ -51,6 +53,26 @@ pub fn event(
 /// ```ignore
 /// Program data: <Base64EncodedEvent>
 /// ```
+/// # Example
+///
+/// ```rust,ignore
+/// use anchor_lang::prelude::*;
+///
+/// // handler function inside #[program]
+/// pub fn initialize(_ctx: Context<Initialize>) -> Result<()> {
+///     emit!(MyEvent {
+///         data: 5,
+///         label: [1,2,3,4,5],
+///     });
+///     Ok(())
+/// }
+///
+/// #[event]
+/// pub struct MyEvent {
+///     pub data: u64,
+///     pub label: [u8; 5],
+/// }
+/// ```
 #[proc_macro]
 pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
     let data: proc_macro2::TokenStream = input.into();

+ 1 - 1
lang/src/lib.rs

@@ -185,7 +185,7 @@ pub trait InstructionData: AnchorSerialize {
     fn data(&self) -> Vec<u8>;
 }
 
-/// An event that can be emitted via a Solana log.
+/// An event that can be emitted via a Solana log. See [`emit!`](crate::prelude::emit) for an example.
 pub trait Event: AnchorSerialize + AnchorDeserialize + Discriminator {
     fn data(&self) -> Vec<u8>;
 }