HasNumberOrSlot

Trait HasNumberOrSlot 

Source
pub trait HasNumberOrSlot:
    Clone
    + Send
    + 'static {
    // Required method
    fn number_or_slot(&self) -> u64;
}
Expand description

Work with block numbers or slots in a unified way.

This trait provides a common interface for accessing block identifiers, which can either be a block number (for execution layer blocks) or a slot (for consensus layer blocks). By implementing this trait, types can expose their block number or slot in a standardized manner, enabling generic handling of different block types in streaming and processing workflows.

§Requirements

Types implementing this trait must also implement Clone, Send, and 'static to ensure compatibility with asynchronous and concurrent contexts.

§Provided Method

  • number_or_slot: Returns the block number or slot as a u64.

§Example

use firehose_client::HasNumberOrSlot;

#[derive(Clone)]
struct ExecutionBlock {
    block_number: u64,
}

#[derive(Clone)]
struct ConsensusBlock {
    slot: u64,
}

impl HasNumberOrSlot for ExecutionBlock {
    fn number_or_slot(&self) -> u64 {
        self.block_number
    }
}

impl HasNumberOrSlot for ConsensusBlock {
    fn number_or_slot(&self) -> u64 {
        self.slot
    }
}

fn process_block<T: HasNumberOrSlot>(block: &T) {
    println!("Processing block with identifier: {}", block.number_or_slot());
}

let execution_block = ExecutionBlock { block_number: 42 };
let consensus_block = ConsensusBlock { slot: 24 };

process_block(&execution_block);
process_block(&consensus_block);

§Use Case

This trait is particularly useful in scenarios where both execution and consensus layer blocks need to be processed generically, such as in blockchain indexing or synchronization applications.

Required Methods§

Source

fn number_or_slot(&self) -> u64

Return the block number or slot.

This value uniquely identifies the block within its respective layer, either as a block number (execution layer) or a slot (consensus layer).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§