diff --git a/.changelog/unreleased/features/974-id-into-string.md b/.changelog/unreleased/features/974-id-into-string.md new file mode 100644 index 000000000..139c6274b --- /dev/null +++ b/.changelog/unreleased/features/974-id-into-string.md @@ -0,0 +1,2 @@ +- Provide `Into` and `AsRef` for all identifiers types. + ([#974](https://github.com/cosmos/ibc-rs/pull/974)) diff --git a/crates/ibc-testkit/src/hosts/block.rs b/crates/ibc-testkit/src/hosts/block.rs index 986ca56ee..daa2008f3 100644 --- a/crates/ibc-testkit/src/hosts/block.rs +++ b/crates/ibc-testkit/src/hosts/block.rs @@ -148,7 +148,7 @@ impl HostBlock { let light_block = TestgenLightBlock::new_default_with_header( TestgenHeader::new(validators) .height(height) - .chain_id(&chain_id.to_string()) + .chain_id(chain_id.as_ref()) .next_validators(next_validators) .time(timestamp.into_tm_time().expect("Never fails")), ) diff --git a/crates/ibc/src/core/ics24_host/identifier.rs b/crates/ibc/src/core/ics24_host/identifier.rs index 8b7943847..9be879c10 100644 --- a/crates/ibc/src/core/ics24_host/identifier.rs +++ b/crates/ibc/src/core/ics24_host/identifier.rs @@ -157,6 +157,18 @@ impl FromStr for ChainId { } } +impl AsRef for ChainId { + fn as_ref(&self) -> &str { + &self.id + } +} + +impl From for String { + fn from(chain_id: ChainId) -> String { + chain_id.id + } +} + impl Display for ChainId { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> { write!(f, "{}", self.id) @@ -240,6 +252,12 @@ impl Display for ClientId { } } +impl AsRef for ClientId { + fn as_ref(&self) -> &str { + self.0.as_str() + } +} + impl FromStr for ClientId { type Err = IdentifierError; @@ -282,7 +300,7 @@ impl PartialEq for ClientId { )] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Into)] pub struct ConnectionId(String); impl ConnectionId { @@ -324,6 +342,12 @@ impl Display for ConnectionId { } } +impl AsRef for ConnectionId { + fn as_ref(&self) -> &str { + self.0.as_str() + } +} + impl FromStr for ConnectionId { type Err = IdentifierError; @@ -366,7 +390,7 @@ impl PartialEq for ConnectionId { )] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Into)] pub struct PortId(String); impl PortId { @@ -429,7 +453,7 @@ impl AsRef for PortId { )] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] #[cfg_attr(feature = "schema", derive(schemars::JsonSchema))] -#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Into)] pub struct ChannelId(String); impl ChannelId {