Skip to content

Enable "add_event" and "add_events" functions to process types implemented "into<Event>" #2044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ and this project adheres to

## [Unreleased]

### Changed

- cosmwasm-std: Enable `add_event` and `add_events` functions to process types
implementing `Into<Event>` ([#2044])

[#2044]: https://github.com/CosmWasm/cosmwasm/pull/2044

## [2.0.0-rc.1] - 2023-02-09

### Fixed
Expand Down
47 changes: 43 additions & 4 deletions packages/std/src/results/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ impl<T> Response<T> {
///
/// The `wasm-` prefix will be appended by the runtime to the provided type
/// of event.
pub fn add_event(mut self, event: Event) -> Self {
self.events.push(event);
pub fn add_event(mut self, event: impl Into<Event>) -> Self {
self.events.push(event.into());
self
}

Expand Down Expand Up @@ -219,8 +219,11 @@ impl<T> Response<T> {
///
/// The `wasm-` prefix will be appended by the runtime to the provided types
/// of events.
pub fn add_events(mut self, events: impl IntoIterator<Item = Event>) -> Self {
self.events.extend(events);
pub fn add_events<E>(mut self, events: impl IntoIterator<Item = E>) -> Self
where
E: Into<Event>,
{
self.events.extend(events.into_iter().map(|e| e.into()));
self
}

Expand Down Expand Up @@ -331,4 +334,40 @@ mod tests {
assert!(failure.is_err());
assert!(!success.is_err());
}

// struct implements `Into<Event>`
#[derive(Clone)]
struct OurEvent {
msg: String,
}

// allow define `into` rather than `from` to define `into` clearly
#[allow(clippy::from_over_into)]
impl Into<Event> for OurEvent {
fn into(self) -> Event {
Event::new("our_event").add_attribute("msg", self.msg)
}
}

#[test]
fn add_event_takes_into_event() {
let msg = "message".to_string();
let our_event = OurEvent { msg };
let event: Event = our_event.clone().into();
let actual = Response::<Empty>::new().add_event(our_event);
let expected = Response::<Empty>::new().add_event(event);
assert_eq!(expected, actual);
}

#[test]
fn add_events_takes_into_event() {
let msg1 = "foo".to_string();
let msg2 = "bare".to_string();
let our_event1 = OurEvent { msg: msg1 };
let our_event2 = OurEvent { msg: msg2 };
let events: Vec<Event> = vec![our_event1.clone().into(), our_event2.clone().into()];
let actual = Response::<Empty>::new().add_events([our_event1, our_event2]);
let expected = Response::<Empty>::new().add_events(events);
assert_eq!(expected, actual);
}
}