Skip to content

Commit

Permalink
refac: add [[nodiscard]] to public methods of Buffer like classes
Browse files Browse the repository at this point in the history
  • Loading branch information
chokobole committed Dec 9, 2023
1 parent 3b33d74 commit 6ce3319
Show file tree
Hide file tree
Showing 22 changed files with 71 additions and 67 deletions.
58 changes: 31 additions & 27 deletions tachyon/base/buffer/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ class TACHYON_EXPORT Buffer {

size_t buffer_len() const { return buffer_len_; }

bool Done() const { return buffer_offset_ == buffer_len_; }
[[nodiscard]] bool Done() const { return buffer_offset_ == buffer_len_; }

// Returns false either
// 1) |buffer_offset| + |size| overflows.
// 2) if it tries to read more than |buffer_len_|.
bool ReadAt(size_t buffer_offset, uint8_t* ptr, size_t size) const;
[[nodiscard]] bool ReadAt(size_t buffer_offset, uint8_t* ptr,
size_t size) const;

template <
typename Ptr, typename T = std::remove_pointer_t<Ptr>,
Expand Down Expand Up @@ -112,67 +113,68 @@ class TACHYON_EXPORT Buffer {
template <
typename T,
std::enable_if_t<internal::IsNonBuiltinSerializable<T>::value>* = nullptr>
bool ReadAt(size_t buffer_offset, T* value) const {
[[nodiscard]] bool ReadAt(size_t buffer_offset, T* value) const {
buffer_offset_ = buffer_offset;
return Copyable<T>::ReadFrom(*this, value);
}

template <typename T, size_t N>
bool ReadAt(size_t buffer_offset, T (&array)[N]) const {
[[nodiscard]] bool ReadAt(size_t buffer_offset, T (&array)[N]) const {
buffer_offset_ = buffer_offset;
for (size_t i = 0; i < N; ++i) {
if (!Read(&array[i])) return false;
}
return true;
}

bool Read(uint8_t* ptr, size_t size) const {
[[nodiscard]] bool Read(uint8_t* ptr, size_t size) const {
return ReadAt(buffer_offset_, ptr, size);
}

template <typename T>
bool Read(T&& value) const {
[[nodiscard]] bool Read(T&& value) const {
return ReadAt(buffer_offset_, std::forward<T>(value));
}

template <typename T>
bool ReadMany(T&& value) const {
[[nodiscard]] bool ReadMany(T&& value) const {
return Read(std::forward<T>(value));
}

template <typename T, typename... Args>
bool ReadMany(T&& value, Args&&... args) const {
[[nodiscard]] bool ReadMany(T&& value, Args&&... args) const {
if (!Read(std::forward<T>(value))) return false;
return ReadMany(std::forward<Args>(args)...);
}

template <typename T, size_t N>
bool ReadManyAt(size_t buffer_offset, T&& value) const {
[[nodiscard]] bool ReadManyAt(size_t buffer_offset, T&& value) const {
return ReadAt(buffer_offset, std::forward<T>(value));
}

template <typename T, typename... Args>
bool ReadManyAt(size_t buffer_offset, T&& value, Args&&... args) const {
[[nodiscard]] bool ReadManyAt(size_t buffer_offset, T&& value,
Args&&... args) const {
if (!ReadAt(buffer_offset, std::forward<T>(value))) return false;
return ReadManyAt(buffer_offset, std::forward<Args>(args)...);
}

bool Write(const uint8_t* ptr, size_t size) {
[[nodiscard]] bool Write(const uint8_t* ptr, size_t size) {
return WriteAt(buffer_offset_, ptr, size);
}

template <typename T>
bool Write(const T& value) {
[[nodiscard]] bool Write(const T& value) {
return WriteAt(buffer_offset_, value);
}

template <typename T>
bool WriteMany(const T& value) {
[[nodiscard]] bool WriteMany(const T& value) {
return Write(value);
}

template <typename T, typename... Args>
bool WriteMany(const T& value, const Args&... args) {
[[nodiscard]] bool WriteMany(const T& value, const Args&... args) {
if (!Write(value)) return false;
return WriteMany(args...);
}
Expand All @@ -181,20 +183,21 @@ class TACHYON_EXPORT Buffer {
// 1) |buffer_offset| + |size| overflows.
// 2) if it is not growable and it tries to write more than
// |buffer_len_|.
bool WriteAt(size_t buffer_offset, const uint8_t* ptr, size_t size);
[[nodiscard]] bool WriteAt(size_t buffer_offset, const uint8_t* ptr,
size_t size);

bool Write16BEAt(size_t buffer_offset, uint16_t ptr);
bool Write16LEAt(size_t buffer_offset, uint16_t ptr);
bool Write32BEAt(size_t buffer_offset, uint32_t ptr);
bool Write32LEAt(size_t buffer_offset, uint32_t ptr);
bool Write64BEAt(size_t buffer_offset, uint64_t ptr);
bool Write64LEAt(size_t buffer_offset, uint64_t ptr);
[[nodiscard]] bool Write16BEAt(size_t buffer_offset, uint16_t ptr);
[[nodiscard]] bool Write16LEAt(size_t buffer_offset, uint16_t ptr);
[[nodiscard]] bool Write32BEAt(size_t buffer_offset, uint32_t ptr);
[[nodiscard]] bool Write32LEAt(size_t buffer_offset, uint32_t ptr);
[[nodiscard]] bool Write64BEAt(size_t buffer_offset, uint64_t ptr);
[[nodiscard]] bool Write64LEAt(size_t buffer_offset, uint64_t ptr);

#define DEFINE_WRITE_AT(bytes, bits, type) \
template <typename T, \
std::enable_if_t<internal::IsBuiltinSerializable<T>::value && \
(sizeof(T) == bytes)>* = nullptr> \
bool WriteAt(size_t buffer_offset, T value) { \
[[nodiscard]] bool WriteAt(size_t buffer_offset, T value) { \
switch (endian_) { \
case Endian::kBig: \
return Write##bits##BEAt(buffer_offset, value); \
Expand All @@ -217,31 +220,32 @@ class TACHYON_EXPORT Buffer {
std::enable_if_t<internal::IsBuiltinSerializable<T>::value &&
!((sizeof(T) == 2) || (sizeof(T) == 4) ||
(sizeof(T) == 8))>* = nullptr>
bool WriteAt(size_t buffer_offset, T value) {
[[nodiscard]] bool WriteAt(size_t buffer_offset, T value) {
return WriteAt(buffer_offset, reinterpret_cast<const uint8_t*>(&value),
sizeof(T));
}

template <
typename T,
std::enable_if_t<internal::IsNonBuiltinSerializable<T>::value>* = nullptr>
bool WriteAt(size_t buffer_offset, const T& value) {
[[nodiscard]] bool WriteAt(size_t buffer_offset, const T& value) {
buffer_offset_ = buffer_offset;
return Copyable<T>::WriteTo(value, this);
}

template <typename T>
bool WriteManyAt(size_t buffer_offset, const T& value) {
[[nodiscard]] bool WriteManyAt(size_t buffer_offset, const T& value) {
return WriteAt(buffer_offset, value);
}

template <typename T, typename... Args>
bool WriteManyAt(size_t buffer_offset, const T& value, const Args&... args) {
[[nodiscard]] bool WriteManyAt(size_t buffer_offset, const T& value,
const Args&... args) {
if (!WriteAt(buffer_offset, value)) return false;
return WriteManyAt(buffer_offset, args...);
}

virtual bool Grow(size_t size) { return false; }
[[nodiscard]] virtual bool Grow(size_t size) { return false; }

protected:
bool Read16BEAt(size_t buffer_offset, uint16_t* ptr) const;
Expand Down
2 changes: 1 addition & 1 deletion tachyon/base/buffer/string_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TACHYON_EXPORT StringBuffer : public Buffer {
}
~StringBuffer() override = default;

bool Grow(size_t size) override {
[[nodiscard]] bool Grow(size_t size) override {
owned_buffer_.resize(size);
buffer_ = owned_buffer_.data();
buffer_len_ = owned_buffer_.size();
Expand Down
2 changes: 1 addition & 1 deletion tachyon/base/buffer/vector_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TACHYON_EXPORT VectorBuffer : public Buffer {
}
~VectorBuffer() override = default;

bool Grow(size_t size) override {
[[nodiscard]] bool Grow(size_t size) override {
owned_buffer_.resize(size);
buffer_ = owned_buffer_.data();
buffer_len_ = owned_buffer_.size();
Expand Down
4 changes: 2 additions & 2 deletions tachyon/crypto/commitments/kzg/kzg_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ TEST_F(KZGTest, Copyable) {
ASSERT_TRUE(expected.UnsafeSetup(N));

base::VectorBuffer write_buf;
EXPECT_TRUE(write_buf.Write(expected));
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);

PCS value;
EXPECT_TRUE(write_buf.Read(&value));
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected.g1_powers_of_tau(), value.g1_powers_of_tau());
EXPECT_EQ(expected.g1_powers_of_tau_lagrange(),
Expand Down
4 changes: 2 additions & 2 deletions tachyon/crypto/commitments/pedersen/pedersen_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ TEST_F(PedersenTest, Copyable) {
ASSERT_TRUE(expected.Setup());

base::VectorBuffer write_buf;
EXPECT_TRUE(write_buf.Write(expected));
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);

VCS value;
EXPECT_TRUE(write_buf.Read(&value));
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected.h(), value.h());
EXPECT_EQ(expected.generators(), value.generators());
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/base/big_int_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ TEST(BigIntTest, Copyable) {
BigInt<2> value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);

write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));
EXPECT_EQ(value, expected);
}

Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/affine_point_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ TEST_F(AffinePointTest, Copyable) {
test::AffinePoint value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/jacobian_point_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ TEST_F(JacobianPointTest, Copyable) {
test::JacobianPoint value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/point_xyzz_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ TEST_F(PointXYZZTest, Copyable) {
test::PointXYZZ value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/elliptic_curves/projective_point_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ TEST_F(ProjectivePointTest, Copyable) {
test::ProjectivePoint value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/finite_fields/fp12_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ TEST_F(Fp12Test, Copyable) {
F value;

base::VectorBuffer write_buf;
EXPECT_TRUE(write_buf.Write(expected));
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
EXPECT_TRUE(write_buf.Read(&value));
ASSERT_TRUE(write_buf.Read(&value));
EXPECT_EQ(expected, value);
}

Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/finite_fields/fp2_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ TEST_F(Fp2Test, Copyable) {
F value;

base::VectorBuffer write_buf;
EXPECT_TRUE(write_buf.Write(expected));
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
EXPECT_TRUE(write_buf.Read(&value));
ASSERT_TRUE(write_buf.Read(&value));
EXPECT_EQ(expected, value);
}

Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/finite_fields/fp6_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ TEST_F(Fp6Test, Copyable) {
F value;

base::VectorBuffer write_buf;
EXPECT_TRUE(write_buf.Write(expected));
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
EXPECT_TRUE(write_buf.Read(&value));
ASSERT_TRUE(write_buf.Read(&value));
EXPECT_EQ(expected, value);
}

Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/finite_fields/prime_field_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ TEST_F(PrimeFieldTest, Copyable) {
GF7 value;

base::VectorBuffer write_buf;
EXPECT_TRUE(write_buf.Write(expected));
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));
EXPECT_EQ(expected, value);
}

Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/geometry/point2_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ TEST(Point2Test, Copyable) {
Point2GF7 value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/geometry/point3_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ TEST(Point3Test, Copyable) {
Point3GF7 value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
4 changes: 2 additions & 2 deletions tachyon/math/geometry/point4_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ TEST(Point4Test, Copyable) {
Point4GF7 value;

base::VectorBuffer write_buf;
write_buf.Write(expected);
ASSERT_TRUE(write_buf.Write(expected));

write_buf.set_buffer_offset(0);
write_buf.Read(&value);
ASSERT_TRUE(write_buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,10 @@ TEST_F(UnivariateDensePolynomialTest, Copyable) {
Poly value;

base::VectorBuffer buf;
buf.Write(expected);
ASSERT_TRUE(buf.Write(expected));

buf.set_buffer_offset(0);
buf.Read(&value);
ASSERT_TRUE(buf.Read(&value));

EXPECT_EQ(expected, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ TEST_F(UnivariateEvaluationsTest, DivScalar) {

TEST_F(UnivariateEvaluationsTest, Copyable) {
base::VectorBuffer buf;
buf.Write(polys_[0]);
ASSERT_TRUE(buf.Write(polys_[0]));

buf.set_buffer_offset(0);
Poly value;
buf.Read(&value);
ASSERT_TRUE(buf.Read(&value));

EXPECT_EQ(polys_[0], value);
}
Expand Down
Loading

0 comments on commit 6ce3319

Please sign in to comment.