diff options
author | Isis Lovecruft <isis@torproject.org> | 2018-03-21 01:44:59 +0000 |
---|---|---|
committer | Isis Lovecruft <isis@torproject.org> | 2018-04-02 19:34:24 +0000 |
commit | fa15ea104d5b9f05192afa3a023d0e2405c3842a (patch) | |
tree | 70764a688af88458962100cfb4bea8cf4e21b452 /src/rust | |
parent | 3c47d31e1f29a016e2f0f21ca8445430ed7aad0a (diff) | |
download | tor-fa15ea104d5b9f05192afa3a023d0e2405c3842a.tar.gz tor-fa15ea104d5b9f05192afa3a023d0e2405c3842a.zip |
rust: Add macro for `impl ToString for {Unvalidated}ProtoEntry`.
This implements conversions from either a ProtoEntry or an UnvalidatedProtoEntry
into a String, for use in replacing such functions as
`protover::write_vote_to_string()`.
* ADD macro for implementing ToString trait for ProtoEntry and
UnvalidatedProtoEntry.
* FIXES part of #24031: https://bugs.torproject.org/24031
Diffstat (limited to 'src/rust')
-rw-r--r-- | src/rust/protover/protover.rs | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/rust/protover/protover.rs b/src/rust/protover/protover.rs index 847406ca2d..2a1a5df9e9 100644 --- a/src/rust/protover/protover.rs +++ b/src/rust/protover/protover.rs @@ -226,6 +226,27 @@ impl FromStr for ProtoEntry { } } +/// Generate an implementation of `ToString` for either a `ProtoEntry` or an +/// `UnvalidatedProtoEntry`. +macro_rules! impl_to_string_for_proto_entry { + ($t:ty) => ( + impl ToString for $t { + fn to_string(&self) -> String { + let mut parts: Vec<String> = Vec::new(); + + for (protocol, versions) in self.iter() { + parts.push(format!("{}={}", protocol.to_string(), versions.to_string())); + } + parts.sort_unstable(); + parts.join(" ") + } + } + ) +} + +impl_to_string_for_proto_entry!(ProtoEntry); +impl_to_string_for_proto_entry!(UnvalidatedProtoEntry); + /// A `ProtoEntry`, but whose `Protocols` can be any `UnknownProtocol`, not just /// the supported ones enumerated in `Protocols`. The protocol versions are /// validated, however. |