Skip to content

Commit

Permalink
Add comments clarifying that getSnapshot never blocks/throws/returns …
Browse files Browse the repository at this point in the history
…nullptr

Summary: Some of this can be made even cleaner by using folly::not_null_* but that's a larger refactoring.

Reviewed By: ngocdmfb

Differential Revision: D68240076

fbshipit-source-id: 63a8d72b5c48cb66b478795ad1f9991423f50472
  • Loading branch information
andriigrynenko authored and facebook-github-bot committed Jan 16, 2025
1 parent e6393a4 commit b893ec3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion third-party/folly/src/folly/observer/Observer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ observer::Observer<ResultOfNoObserverUnwrap<F>> makeValueObserver(F&& creator) {
namespace observer {

template <typename T>
Snapshot<T> Observer<T>::getSnapshot() const {
Snapshot<T> Observer<T>::getSnapshot() const noexcept {
auto data = core_->getData();
return Snapshot<T>(
*core_,
Expand Down
23 changes: 21 additions & 2 deletions third-party/folly/src/folly/observer/Observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ namespace observer {
* Snapshot will hold a view of the object, even if object in the Observer
* gets updated.
*
* Note: fetching a snapshot from Observer will never block/fail. And returned
* snapshow will never contain a nullptr.
*
*
* What makes Observer powerful is its ability to track updates to other
* Observers. Imagine we have two separate Observers A and B which hold
Expand Down Expand Up @@ -168,12 +171,24 @@ class Snapshot {
public:
const T& operator*() const { return *get(); }

/**
* Never returns nullptr
*/
const T* operator->() const { return get(); }

/**
* Never returns nullptr
*/
const T* get() const { return data_.get(); }

/**
* Never returns nullptr
*/
std::shared_ptr<const T> getShared() const& { return data_; }

/**
* Never returns nullptr
*/
std::shared_ptr<const T> getShared() && { return std::move(data_); }

/**
Expand Down Expand Up @@ -225,8 +240,12 @@ class Observer {
public:
explicit Observer(observer_detail::Core::Ptr core);

Snapshot<T> getSnapshot() const;
Snapshot<T> operator*() const { return getSnapshot(); }
/**
* Never throws or blocks
* Never returns an empty snapshot
*/
Snapshot<T> getSnapshot() const noexcept;
Snapshot<T> operator*() const noexcept { return getSnapshot(); }

/**
* Check if we have a newer version of the observed object than the snapshot.
Expand Down

0 comments on commit b893ec3

Please sign in to comment.