Skip to content

Commit c8d66aa

Browse files
otfacebook-github-bot
authored andcommitted
IOBuf::toString()
Summary: `to<std::string>()` becomes awkward in templates, if called on an object with dependent type it needs to be invoked as `obj->template to<std::string>()`. Since 99.999% of the use cases for `to()` are with `std::string`, add a convenience non-template method. Reviewed By: ilvokhin Differential Revision: D58672590 fbshipit-source-id: 6aeddff43feef1bd0587aadeda1f51a59e7d93ae
1 parent f89a101 commit c8d66aa

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

folly/io/IOBuf.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <iterator>
2929
#include <limits>
3030
#include <memory>
31+
#include <string>
3132
#include <type_traits>
3233

3334
#include <glog/logging.h>
@@ -1787,17 +1788,24 @@ class IOBuf {
17871788
void appendTo(Container& container) const;
17881789

17891790
/**
1790-
* Dump the chain data into a container.
1791+
* Returns a container containing the chain data.
17911792
*
17921793
* @copydetails appendTo(Container&) const
17931794
*
17941795
* @tparam Container The type of container to return.
17951796
*
1796-
* @returns A Container whose data equals the coalseced data of this chain
1797+
* @returns A Container whose data equals the coalesced data of this chain
17971798
*/
17981799
template <class Container>
17991800
Container to() const;
18001801

1802+
/**
1803+
* Convenience version of to<std::string>() that works when called
1804+
* on a dependent name in a template function without having to use
1805+
* the "template" keyword.
1806+
*/
1807+
std::string toString() const { return to<std::string>(); }
1808+
18011809
/**
18021810
* Get an iovector suitable for e.g. writev()
18031811
*

folly/io/test/IOBufTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,7 @@ TEST(IOBuf, AppendTo) {
18051805

18061806
IOBuf buf;
18071807
EXPECT_EQ(buf.to<std::string>(), "");
1808+
EXPECT_EQ(buf.toString(), "");
18081809

18091810
auto temp = &buf;
18101811
temp->insertAfterThisOne(IOBuf::copyBuffer("Hello"));

0 commit comments

Comments
 (0)