Commit cb969f7a authored by Andreas C. Osowski's avatar Andreas C. Osowski Committed by Facebook Github Bot

Add hash and equal_to implementations to folly::Uri

Summary:
Adds a default inline implementation for `std::hash` and `std::equal_to` to `folly::Uri`.
Both do the comparison / hash based upon the return value of `folly::Uri::toString`.

Reviewed By: yfeldblum

Differential Revision: D4711506

fbshipit-source-id: f4c2a9de8d4302fd315a9f31329cc8ba9f5e0409
parent ebebe68b
......@@ -18,10 +18,40 @@
#error This file may only be included from folly/Uri.h
#endif
#include <functional>
#include <tuple>
#include <folly/Conv.h>
#include <folly/Hash.h>
namespace folly {
namespace uri_detail {
using UriTuple = std::tuple<
const fbstring&,
const fbstring&,
const fbstring&,
const fbstring&,
uint16_t,
const fbstring&,
const fbstring&,
const fbstring&>;
inline UriTuple as_tuple(const folly::Uri& k) {
return UriTuple(
k.scheme(),
k.username(),
k.password(),
k.host(),
k.port(),
k.path(),
k.query(),
k.fragment());
}
} // namespace uri_detail
template <class String>
String Uri::toString() const {
String str;
......@@ -49,4 +79,23 @@ String Uri::toString() const {
return str;
}
} // namespace folly
} // namespace folly
namespace std {
template <>
struct hash<folly::Uri> {
std::size_t operator()(const folly::Uri& k) const {
return std::hash<folly::uri_detail::UriTuple>{}(
folly::uri_detail::as_tuple(k));
}
};
template <>
struct equal_to<folly::Uri> {
bool operator()(const folly::Uri& a, const folly::Uri& b) const {
return folly::uri_detail::as_tuple(a) == folly::uri_detail::as_tuple(b);
}
};
} // namespace std
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment