Skip to content

Commit

Permalink
Add doc comments about why symbol_short! (#1064)
Browse files Browse the repository at this point in the history
### What
Add doc comments about why symbol_short! is preferred over
Symbol::short.

### Why
The behavior of const functions when called in non-const-contexts is not
well known, as has been evidenced by the many times folks have asked why
we have symbol_short!, and the case where symbol_short! was removed with
folks assuming it was of no use. For example:
#1043 (comment).
Adding docs to knowledge share.
  • Loading branch information
leighmcculloch authored Aug 25, 2023
1 parent f9969b4 commit 535a9dd
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion soroban-sdk/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,30 @@ impl Symbol {
///
/// Valid characters are `a-zA-Z0-9_` and maximum length is 9 characters.
///
/// The conversion can happen at compile time.
/// The conversion can happen at compile time if called in a const context,
/// such as:
///
/// ```rust
/// # use soroban_sdk::Symbol;
/// const SYMBOL: Symbol = Symbol::short("abcde");
/// ```
///
/// Note that when called from a non-const context the conversion will occur
/// at runtime and the conversion logic will add considerable number of
/// bytes to built wasm file. For this reason the function should be generally
/// avoided:
///
/// ```rust
/// # use soroban_sdk::Symbol;
/// let SYMBOL: Symbol = Symbol::short("abcde"); // AVOID!
/// ```
///
/// Instead use the `symbol_short!()` macro that will ensure the conversion always occurs in a const-context:
///
/// ```rust
/// # use soroban_sdk::{symbol_short, Symbol};
/// let SYMBOL: Symbol = symbol_short!("abcde"); // 👍
/// ```
///
/// ### Panics
///
Expand Down

0 comments on commit 535a9dd

Please sign in to comment.