mark parts of EdenTimestamp noexcept

Summary:
Make it clear (especially for the upcoming InodeMetadata struct) which
operations with EdenTimestamp and InodeTimestamps will never throw.

Reviewed By: simpkins

Differential Revision: D7920219

fbshipit-source-id: 5917da51b8128455893a1480def6f2c1c8de13d4
This commit is contained in:
Chad Austin 2018-05-09 16:20:44 -07:00 committed by Facebook Github Bot
parent c51fea6a85
commit d9a6089dd5
2 changed files with 23 additions and 14 deletions

View File

@ -39,15 +39,17 @@ constexpr int64_t kLargestRepresentableSec = 16299260425ll;
constexpr uint32_t kLargestRepresentableNsec = 709551615u;
struct ClampPolicy {
static uint64_t minimum(timespec /*ts*/) {
static constexpr bool is_noexcept = true;
static uint64_t minimum(timespec /*ts*/) noexcept {
return 0;
}
static uint64_t maximum(timespec /*ts*/) {
static uint64_t maximum(timespec /*ts*/) noexcept {
return ~0ull;
}
};
struct ThrowPolicy {
static constexpr bool is_noexcept = false;
static uint64_t minimum(timespec ts) {
throw std::underflow_error(folly::to<std::string>(
"underflow converting timespec (",
@ -67,7 +69,7 @@ struct ThrowPolicy {
};
template <typename OutOfRangePolicy>
uint64_t repFromTimespec(timespec ts) {
uint64_t repFromTimespec(timespec ts) noexcept(OutOfRangePolicy::is_noexcept) {
if (ts.tv_sec < -kEpochOffsetSeconds) {
return OutOfRangePolicy::minimum(ts);
} else if (
@ -105,13 +107,13 @@ timespec repToTimespec(uint64_t nsec) {
} // namespace
EdenTimestamp::EdenTimestamp(timespec ts, Clamp)
EdenTimestamp::EdenTimestamp(timespec ts, Clamp) noexcept
: nsec_{repFromTimespec<ClampPolicy>(ts)} {}
EdenTimestamp::EdenTimestamp(timespec ts, ThrowIfOutOfRange)
: nsec_{repFromTimespec<ThrowPolicy>(ts)} {}
timespec EdenTimestamp::toTimespec() const {
timespec EdenTimestamp::toTimespec() const noexcept {
return repToTimespec(nsec_);
}

View File

@ -49,14 +49,14 @@ class EdenTimestamp {
* Constructs an EdenTimestamp given a raw uint64_t in nanoseconds since
* the earliest representable ext4 timestamp.
*/
explicit EdenTimestamp(uint64_t nsec) : nsec_(nsec) {}
explicit EdenTimestamp(uint64_t nsec) noexcept : nsec_(nsec) {}
/**
* Converts a timespec to an EdenTimestamp.
*
* If the timespec is out of range, it is clamped.
*/
explicit EdenTimestamp(timespec ts, Clamp = clamp);
explicit EdenTimestamp(timespec ts, Clamp = clamp) noexcept;
/**
* Converts a timespec to an EdenTimestamp.
@ -68,7 +68,7 @@ class EdenTimestamp {
EdenTimestamp& operator=(const EdenTimestamp&) = default;
EdenTimestamp& operator=(timespec ts) {
EdenTimestamp& operator=(timespec ts) noexcept {
return *this = EdenTimestamp{ts};
}
@ -83,12 +83,12 @@ class EdenTimestamp {
/**
* Returns a timespec representing duration since the unix epoch.
*/
timespec toTimespec() const;
timespec toTimespec() const noexcept;
/**
* Returns the raw representation -- should be for testing only. :)
*/
uint64_t asRawRepresentation() const {
uint64_t asRawRepresentation() const noexcept {
return nsec_;
}
@ -107,18 +107,18 @@ struct InodeTimestamps {
/**
* Initializes all timestamps to zero.
*/
InodeTimestamps() {}
InodeTimestamps() = default;
/**
* Initializes all timestamps from the same value.
*/
explicit InodeTimestamps(const timespec& ts)
: atime(ts), mtime(ts), ctime(ts) {}
explicit InodeTimestamps(const timespec& ts) noexcept
: atime{ts}, mtime{ts}, ctime{ts} {}
/**
* Assigns the specified ts to atime, mtime, and ctime.
*/
void setAll(const timespec& ts) {
void setAll(const timespec& ts) noexcept {
atime = ts;
mtime = ts;
ctime = ts;
@ -138,5 +138,12 @@ struct InodeTimestamps {
void applyToStat(struct stat& st) const;
};
static_assert(noexcept(EdenTimestamp{}), "");
static_assert(noexcept(EdenTimestamp{timespec{}}), "");
static_assert(noexcept(EdenTimestamp{uint64_t{}}), "");
static_assert(noexcept(InodeTimestamps{}), "");
static_assert(noexcept(InodeTimestamps{timespec{}}), "");
} // namespace eden
} // namespace facebook