Skip to content

Commit

Permalink
Merge pull request #8
Browse files Browse the repository at this point in the history
Use a struct for rpc result type
  • Loading branch information
uatuko authored Dec 15, 2023
2 parents 0cf174e + 3b509cb commit 6c6eca6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ sources += $(shell find $(libdir) -type f -name '*.h' -o -name '*.cpp')
sources += $(shell find $(srcdir) -type f -name '*.h' -o -name '*.cpp')

.PHONY: all clean examples lint lint\:ci lint\:fix
.SILENT: lint lint\:fix

all: lint
all: $(buildfile)
cmake --build $(builddir) --target all

$(buildfile):
cmake -B $(builddir) -G Ninja
Expand All @@ -35,4 +37,3 @@ lint\:ci:

lint\:fix:
clang-format --style=file -i $(sources)

13 changes: 8 additions & 5 deletions lib/grpcxx/rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ namespace grpcxx {
template <fixed_string M, typename T, typename U> struct rpc {
static constexpr std::string_view method{M};

using method_type = fixed_string_t<M>;
using request_type = T;
using response_type = U;

using method_type = fixed_string_t<M>;
using request_type = T;
using response_type = U;
using optional_response_type = std::optional<response_type>;
using result_type = std::pair<status, optional_response_type>;

struct result_type {
class status status;
optional_response_type response;
};

request_type map(std::string_view data) const {
constexpr bool can_map = requires(request_type t) {
Expand Down
9 changes: 5 additions & 4 deletions lib/grpcxx/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ concept rpc_type = requires(T t) {

// Result
typename T::result_type;
requires std::same_as<
typename T::result_type,
std::pair<grpcxx::status, typename T::optional_response_type>>;
requires requires(typename T::result_type t) {
{ t.status } -> std::same_as<grpcxx::status &>;
{ t.response } -> std::same_as<typename T::optional_response_type &>;
};
};
} // namespace concepts

Expand All @@ -53,7 +54,7 @@ template <fixed_string N, concepts::rpc_type... R> class service {
auto req = rpc.map(data);
auto result = std::invoke(&I::template call<type>, impl, req);

return {result.first, rpc.map(result.second)};
return {result.status, rpc.map(result.response)};
};

_handlers.insert({rpc.method, handler});
Expand Down

0 comments on commit 6c6eca6

Please sign in to comment.