Skip to content

Commit

Permalink
docs: add missing documentation on types
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Dec 2, 2024
1 parent 6c78ab6 commit 7aab316
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
10 changes: 9 additions & 1 deletion starknet-core/src/types/byte_array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Support for [`String`] compatibility with Cairo `ByteArray`.
//! [https://github.com/starkware-libs/cairo/blob/f3af4cb8dbe9acecaf71cfbc604df3d1e41fe45a/corelib/src/byte_array.cairo].
//! <https://github.com/starkware-libs/cairo/blob/0b86ece404b0922b76caca5d07a94ed41407f174/corelib/src/byte_array.cairo>.
//!
//! The basic concept of this `ByteArray` is relying on a string being
//! represented as an array of bytes packed by 31 bytes ([`Bytes31`]) in a [`Felt`].
Expand All @@ -16,10 +16,18 @@ use crate::types::{Bytes31, Felt};

const MAX_WORD_LEN: usize = 31;

/// A struct representing a Cairo `ByteArray`.
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct ByteArray {
/// An array of full "words" of 31 bytes each.
/// The first byte of each word in the byte array is the most significant byte in the word.
pub data: Vec<Bytes31>,
/// A `felt252` that actually represents a `bytes31`, with less than 31 bytes.
/// It is represented as a `felt252` to improve performance of building the byte array.
/// The first byte is the most significant byte among the `pending_word_len` bytes in the word.
pub pending_word: Bytes31,
/// The number of bytes in `pending_word`.
/// Its value should be in the range [0, 30].
pub pending_word_len: usize,
}

Expand Down
3 changes: 2 additions & 1 deletion starknet-core/src/types/bytes_31.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Support for `Bytes31` Cairo primitive.
//! [https://github.com/starkware-libs/cairo/blob/main/corelib/src/bytes_31.cairo].
//! <https://github.com/starkware-libs/cairo/blob/main/corelib/src/bytes_31.cairo>.
//!
//! This type is mostly used internally for [`crate::types::ByteArray`] internal logic.
use alloc::{
Expand Down Expand Up @@ -59,6 +59,7 @@ impl Bytes31 {
String::from_utf8(buffer)
}

/// Converts a hex string to a [`Bytes31`].
pub fn from_hex(hex: &str) -> Result<Self, FromStrError> {
Ok(Self(Felt::from_hex(hex)?))
}
Expand Down

0 comments on commit 7aab316

Please sign in to comment.