Skip to content

Commit

Permalink
Merge pull request #59 from boostorg/42-the-names-from_bulk-and-to_bu…
Browse files Browse the repository at this point in the history
…lk-are-too-generic-for-adl-customization-points

Prefix to_ and from_bulk with boost_redis_ (boost review).
  • Loading branch information
mzimbres committed Feb 2, 2023
2 parents c1ce835 + 4b07b6d commit 0c5ff09
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -303,21 +303,21 @@ Sending a request to Redis is performed with `boost::redis::connection::async_ex
The `resp3::request::push` and `resp3::request::push_range` member functions work
with integer data types e.g. `int` and `std::string` out of the box.
To send your own data type define a `to_bulk` function like this
To send your own data type define a `boost_redis_to_bulk` function like this
```cpp
// User defined type.
struct mystruct {...};
// Serialize it in to_bulk.
void to_bulk(std::pmr::string& to, mystruct const& obj)
// Serialize it in boost_redis_to_bulk.
void boost_redis_to_bulk(std::pmr::string& to, mystruct const& obj)
{
std::string dummy = "Dummy serializaiton string.";
boost::redis::resp3::to_bulk(to, dummy);
boost::redis::resp3::boost_redis_to_bulk(to, dummy);
}
```

Once `to_bulk` is defined and visible over ADL `mystruct` can
Once `boost_redis_to_bulk` is defined and visible over ADL `mystruct` can
be passed to the `request`

```cpp
Expand Down Expand Up @@ -535,11 +535,11 @@ As mentioned in the serialization section, it is common practice to
serialize data before sending it to Redis e.g. as json strings. For
performance and convenience reasons, we may also want to deserialize
responses directly in their final data structure. Boost.Redis supports this
use case by calling a user provided `from_bulk` function while parsing
use case by calling a user provided `boost_redis_from_bulk` function while parsing
the response. For example
```cpp
void from_bulk(mystruct& obj, char const* p, std::size_t size, boost::system::error_code& ec)
void boost_redis_from_bulk(mystruct& obj, char const* p, std::size_t size, boost::system::error_code& ec)
{
// Deserializes p into obj.
}
Expand Down Expand Up @@ -600,7 +600,7 @@ from Redis with `HGETALL`, some of the options are
* `std::vector<node<std::string>`: Works always.
* `std::vector<std::string>`: Efficient and flat, all elements as string.
* `std::map<std::string, std::string>`: Efficient if you need the data as a `std::map`.
* `std::map<U, V>`: Efficient if you are storing serialized data. Avoids temporaries and requires `from_bulk` for `U` and `V`.
* `std::map<U, V>`: Efficient if you are storing serialized data. Avoids temporaries and requires `boost_redis_from_bulk` for `U` and `V`.

In addition to the above users can also use unordered versions of the
containers. The same reasoning applies to sets e.g. `SMEMBERS`
Expand Down Expand Up @@ -863,6 +863,12 @@ Acknowledgement to people that helped shape Boost.Redis

## Changelog

### master

* Renames the project to Boost.Redis and moves the code into namespace `boost::redis`.
* As pointed out in the reviews `to_buld` and `from_buld` were too generic
for ADL customization. They gained the prefix `boost_redis_`.

### v1.4.0-1

* Renames `retry_on_connection_lost` to `cancel_if_unresponded`. (v1.4.1)
Expand Down
6 changes: 3 additions & 3 deletions examples/cpp20_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ auto tag_invoke(value_to_tag<user>, value const& jv)
}

// Serialization
void to_bulk(std::string& to, user const& u)
void boost_redis_to_bulk(std::string& to, user const& u)
{
redis::resp3::to_bulk(to, serialize(value_from(u)));
redis::resp3::boost_redis_to_bulk(to, serialize(value_from(u)));
}

void from_bulk(user& u, std::string_view sv, boost::system::error_code&)
void boost_redis_from_bulk(user& u, std::string_view sv, boost::system::error_code&)
{
value jv = parse(sv);
u = value_to<user>(jv);
Expand Down
22 changes: 11 additions & 11 deletions include/boost/redis/adapter/detail/adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ namespace boost::redis::adapter::detail {
// Serialization.

template <class T>
auto from_bulk(T& i, std::string_view sv, system::error_code& ec) -> typename std::enable_if<std::is_integral<T>::value, void>::type
auto boost_redis_from_bulk(T& i, std::string_view sv, system::error_code& ec) -> typename std::enable_if<std::is_integral<T>::value, void>::type
{
auto const res = std::from_chars(sv.data(), sv.data() + std::size(sv), i);
if (res.ec != std::errc())
ec = error::not_a_number;
}

inline
void from_bulk(bool& t, std::string_view sv, system::error_code&)
void boost_redis_from_bulk(bool& t, std::string_view sv, system::error_code&)
{
t = *sv.data() == 't';
}

inline
void from_bulk(double& d, std::string_view sv, system::error_code& ec)
void boost_redis_from_bulk(double& d, std::string_view sv, system::error_code& ec)
{
auto const res = std::from_chars(sv.data(), sv.data() + std::size(sv), d);
if (res.ec != std::errc())
Expand All @@ -56,7 +56,7 @@ void from_bulk(double& d, std::string_view sv, system::error_code& ec)

template <class CharT, class Traits, class Allocator>
void
from_bulk(
boost_redis_from_bulk(
std::basic_string<CharT, Traits, Allocator>& s,
std::string_view sv,
system::error_code&)
Expand Down Expand Up @@ -128,7 +128,7 @@ class simple_impl {
return;
}

from_bulk(result, n.value, ec);
boost_redis_from_bulk(result, n.value, ec);
}
};

Expand Down Expand Up @@ -165,7 +165,7 @@ class set_impl {
}

typename Result::key_type obj;
from_bulk(obj, nd.value, ec);
boost_redis_from_bulk(obj, nd.value, ec);
hint_ = result.insert(hint_, std::move(obj));
}
};
Expand Down Expand Up @@ -205,11 +205,11 @@ class map_impl {

if (on_key_) {
typename Result::key_type obj;
from_bulk(obj, nd.value, ec);
boost_redis_from_bulk(obj, nd.value, ec);
current_ = result.insert(current_, {std::move(obj), {}});
} else {
typename Result::mapped_type obj;
from_bulk(obj, nd.value, ec);
boost_redis_from_bulk(obj, nd.value, ec);
current_->second = std::move(obj);
}

Expand Down Expand Up @@ -237,7 +237,7 @@ class vector_impl {
result.reserve(result.size() + m * nd.aggregate_size);
} else {
result.push_back({});
from_bulk(result.back(), nd.value, ec);
boost_redis_from_bulk(result.back(), nd.value, ec);
}
}
};
Expand Down Expand Up @@ -277,7 +277,7 @@ class array_impl {
}

BOOST_ASSERT(nd.aggregate_size == 1);
from_bulk(result.at(i_), nd.value, ec);
boost_redis_from_bulk(result.at(i_), nd.value, ec);
}

++i_;
Expand Down Expand Up @@ -307,7 +307,7 @@ struct list_impl {
}

result.push_back({});
from_bulk(result.back(), nd.value, ec);
boost_redis_from_bulk(result.back(), nd.value, ec);
}
}
};
Expand Down
21 changes: 10 additions & 11 deletions include/boost/redis/resp3/request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// the value type is a pair.

namespace boost::redis::resp3 {

constexpr char const* separator = "\r\n";

/** @brief Adds a bulk to the request.
Expand All @@ -29,10 +28,10 @@ constexpr char const* separator = "\r\n";
* structures in a request. For example
*
* @code
* void to_bulk(std::string& to, mystruct const& obj)
* void boost_redis_to_bulk(std::string& to, mystruct const& obj)
* {
* auto const str = // Convert obj to a string.
* resp3::to_bulk(to, str);
* boost_redis_to_bulk(to, str);
* }
* @endcode
*
Expand All @@ -42,7 +41,7 @@ constexpr char const* separator = "\r\n";
* See more in @ref serialization.
*/
template <class Request>
void to_bulk(Request& to, std::string_view data)
void boost_redis_to_bulk(Request& to, std::string_view data)
{
auto const str = std::to_string(data.size());

Expand All @@ -54,10 +53,10 @@ void to_bulk(Request& to, std::string_view data)
}

template <class Request, class T, typename = typename std::enable_if<std::is_integral<T>::value>::type>
void to_bulk(Request& to, T n)
void boost_redis_to_bulk(Request& to, T n)
{
auto const s = std::to_string(n);
to_bulk(to, std::string_view{s});
boost_redis_to_bulk(to, std::string_view{s});
}

namespace detail {
Expand All @@ -70,7 +69,7 @@ struct add_bulk_impl {
static void add(Request& to, T const& from)
{
using namespace boost::redis::resp3;
to_bulk(to, from);
boost_redis_to_bulk(to, from);
}
};

Expand All @@ -82,7 +81,7 @@ struct add_bulk_impl<std::tuple<Ts...>> {
auto f = [&](auto const&... vs)
{
using namespace boost::redis::resp3;
(to_bulk(to, vs), ...);
(boost_redis_to_bulk(to, vs), ...);
};

std::apply(f, t);
Expand All @@ -95,8 +94,8 @@ struct add_bulk_impl<std::pair<U, V>> {
static void add(Request& to, std::pair<U, V> const& from)
{
using namespace boost::redis::resp3;
to_bulk(to, from.first);
to_bulk(to, from.second);
boost_redis_to_bulk(to, from.first);
boost_redis_to_bulk(to, from.second);
}
};

Expand Down Expand Up @@ -162,7 +161,7 @@ void add_separator(Request& to)
* \remarks
*
* \li Non-string types will be converted to string by using \c
* to_bulk, which must be made available over ADL.
* boost_redis_to_bulk, which must be made available over ADL.
* \li Uses a std::string for internal storage.
*/
class request {
Expand Down

0 comments on commit 0c5ff09

Please sign in to comment.