Skip to content

Commit e63ea26

Browse files
add docs
1 parent 8430708 commit e63ea26

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

soroban-sdk/src/lib.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,14 @@ pub use soroban_sdk_macros::contract;
365365
/// Functions that are publicly accessible in the implementation are invocable
366366
/// by other contracts, or directly by transactions, when deployed.
367367
///
368+
/// All trait functions visiible in the impl block are invocable. Trait
369+
/// default functions are only invocable if specified without a block. See the
370+
/// example below.
371+
///
368372
/// ### Examples
369373
///
374+
/// #### Defining a function
375+
///
370376
/// Define a contract with one function, `hello`, and call it from within a test
371377
/// using the generated client.
372378
///
@@ -399,6 +405,85 @@ pub use soroban_sdk_macros::contract;
399405
/// # #[cfg(not(feature = "testutils"))]
400406
/// # fn main() { }
401407
/// ```
408+
///
409+
/// #### Defining a function using a trait
410+
///
411+
/// Define a contract with one function, `hello`, that is defined by a trait.
412+
///
413+
/// ```
414+
/// use soroban_sdk::{contract, contractimpl, vec, symbol_short, BytesN, Env, Symbol, Vec};
415+
///
416+
/// pub trait HelloTrait {
417+
/// fn hello(env: Env, to: Symbol) -> Vec<Symbol>;
418+
/// }
419+
///
420+
/// #[contract]
421+
/// pub struct HelloContract;
422+
///
423+
/// #[contractimpl]
424+
/// impl HelloTrait for HelloContract {
425+
/// fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
426+
/// vec![&env, symbol_short!("Hello"), to]
427+
/// }
428+
/// }
429+
///
430+
/// #[test]
431+
/// fn test() {
432+
/// # }
433+
/// # #[cfg(feature = "testutils")]
434+
/// # fn main() {
435+
/// let env = Env::default();
436+
/// let contract_id = env.register(HelloContract, ());
437+
/// let client = HelloContractClient::new(&env, &contract_id);
438+
///
439+
/// let words = client.hello(&symbol_short!("Dev"));
440+
///
441+
/// assert_eq!(words, vec![&env, symbol_short!("Hello"), symbol_short!("Dev"),]);
442+
/// }
443+
/// # #[cfg(not(feature = "testutils"))]
444+
/// # fn main() { }
445+
/// ```
446+
///
447+
/// #### Defining a function using a trait with a default implementation
448+
///
449+
/// Define a contract with one function, `hello`, that is defined by a trait, and has a default
450+
/// implementation in the trait that the contract will make invocable.
451+
///
452+
/// ```
453+
/// use soroban_sdk::{contract, contractimpl, vec, symbol_short, BytesN, Env, Symbol, Vec};
454+
///
455+
/// pub trait HelloTrait {
456+
/// fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
457+
/// vec![&env, symbol_short!("Hello"), to]
458+
/// }
459+
/// }
460+
///
461+
/// #[contract]
462+
/// pub struct HelloContract;
463+
///
464+
/// #[contractimpl]
465+
/// impl HelloTrait for HelloContract {
466+
/// // To make a trait default function invocable it must be specified
467+
/// // in the impl block with no code block.
468+
/// fn hello(env: Env, to: Symbol) -> Vec<Symbol>;
469+
/// }
470+
///
471+
/// #[test]
472+
/// fn test() {
473+
/// # }
474+
/// # #[cfg(feature = "testutils")]
475+
/// # fn main() {
476+
/// let env = Env::default();
477+
/// let contract_id = env.register(HelloContract, ());
478+
/// let client = HelloContractClient::new(&env, &contract_id);
479+
///
480+
/// let words = client.hello(&symbol_short!("Dev"));
481+
///
482+
/// assert_eq!(words, vec![&env, symbol_short!("Hello"), symbol_short!("Dev"),]);
483+
/// }
484+
/// # #[cfg(not(feature = "testutils"))]
485+
/// # fn main() { }
486+
/// ```
402487
pub use soroban_sdk_macros::contractimpl;
403488

404489
/// Removes functions without blocks from impl blocks that use functions without blocks, terminated

0 commit comments

Comments
 (0)