Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use const array
  • Loading branch information
SkymanOne committed Dec 22, 2023
commit 683e180f25b537dc3babc687603d470f22825e6c
2 changes: 1 addition & 1 deletion crates/env/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,5 @@ pub trait Event: scale::Encode + GetSignatureTopic {
/// default calculates this as `blake2b("Event(field1_type,field2_type)")`
pub trait GetSignatureTopic {
/// Retrieve the signature topic
fn signature_topic() -> Option<[u8; 32]>;
const SIGNATURE_TOPIC: core::option::Option<[u8; 32]>;
}
4 changes: 1 addition & 3 deletions crates/ink/codegen/src/generator/signature_topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ impl SignatureTopic<'_> {

quote! {
impl ::ink::env::GetSignatureTopic for #item_ident {
fn signature_topic() -> Option<[u8; 32]> {
#signature_topic
}
const SIGNATURE_TOPIC: ::core::option::Option<[u8; 32]> = #signature_topic;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ink/macro/src/event/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn event_metadata_derive_struct(s: synstructure::Structure) -> syn::Result<Token
::ink::metadata::EventSpec::new(::core::stringify!(#ident))
.module_path(::core::module_path!())
.signature_topic(
<Self as ::ink::env::GetSignatureTopic>::signature_topic()
<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC
)
.args([
#( #args ),*
Expand Down
6 changes: 2 additions & 4 deletions crates/ink/macro/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn event_derive_struct(mut s: synstructure::Structure) -> syn::Result<TokenStrea
None
} else {
Some(quote_spanned!(span=>
.push_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic().as_ref())
.push_topic(<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC.as_ref())
))
};

Expand Down Expand Up @@ -163,9 +163,7 @@ fn generate_signature_topic_blank(
if anonymous {
quote! {
impl ::ink::env::GetSignatureTopic for #item_ident {
fn signature_topic() -> Option<[u8; 32]> {
None
}
const SIGNATURE_TOPIC: ::core::option::Option<[u8; 32]> = None;
}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/ink/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ pub fn event(attr: TokenStream, item: TokenStream) -> TokenStream {
///
/// use ink::env::GetSignatureTopic;
/// assert_eq!(Some([17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17]),
/// <MyCustomSignatureEvent as GetSignatureTopic>::signature_topic())
/// <MyCustomSignatureEvent as GetSignatureTopic>::SIGNATURE_TOPIC)
#[proc_macro_attribute]
pub fn signature_topic(attr: TokenStream, item: TokenStream) -> TokenStream {
event::generate_signature_topic(attr.into(), item.into()).into()
Expand Down Expand Up @@ -1447,7 +1447,7 @@ synstructure::decl_derive!(
/// }
///
/// use ink::env::GetSignatureTopic;
/// assert_ne!(<MyEvent as GetSignatureTopic>::signature_topic(), <other_event::MyEvent as GetSignatureTopic>::signature_topic());
/// assert_ne!(<MyEvent as GetSignatureTopic>::SIGNATURE_TOPIC, <other_event::MyEvent as GetSignatureTopic>::SIGNATURE_TOPIC);
/// ```
///
/// ## Anonymous Events
Expand Down
10 changes: 4 additions & 6 deletions crates/ink/macro/src/tests/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn unit_struct_works() {
UnitStruct => {
builder
.build::<Self>()
.push_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic().as_ref())
.push_topic(<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC.as_ref())
.finish()
}
}
Expand All @@ -68,9 +68,7 @@ fn unit_struct_anonymous_has_no_topics() {
}
expands to {
impl ::ink::env::GetSignatureTopic for UnitStruct {
fn signature_topic() -> Option<[u8; 32]> {
None
}
const SIGNATURE_TOPIC: ::core::option::Option<[u8; 32]> = None;
}
const _: () = {
impl ::ink::env::Event for UnitStruct {
Expand Down Expand Up @@ -127,7 +125,7 @@ fn struct_with_fields_no_topics() {
Event { .. } => {
builder
.build::<Self>()
.push_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic().as_ref())
.push_topic(<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC.as_ref())
.finish()
}
}
Expand Down Expand Up @@ -170,7 +168,7 @@ fn struct_with_fields_and_some_topics() {
Event { field_2 : __binding_1 , field_3 : __binding_2 , .. } => {
builder
.build::<Self>()
.push_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic().as_ref())
.push_topic(<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC.as_ref())
.push_topic(::ink::as_option!(__binding_1))
.push_topic(::ink::as_option!(__binding_2))
.finish()
Expand Down
6 changes: 3 additions & 3 deletions crates/ink/macro/src/tests/event_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn unit_struct_works() {

::ink::metadata::EventSpec::new(::core::stringify!(UnitStruct))
.module_path(::core::module_path!())
.signature_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic())
.signature_topic(<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC)
.args([])
.docs([])
.done()
Expand Down Expand Up @@ -71,7 +71,7 @@ fn struct_with_fields_no_topics() {

::ink::metadata::EventSpec::new(::core::stringify!(Event))
.module_path(::core::module_path!())
.signature_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic())
.signature_topic(<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC)
.args([
::ink::metadata::EventParamSpec::new(::core::stringify!(field_1))
.of_type(::ink::metadata::TypeSpec::of_type::<u32>())
Expand Down Expand Up @@ -125,7 +125,7 @@ fn struct_with_fields_and_some_topics() {

::ink::metadata::EventSpec::new(::core::stringify!(Event))
.module_path(::core::module_path!())
.signature_topic(<Self as ::ink::env::GetSignatureTopic>::signature_topic())
.signature_topic(<Self as ::ink::env::GetSignatureTopic>::SIGNATURE_TOPIC)
.args([
::ink::metadata::EventParamSpec::new(::core::stringify!(field_1))
.of_type(::ink::metadata::TypeSpec::of_type::<u32>())
Expand Down
16 changes: 8 additions & 8 deletions integration-tests/events/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ pub mod events {

assert_eq!(event.topics.len(), 3);
let signature_topic =
<event_def::ThirtyTwoByteTopics as ink::env::GetSignatureTopic>::signature_topic()
<event_def::ThirtyTwoByteTopics as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC
.map(|topic| topic.to_vec());
assert_eq!(Some(&event.topics[0]), signature_topic.as_ref());
assert_eq!(event.topics[1], [0x42; 32]);
Expand All @@ -186,7 +186,7 @@ pub mod events {
let event = &emitted_events[0];

let signature_topic =
<event_def::ThirtyTwoByteTopics as ink::env::GetSignatureTopic>::signature_topic()
<event_def::ThirtyTwoByteTopics as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC
.map(|topic| topic.to_vec())
.unwrap();

Expand All @@ -207,7 +207,7 @@ pub mod events {
assert_eq!(1, emitted_events.len());

let signature_topic =
<InlineCustomFlipped as ink::env::GetSignatureTopic>::signature_topic();
<InlineCustomFlipped as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC;

assert_eq!(Some([17u8; 32]), signature_topic);
}
Expand All @@ -230,7 +230,7 @@ pub mod events {
assert_eq!(event.topics[0], topic);

let signature_topic =
<InlineAnonymousEvent as ink::env::GetSignatureTopic>::signature_topic();
<InlineAnonymousEvent as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC;
assert_eq!(None, signature_topic);
}
}
Expand Down Expand Up @@ -278,7 +278,7 @@ pub mod events {
assert_eq!(!init_value, flipped.value);

let signature_topic =
<event_def::ForeignFlipped as ink::env::GetSignatureTopic>::signature_topic()
<event_def::ForeignFlipped as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC
.map(H256::from)
.unwrap();

Expand Down Expand Up @@ -321,7 +321,7 @@ pub mod events {
assert_eq!(!init_value, flipped.value);

let signature_topic =
<InlineFlipped as ink::env::GetSignatureTopic>::signature_topic()
<InlineFlipped as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC
.map(H256::from)
.unwrap();

Expand Down Expand Up @@ -364,7 +364,7 @@ pub mod events {
assert!(event.maybe_hash.is_none());

let signature_topic =
<event_def::ThirtyTwoByteTopics as ink::env::GetSignatureTopic>::signature_topic()
<event_def::ThirtyTwoByteTopics as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC
.map(H256::from)
.unwrap();

Expand Down Expand Up @@ -406,7 +406,7 @@ pub mod events {
assert_eq!(1, contract_events.len());

let signature_topic =
<InlineCustomFlipped as ink::env::GetSignatureTopic>::signature_topic();
<InlineCustomFlipped as ink::env::GetSignatureTopic>::SIGNATURE_TOPIC;

assert_eq!(Some([17u8; 32]), signature_topic);

Expand Down