Skip to content

Commit

Permalink
Core Lib Documentation: Gas module (#6967)
Browse files Browse the repository at this point in the history
Co-authored-by: Mathieu <[email protected]>
  • Loading branch information
TAdev0 and enitrat authored Jan 3, 2025
1 parent 7437056 commit 4e834eb
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions corelib/src/gas.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Utilities for handling gas in Cairo code.

#[cfg(not(gas: "disabled"))]
use crate::RangeCheck;

Expand All @@ -20,12 +22,15 @@ pub extern type GasBuiltin;
/// Returns `Option::Some(())` if there is sufficient gas to handle the success case, otherwise
/// returns `Option::None`.
///
/// Example:
/// # Examples
///
/// ```
/// // The success branch is the following lines, the failure branch is the `panic` caused by the
/// // `unwrap` call.
/// withdraw_gas().unwrap();
/// ```
///
/// ```
/// // Direct handling of `withdraw_gas`.
/// match withdraw_gas() {
/// Option::Some(()) => success_case(),
Expand All @@ -34,6 +39,7 @@ pub extern type GasBuiltin;
/// ```
#[cfg(not(gas: "disabled"))]
pub extern fn withdraw_gas() -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;

/// Placeholder when gas mechanism is disabled.
#[cfg(gas: "disabled")]
pub fn withdraw_gas() -> Option<()> nopanic {
Expand All @@ -48,13 +54,13 @@ pub fn withdraw_gas() -> Option<()> nopanic {
pub extern fn withdraw_gas_all(
costs: BuiltinCosts,
) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;

/// Placeholder when gas mechanism is disabled.
#[cfg(gas: "disabled")]
pub fn withdraw_gas_all(costs: BuiltinCosts) -> Option<()> nopanic {
Option::Some(())
}


/// Returns unused gas into the gas builtin.
///
/// Useful for cases where different branches take different amounts of gas, but gas withdrawal is
Expand All @@ -64,6 +70,7 @@ pub extern fn redeposit_gas() implicits(GasBuiltin) nopanic;
/// Returns the `BuiltinCosts` table to be used in `withdraw_gas_all`.
#[cfg(not(gas: "disabled"))]
pub extern fn get_builtin_costs() -> BuiltinCosts nopanic;

/// Placeholder when gas mechanism is disabled.
#[cfg(gas: "disabled")]
pub fn get_builtin_costs() -> BuiltinCosts nopanic {
Expand Down

0 comments on commit 4e834eb

Please sign in to comment.