[result] fix Result.map() issue with copying the value when the result was in error

pull/824/head
Timothy Stack 4 years ago
parent 7476dd5bb9
commit 9a073e80e7

@ -151,6 +151,12 @@ FILES = [
]
while True:
sys.stdout.write(FILES[1][1].next())
sys.stdout.flush()
time.sleep(random.uniform(1.0, 2.0))
for fname, gen in FILES:
for i in range(random.randrange(0, 4)):
with open(fname, "a+") as fp:
fp.write(gen.next())
if random.uniform(0.0, 1.0) < 0.010:
fp.truncate(0)
time.sleep(random.uniform(0.00, 0.01))
if random.uniform(0.0, 1.0) < 0.001:
os.remove(fname)

@ -772,10 +772,10 @@ struct Result {
template<typename Func>
auto map(Func func) const {
auto value = this->storage().template get<T>();
using return_type = decltype(func(value));
using return_type = decltype(func(T{}));
if (this->isOk()) {
auto value = this->storage().template get<T>();
auto res = func(value);
return Result<return_type, E>(types::Ok<return_type>(std::move(res)));
}

@ -774,7 +774,9 @@ Result<shared_buffer_ref, std::string> line_buffer::read_range(const file_range
return Err(string("out-of-bounds"));
}
this->fill_range(fr.fr_offset, fr.fr_size);
if (!this->fill_range(fr.fr_offset, fr.fr_size)) {
return Err(string("unable to read file"));
}
line_start = this->get_range(fr.fr_offset, avail);
if (fr.fr_size > avail) {

@ -139,3 +139,20 @@ void shared_buffer_ref::disown()
this->sb_data = nullptr;
this->sb_length = 0;
}
void shared_buffer_ref::copy_ref(const shared_buffer_ref &other)
{
if (other.sb_data == nullptr) {
this->sb_owner = nullptr;
this->sb_data = nullptr;
this->sb_length = 0;
}
else if (other.sb_owner != nullptr) {
this->share(*other.sb_owner, other.sb_data, other.sb_length);
} else {
this->sb_owner = nullptr;
this->sb_data = (char *)malloc(other.sb_length);
memcpy(this->sb_data, other.sb_data, other.sb_length);
this->sb_length = other.sb_length;
}
}

@ -116,21 +116,7 @@ public:
void disown();
private:
void copy_ref(const shared_buffer_ref &other) {
if (other.sb_data == nullptr) {
this->sb_owner = nullptr;
this->sb_data = nullptr;
this->sb_length = 0;
}
else if (other.sb_owner != nullptr) {
this->share(*other.sb_owner, other.sb_data, other.sb_length);
} else {
this->sb_owner = nullptr;
this->sb_data = (char *)malloc(other.sb_length);
memcpy(this->sb_data, other.sb_data, other.sb_length);
this->sb_length = other.sb_length;
}
}
void copy_ref(const shared_buffer_ref &other);
auto_mem<char *> sb_backtrace;
shared_buffer *sb_owner;

Loading…
Cancel
Save