Kaynağa Gözat

lang: Cleanup private apis and documentation (#169)

Armani Ferrante 4 yıl önce
ebeveyn
işleme
81e03c5e37

+ 7 - 8
cli/src/main.rs

@@ -921,12 +921,12 @@ fn test(skip_deploy: bool, skip_local_validator: bool, file: Option<String>) ->
             }
         };
 
-        let log_streams = stream_logs(&cfg.cluster.url())?;
+        // Setup log reader.
+        let log_streams = stream_logs(&cfg.cluster.url());
 
-        let result: Result<_> = {
+        // Run the tests.
+        let test_result: Result<_> = {
             let ts_config_exist = Path::new("tsconfig.json").exists();
-
-            // Run the tests.
             let mut args = vec!["-t", "1000000"];
             if let Some(ref file) = file {
                 args.push(file);
@@ -959,19 +959,18 @@ fn test(skip_deploy: bool, skip_local_validator: bool, file: Option<String>) ->
             exit
         };
 
+        // Check all errors and shut down.
         if let Some(mut child) = validator_handle {
             if let Err(err) = child.kill() {
                 println!("Failed to kill subprocess {}: {}", child.id(), err);
             }
         }
-
-        for mut child in log_streams {
+        for mut child in log_streams? {
             if let Err(err) = child.kill() {
                 println!("Failed to kill subprocess {}: {}", child.id(), err);
             }
         }
-
-        match result {
+        match test_result {
             Ok(exit) => {
                 if !exit.status.success() {
                     std::process::exit(exit.status.code().unwrap());

+ 2 - 2
client/src/lib.rs

@@ -120,7 +120,7 @@ impl Program {
         self.program_id
     }
 
-    pub fn on<T: anchor_lang::EventData + anchor_lang::AnchorDeserialize>(
+    pub fn on<T: anchor_lang::Event + anchor_lang::AnchorDeserialize>(
         &self,
         f: impl Fn(&EventContext, T) -> () + Send + 'static,
     ) -> Result<EventHandle, ClientError> {
@@ -187,7 +187,7 @@ impl Program {
     }
 }
 
-fn handle_program_log<T: anchor_lang::EventData + anchor_lang::AnchorDeserialize>(
+fn handle_program_log<T: anchor_lang::Event + anchor_lang::AnchorDeserialize>(
     self_program_str: &str,
     l: &str,
 ) -> Result<(Option<T>, Option<String>, bool), ClientError> {

+ 7 - 7
lang/attribute/event/src/lib.rs

@@ -26,10 +26,10 @@ pub fn event(
     };
 
     proc_macro::TokenStream::from(quote! {
-        #[derive(anchor_lang::EventIndex, AnchorSerialize, AnchorDeserialize)]
+        #[derive(anchor_lang::__private::EventIndex, AnchorSerialize, AnchorDeserialize)]
         #event_strct
 
-        impl anchor_lang::EventData for #event_name {
+        impl anchor_lang::Event for #event_name {
             fn data(&self) -> Vec<u8> {
                 let mut d = #discriminator.to_vec();
                 d.append(&mut self.try_to_vec().unwrap());
@@ -45,16 +45,16 @@ pub fn event(
     })
 }
 
-/// Creates an event in an Anchor program, which can subsequently be subscribed
-/// to by clients. Calling this method will internally borsh serialize the
-/// [event](./attr.event.html), base64 encode the bytes, and then add a
-/// [msg!](../solana_program/macro.msg.html) log to the transaction.
+/// Creates an event that can be subscribed to by clients. Calling this method
+/// will internally borsh serialize the [event](./attr.event.html), base64
+/// encode the bytes, and then add a [msg!](../solana_program/macro.msg.html)
+/// log to the transaction.
 #[proc_macro]
 pub fn emit(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
     let data: proc_macro2::TokenStream = input.into();
     proc_macro::TokenStream::from(quote! {
         {
-            let data = anchor_lang::EventData::data(&#data);
+            let data = anchor_lang::Event::data(&#data);
             let msg_str = &anchor_lang::__private::base64::encode(data);
             anchor_lang::solana_program::msg!(msg_str);
         }

+ 1 - 0
lang/src/context.rs

@@ -9,6 +9,7 @@ pub struct Context<'a, 'b, 'c, 'info, T> {
     /// Deserialized accounts.
     pub accounts: &'b mut T,
     /// Remaining accounts given but not deserialized or validated.
+    /// Be very careful when using this directly.
     pub remaining_accounts: &'c [AccountInfo<'info>],
 }
 

+ 2 - 0
lang/src/error.rs

@@ -1,5 +1,7 @@
 use solana_program::program_error::ProgramError;
 
+// Error type that can be returned by internal framework code.
+#[doc(hidden)]
 #[derive(thiserror::Error, Debug)]
 pub enum Error {
     #[error(transparent)]

+ 1 - 1
lang/src/idl.rs

@@ -1,4 +1,4 @@
-//! idl.rs defines the instructions and account state used to store a program's
+//! Defines the instructions and account state used to store a program's
 //! IDL on-chain at a canonical account address, which can be derived as a
 //! function of nothing other than the program's ID.
 //!

+ 19 - 8
lang/src/lib.rs

@@ -35,6 +35,7 @@ mod context;
 mod cpi_account;
 mod ctor;
 mod error;
+#[doc(hidden)]
 pub mod idl;
 mod program_account;
 mod state;
@@ -44,26 +45,27 @@ mod vec;
 // Internal module used by macros.
 #[doc(hidden)]
 pub mod __private {
+    pub use crate::ctor::Ctor;
+    pub use crate::error::Error;
+    pub use anchor_attribute_event::EventIndex;
     pub use base64;
 }
 
 pub use crate::context::{Context, CpiContext};
 pub use crate::cpi_account::CpiAccount;
-pub use crate::ctor::Ctor;
 pub use crate::program_account::ProgramAccount;
 pub use crate::state::ProgramState;
 pub use crate::sysvar::Sysvar;
 pub use anchor_attribute_access_control::access_control;
 pub use anchor_attribute_account::account;
 pub use anchor_attribute_error::error;
-pub use anchor_attribute_event::{emit, event, EventIndex};
+pub use anchor_attribute_event::{emit, event};
 pub use anchor_attribute_interface::interface;
 pub use anchor_attribute_program::program;
 pub use anchor_attribute_state::state;
 pub use anchor_derive_accounts::Accounts;
 /// Borsh is the default serialization format for instructions and accounts.
 pub use borsh::{BorshDeserialize as AnchorDeserialize, BorshSerialize as AnchorSerialize};
-pub use error::Error;
 pub use solana_program;
 
 /// A data structure of validated accounts that can be deserialized from the
@@ -176,18 +178,27 @@ pub trait InstructionData: AnchorSerialize {
     fn data(&self) -> Vec<u8>;
 }
 
-/// Calculates the size of an account, which may be larger than the deserialized
-/// data in it. This trait is currently only used for `#[state]` accounts.
+// Calculates the size of an account, which may be larger than the deserialized
+// data in it. This trait is currently only used for `#[state]` accounts.
+#[doc(hidden)]
 pub trait AccountSize: AnchorSerialize {
     fn size(&self) -> Result<u64, ProgramError>;
 }
 
-/// The serialized event data to be emitted via a Solana log.
+/// An event that can be emitted via a Solana log.
+pub trait Event: AnchorSerialize + AnchorDeserialize + Discriminator {
+    fn data(&self) -> Vec<u8>;
+}
+
+// The serialized event data to be emitted via a Solana log.
+// TODO: remove this on the next major version upgrade.
+#[doc(hidden)]
+#[deprecated(since = "0.4.2", note = "Please use Event instead")]
 pub trait EventData: AnchorSerialize + Discriminator {
     fn data(&self) -> Vec<u8>;
 }
 
-/// 8 byte identifier for a type.
+/// 8 byte unique identifier for a type.
 pub trait Discriminator {
     fn discriminator() -> [u8; 8];
 }
@@ -198,7 +209,7 @@ pub mod prelude {
     pub use super::{
         access_control, account, emit, error, event, interface, program, state, AccountDeserialize,
         AccountSerialize, Accounts, AccountsExit, AccountsInit, AnchorDeserialize, AnchorSerialize,
-        Context, CpiAccount, CpiContext, Ctor, ProgramAccount, ProgramState, Sysvar, ToAccountInfo,
+        Context, CpiAccount, CpiContext, ProgramAccount, ProgramState, Sysvar, ToAccountInfo,
         ToAccountInfos, ToAccountMetas,
     };
 

+ 1 - 1
lang/syn/src/codegen/program.rs

@@ -401,7 +401,7 @@ pub fn generate_non_inlined_handlers(program: &Program) -> proc_macro2::TokenStr
                         let mut remaining_accounts: &[AccountInfo] = accounts;
 
                         // Deserialize accounts.
-                        let ctor_accounts = anchor_lang::Ctor::try_accounts(program_id, &mut remaining_accounts)?;
+                        let ctor_accounts = anchor_lang::__private::Ctor::try_accounts(program_id, &mut remaining_accounts)?;
                         let mut ctor_user_def_accounts = #anchor_ident::try_accounts(program_id, &mut remaining_accounts)?;
 
                         // Invoke the ctor.