diff --git a/saphyr/node.cc b/saphyr/node.cc index 8cd6aba..4c982f8 100644 --- a/saphyr/node.cc +++ b/saphyr/node.cc @@ -125,20 +125,20 @@ size_t Node::size() const { return node_.len(); } bool Node::IsEmpty() const { return node_.is_empty(); } Node::operator bool() const { return IsDefined(); } -NodeView Node::operator[](rs_std::StrRef key) const { +NodeView Node::operator[](rs_std::StrRef key) const& { return NodeView(node_.get_at_key(key).value_or(rust::NodeView())); } -NodeView Node::operator[](size_t index) const { +NodeView Node::operator[](size_t index) const& { return NodeView(node_.get_at_index(index).value_or(rust::NodeView())); } -std::optional Node::Get(size_t index) const { +std::optional Node::Get(size_t index) const& { if (auto val = node_.get_at_index(index)) { return std::make_optional(NodeView(*val)); } return std::nullopt; } -std::optional Node::Get(rs_std::StrRef key) const { +std::optional Node::Get(rs_std::StrRef key) const& { if (auto val = node_.get_at_key(key)) { return std::make_optional(NodeView(*val)); } @@ -150,7 +150,7 @@ void Node::SetAtIndex(size_t index, rust::YamlOwned value) { } template <> -std::optional Node::as_optional() const { +std::optional Node::as_optional() const& { auto v = node_.as_i64(); if (!v) return std::nullopt; if (*v < std::numeric_limits::min() || @@ -161,28 +161,28 @@ std::optional Node::as_optional() const { } template <> -std::optional Node::as_optional() const { +std::optional Node::as_optional() const& { auto v = node_.as_i64(); if (!v || *v < 0) return std::nullopt; return static_cast(*v); } template <> -std::optional Node::as_optional() const { +std::optional Node::as_optional() const& { return node_.as_bool(); } template <> -std::optional Node::as_optional() const { +std::optional Node::as_optional() const& { return node_.as_str(); } template <> -std::optional Node::as_optional() const { +std::optional Node::as_optional() const& { return node_.as_f64(); } -NodeView Node::as_view() const { return NodeView(node_.as_view()); } +NodeView Node::as_view() const& { return NodeView(node_.as_view()); } absl::StatusOr Dump(const Node& node) { return node.as_view().Dump(); diff --git a/saphyr/node.h b/saphyr/node.h index 1e3e85d..f6f4a8f 100644 --- a/saphyr/node.h +++ b/saphyr/node.h @@ -211,27 +211,36 @@ class Node { // Accesses an element in a map by key. // Returns a `NodeView` for which `IsDefined()` is false if the node is not a // map or the key is not found. - NodeView operator[](rs_std::StrRef key) const ABSL_ATTRIBUTE_LIFETIME_BOUND; - NodeView operator[](const char* key) const ABSL_ATTRIBUTE_LIFETIME_BOUND { + NodeView operator[](rs_std::StrRef key) const& ABSL_ATTRIBUTE_LIFETIME_BOUND; + NodeView operator[](const char* key) const& ABSL_ATTRIBUTE_LIFETIME_BOUND { return operator[](rs_std::StrRef::FromUtf8Unchecked(key)); } - NodeView operator[](absl::string_view key) const - ABSL_ATTRIBUTE_LIFETIME_BOUND { + NodeView operator[]( + absl::string_view key) const& ABSL_ATTRIBUTE_LIFETIME_BOUND { return operator[](rs_std::StrRef::FromUtf8Unchecked(key)); } + NodeView operator[](rs_std::StrRef key) const&& = delete; + NodeView operator[](const char* key) const&& = delete; + NodeView operator[](absl::string_view key) const&& = delete; + // Accesses an element in a sequence by index. // Returns a `NodeView` for which `IsDefined()` is false if the node is not a // sequence or the index is out of bounds. - NodeView operator[](size_t index) const ABSL_ATTRIBUTE_LIFETIME_BOUND; - NodeView operator[](int index) const ABSL_ATTRIBUTE_LIFETIME_BOUND { + NodeView operator[](size_t index) const& ABSL_ATTRIBUTE_LIFETIME_BOUND; + NodeView operator[](int index) const& ABSL_ATTRIBUTE_LIFETIME_BOUND { return index < 0 ? NodeView() : operator[](static_cast(index)); } + NodeView operator[](size_t index) const&& = delete; + NodeView operator[](int index) const&& = delete; // A safer version of `operator[]` for sequence. - std::optional Get(size_t index) const ABSL_ATTRIBUTE_LIFETIME_BOUND; + std::optional Get( + size_t index) const& ABSL_ATTRIBUTE_LIFETIME_BOUND; // A safer version of `operator[]` for map. - std::optional Get(rs_std::StrRef key) const - ABSL_ATTRIBUTE_LIFETIME_BOUND; + std::optional Get( + rs_std::StrRef key) const& ABSL_ATTRIBUTE_LIFETIME_BOUND; + std::optional Get(size_t index) const&& = delete; + std::optional Get(rs_std::StrRef key) const&& = delete; // Sets the value at a given index in a sequence. // If `index` is within bounds, the existing value is replaced. @@ -242,10 +251,13 @@ class Node { // Tries to convert the node to the specified type `T`. // Returns `std::nullopt` if the conversion fails or the node is undefined. template - std::optional as_optional() const; + std::optional as_optional() const&; + template + std::optional as_optional() const&& = delete; // Returns a view into the node. - NodeView as_view() const ABSL_ATTRIBUTE_LIFETIME_BOUND; + NodeView as_view() const& ABSL_ATTRIBUTE_LIFETIME_BOUND; + NodeView as_view() const&& = delete; private: rust::NodeOwned node_;