public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] util: Add StackedContext class
@ 2022-07-28 15:35 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-07-28 15:35 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:0bf54ff468270dc60cb06dc1940e4bfb9e918c2b

commit 0bf54ff468270dc60cb06dc1940e4bfb9e918c2b
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Jul 27 14:52:57 2022 +0200

    util: Add StackedContext class

Diff:
---
 gcc/rust/util/rust-stacked-contexts.h | 86 +++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)

diff --git a/gcc/rust/util/rust-stacked-contexts.h b/gcc/rust/util/rust-stacked-contexts.h
new file mode 100644
index 00000000000..c34eb907f06
--- /dev/null
+++ b/gcc/rust/util/rust-stacked-contexts.h
@@ -0,0 +1,86 @@
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_CONTEXT_STACK_H
+#define RUST_CONTEXT_STACK_H
+
+#include "rust-system.h"
+
+namespace Rust {
+
+/**
+ * Context stack util class. This class is useful for situations where you can
+ * enter the same kind of context multiple times. For example, when dealing with
+ * unsafe contexts, you might be tempted to simply keep a boolean value.
+ *
+ * ```rust
+ * let a = 15;
+ * unsafe { // we set the boolean to true
+ *     // Now unsafe operations are allowed!
+ *     let b = *(&a as *const i32);
+ *     let c = std::mem::transmute<i32, f32>(b); // Urgh!
+ * } // we set it to false
+ * ```
+ *
+ * However, since the language allows nested unsafe blocks, you may run into
+ * this situation:
+ *
+ * ```rust
+ * unsafe { // we set the boolean to true
+ *     unsafe { // we set the boolean to true
+ *     } // we set it to false
+ *
+ *     // Now unsafe operations are forbidden again, the boolean is false
+ *     let f = std::mem::transmute<i32, f32>(15); // Error!
+ * } // we set it to false
+ * ```
+ */
+template <typename T> class StackedContexts
+{
+public:
+  /**
+   * Enter a special context
+   */
+  void enter (T value) { stack.emplace_back (value); }
+
+  /**
+   * Exit a special context
+   */
+  T exit ()
+  {
+    rust_assert (!stack.empty ());
+
+    auto last = stack.back ();
+    stack.pop_back ();
+
+    return last;
+  }
+
+  /**
+   * Are we currently inside of a special context?
+   */
+  bool is_in_context () const { return !stack.empty (); }
+
+private:
+  /* Actual data */
+  std::vector<T> stack;
+};
+
+} // namespace Rust
+
+#endif /* !RUST_CONTEXT_STACK_H */


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-07-28 15:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-28 15:35 [gcc/devel/rust/master] util: Add StackedContext class Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).