diff options
author | Daniel Schadt <kingdread@gmx.de> | 2020-04-29 14:35:06 +0200 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2020-04-29 14:35:06 +0200 |
commit | f87419f62fdd015e69798982a7d8f09b55743ff5 (patch) | |
tree | c39d48ce73b418f3b12d9877e7e05aa8c7bab57e /src/lib.rs | |
parent | a7b7b4186b396c4708952d06426a286cab095ecd (diff) | |
download | evtclib-f87419f62fdd015e69798982a7d8f09b55743ff5.tar.gz evtclib-f87419f62fdd015e69798982a7d8f09b55743ff5.tar.bz2 evtclib-f87419f62fdd015e69798982a7d8f09b55743ff5.zip |
implement TryFrom for non-references
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 61 |
1 files changed, 61 insertions, 0 deletions
@@ -124,6 +124,24 @@ impl Character { } /// The type of an agent. +/// +/// arcdps differentiates between three types of agents: [`Player`][Player], +/// [`Character`][Character] and [`Gadget`][Gadget]. This enum unifies handling between them by +/// allowing you to pattern match or use one of the accessor methods. +/// +/// The main way to obtain a `AgentKind` is by using the [`.kind()`][Agent::kind] method on an +/// [`Agent`][Agent]. In cases where you already have a [`raw::Agent`][raw::Agent] available, you +/// can also use the [`TryFrom`][TryFrom]/[`TryInto`][std::convert::TryInto] traits to convert a +/// `raw::Agent` or `&raw::Agent` to a `AgentKind`: +/// +/// ```no_run +/// # use evtclib::{AgentKind, raw}; +/// use std::convert::TryInto; +/// // Get a raw::Agent from somewhere +/// let raw_agent: raw::Agent = panic!(); +/// // Convert it +/// let agent: AgentKind = raw_agent.try_into().unwrap(); +/// ``` #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub enum AgentKind { Player(Player), @@ -220,6 +238,17 @@ impl AgentKind { } } +impl TryFrom<raw::Agent> for AgentKind { + type Error = EvtcError; + /// Convenience method to avoid manual borrowing. + /// + /// Note that this conversion will consume the agent, so if you plan on re-using it, use the + /// `TryFrom<&raw::Agent>` implementation that works with a reference. + fn try_from(raw_agent: raw::Agent) -> Result<Self, Self::Error> { + Self::try_from(&raw_agent) + } +} + impl TryFrom<&raw::Agent> for AgentKind { type Error = EvtcError; @@ -264,6 +293,26 @@ impl TryFrom<&raw::Agent> for AgentKind { /// All of these agents share some common fields, which are the ones accessible in `Agent<Kind>`. /// The kind can be retrieved using [`.kind()`][Agent::kind], which can be matched on. /// +/// # Obtaining an agent +/// +/// The normal way to obtain the agents is to use the [`.agents()`](Log::agents) method on a +/// [`Log`][Log], or one of the other accessor methods (like [`.players()`][Log::players] or +/// [`.agent_by_addr()`][Log::agent_by_addr]). +/// +/// In the cases where you already have a [`raw::Agent`][raw::Agent] available, you can also +/// convert it to an [`Agent`][Agent] by using the standard +/// [`TryFrom`][TryFrom]/[`TryInto`][std::convert::TryInto] traits: +/// +/// ```no_run +/// # use evtclib::{Agent, raw}; +/// use std::convert::TryInto; +/// let raw_agent: raw::Agent = panic!(); +/// let agent: Agent = raw_agent.try_into().unwrap(); +/// ``` +/// +/// Note that you can convert references as well, so if you plan on re-using the raw agent +/// afterwards, you should opt for `Agent::try_from(&raw_agent)` instead. +/// /// # The `Kind` parameter /// /// The type parameter is not actually used and only exists at the type level. It can be used to @@ -375,6 +424,18 @@ impl TryFrom<&raw::Agent> for Agent { } } +impl TryFrom<raw::Agent> for Agent { + type Error = EvtcError; + + /// Convenience method to avoid manual borrowing. + /// + /// Note that this conversion will consume the agent, so if you plan on re-using it, use the + /// `TryFrom<&raw::Agent>` implementation that works with a reference. + fn try_from(raw_agent: raw::Agent) -> Result<Self, Self::Error> { + Agent::try_from(&raw_agent) + } +} + impl<Kind> Agent<Kind> { /// Unconditionally change the tagged type. #[inline] |