1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use crate::{Bytes, B256};
use alloc::vec::Vec;
/// An Ethereum event log object.
#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(derive_arbitrary::Arbitrary, proptest_derive::Arbitrary))]
pub struct Log {
/// The indexed topic list.
topics: Vec<B256>,
/// The plain data.
pub data: Bytes,
}
impl Log {
/// Creates a new log, without length-checking. This allows creation of
/// invalid logs. May be safely used when the length of the topic list is
/// known to be 4 or less.
#[inline]
pub fn new_unchecked(topics: Vec<B256>, data: Bytes) -> Self {
Self { topics, data }
}
/// Creates a new log.
#[inline]
pub fn new(topics: Vec<B256>, data: Bytes) -> Option<Self> {
let this = Self::new_unchecked(topics, data);
this.is_valid().then_some(this)
}
/// Creates a new empty log.
#[inline]
pub const fn empty() -> Self {
Self { topics: Vec::new(), data: Bytes::new() }
}
/// True if valid, false otherwise.
#[inline]
pub fn is_valid(&self) -> bool {
self.topics.len() <= 4
}
/// Get the topic list.
#[inline]
pub fn topics(&self) -> &[B256] {
&self.topics
}
/// Get the topic list, mutably. This gives access to the internal
/// array, without allowing extension of that array.
#[inline]
pub fn topics_mut(&mut self) -> &mut [B256] {
&mut self.topics
}
/// Get a mutable reference to the topic list. This allows creation of
/// invalid logs.
#[inline]
pub fn topics_mut_unchecked(&mut self) -> &mut Vec<B256> {
&mut self.topics
}
/// Set the topic list, without length-checking. This allows creation of
/// invalid logs.
#[inline]
pub fn set_topics_unchecked(&mut self, topics: Vec<B256>) {
self.topics = topics;
}
/// Set the topic list, truncating to 4 topics.
#[inline]
pub fn set_topics_truncating(&mut self, mut topics: Vec<B256>) {
topics.truncate(4);
self.set_topics_unchecked(topics);
}
}