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 0000000000..139c6274bd --- /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/src/core/ics24_host/identifier.rs b/crates/ibc/src/core/ics24_host/identifier.rs index 8b7943847b..9be879c10b 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 {