Move optional.h to webrtc/api/

We use Optional in our public API, so its header should be in
webrtc/api/.

BUG=webrtc:8205

Review-Url: https://codereview.webrtc.org/3011943002
Cr-Commit-Position: refs/heads/master@{#19693}
diff --git a/webrtc/rtc_base/BUILD.gn b/webrtc/rtc_base/BUILD.gn
index 1914b27..6676fdf 100644
--- a/webrtc/rtc_base/BUILD.gn
+++ b/webrtc/rtc_base/BUILD.gn
@@ -137,8 +137,6 @@
     "mod_ops.h",
     "moving_max_counter.h",
     "onetimeevent.h",
-    "optional.cc",
-    "optional.h",
     "pathutils.cc",
     "pathutils.h",
     "platform_file.cc",
@@ -436,6 +434,7 @@
   defines = []
   deps = [
     "..:webrtc_common",
+    "../api:optional",
   ]
   public_deps = [
     ":rtc_base_approved",
@@ -855,7 +854,6 @@
       "mod_ops_unittest.cc",
       "moving_max_counter_unittest.cc",
       "onetimeevent_unittest.cc",
-      "optional_unittest.cc",
       "pathutils_unittest.cc",
       "platform_thread_unittest.cc",
       "random_unittest.cc",
@@ -1047,6 +1045,7 @@
     deps = [
       ":rtc_base_tests_main",
       ":rtc_base_tests_utils",
+      "../api:optional",
       "../test:test_support",
     ]
     public_deps = [
diff --git a/webrtc/rtc_base/moving_max_counter.h b/webrtc/rtc_base/moving_max_counter.h
index 907e9cf..190b7b8 100644
--- a/webrtc/rtc_base/moving_max_counter.h
+++ b/webrtc/rtc_base/moving_max_counter.h
@@ -17,9 +17,9 @@
 #include <limits>
 #include <utility>
 
+#include "webrtc/api/optional.h"
 #include "webrtc/rtc_base/checks.h"
 #include "webrtc/rtc_base/constructormagic.h"
-#include "webrtc/rtc_base/optional.h"
 
 namespace rtc {
 
diff --git a/webrtc/rtc_base/optional.cc b/webrtc/rtc_base/optional.cc
deleted file mode 100644
index 4b41a2d..0000000
--- a/webrtc/rtc_base/optional.cc
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- *  Copyright 2016 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include "webrtc/rtc_base/optional.h"
-
-namespace rtc {
-namespace optional_internal {
-
-#if RTC_HAS_ASAN
-
-void* FunctionThatDoesNothingImpl(void* x) { return x; }
-
-#endif
-
-}  // namespace optional_internal
-}  // namespace rtc
diff --git a/webrtc/rtc_base/optional.h b/webrtc/rtc_base/optional.h
index ca3208e..b707746 100644
--- a/webrtc/rtc_base/optional.h
+++ b/webrtc/rtc_base/optional.h
@@ -8,402 +8,12 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
+// This header is for backwards compatibility only, and will be removed soon.
+// Include webrtc/api/optional.h instead.
+
 #ifndef WEBRTC_RTC_BASE_OPTIONAL_H_
 #define WEBRTC_RTC_BASE_OPTIONAL_H_
 
-#include <algorithm>
-#include <memory>
-#include <utility>
-
-#ifdef UNIT_TEST
-#include <iomanip>
-#include <ostream>
-#endif  // UNIT_TEST
-
-#include "webrtc/api/array_view.h"
-#include "webrtc/rtc_base/checks.h"
-#include "webrtc/rtc_base/sanitizer.h"
-
-namespace rtc {
-
-namespace optional_internal {
-
-#if RTC_HAS_ASAN
-
-// This is a non-inlined function. The optimizer can't see inside it.  It
-// prevents the compiler from generating optimized code that reads value_ even
-// if it is unset. Although safe, this causes memory sanitizers to complain.
-void* FunctionThatDoesNothingImpl(void*);
-
-template <typename T>
-inline T* FunctionThatDoesNothing(T* x) {
-  return reinterpret_cast<T*>(
-      FunctionThatDoesNothingImpl(reinterpret_cast<void*>(x)));
-}
-
-#else
-
-template <typename T>
-inline T* FunctionThatDoesNothing(T* x) { return x; }
-
-#endif
-
-}  // namespace optional_internal
-
-// Simple std::optional-wannabe. It either contains a T or not.
-//
-// A moved-from Optional<T> may only be destroyed, and assigned to if T allows
-// being assigned to after having been moved from. Specifically, you may not
-// assume that it just doesn't contain a value anymore.
-//
-// Examples of good places to use Optional:
-//
-// - As a class or struct member, when the member doesn't always have a value:
-//     struct Prisoner {
-//       std::string name;
-//       Optional<int> cell_number;  // Empty if not currently incarcerated.
-//     };
-//
-// - As a return value for functions that may fail to return a value on all
-//   allowed inputs. For example, a function that searches an array might
-//   return an Optional<size_t> (the index where it found the element, or
-//   nothing if it didn't find it); and a function that parses numbers might
-//   return Optional<double> (the parsed number, or nothing if parsing failed).
-//
-// Examples of bad places to use Optional:
-//
-// - As a return value for functions that may fail because of disallowed
-//   inputs. For example, a string length function should not return
-//   Optional<size_t> so that it can return nothing in case the caller passed
-//   it a null pointer; the function should probably use RTC_[D]CHECK instead,
-//   and return plain size_t.
-//
-// - As a return value for functions that may fail to return a value on all
-//   allowed inputs, but need to tell the caller what went wrong. Returning
-//   Optional<double> when parsing a single number as in the example above
-//   might make sense, but any larger parse job is probably going to need to
-//   tell the caller what the problem was, not just that there was one.
-//
-// - As a non-mutable function argument. When you want to pass a value of a
-//   type T that can fail to be there, const T* is almost always both fastest
-//   and cleanest. (If you're *sure* that the the caller will always already
-//   have an Optional<T>, const Optional<T>& is slightly faster than const T*,
-//   but this is a micro-optimization. In general, stick to const T*.)
-//
-// TODO(kwiberg): Get rid of this class when the standard library has
-// std::optional (and we're allowed to use it).
-template <typename T>
-class Optional final {
- public:
-  // Construct an empty Optional.
-  Optional() : has_value_(false), empty_('\0') {
-    PoisonValue();
-  }
-
-  // Construct an Optional that contains a value.
-  explicit Optional(const T& value) : has_value_(true) {
-    new (&value_) T(value);
-  }
-  explicit Optional(T&& value) : has_value_(true) {
-    new (&value_) T(std::move(value));
-  }
-
-  // Copy constructor: copies the value from m if it has one.
-  Optional(const Optional& m) : has_value_(m.has_value_) {
-    if (has_value_)
-      new (&value_) T(m.value_);
-    else
-      PoisonValue();
-  }
-
-  // Move constructor: if m has a value, moves the value from m, leaving m
-  // still in a state where it has a value, but a moved-from one (the
-  // properties of which depends on T; the only general guarantee is that we
-  // can destroy m).
-  Optional(Optional&& m) : has_value_(m.has_value_) {
-    if (has_value_)
-      new (&value_) T(std::move(m.value_));
-    else
-      PoisonValue();
-  }
-
-  ~Optional() {
-    if (has_value_)
-      value_.~T();
-    else
-      UnpoisonValue();
-  }
-
-  // Copy assignment. Uses T's copy assignment if both sides have a value, T's
-  // copy constructor if only the right-hand side has a value.
-  Optional& operator=(const Optional& m) {
-    if (m.has_value_) {
-      if (has_value_) {
-        value_ = m.value_;  // T's copy assignment.
-      } else {
-        UnpoisonValue();
-        new (&value_) T(m.value_);  // T's copy constructor.
-        has_value_ = true;
-      }
-    } else {
-      reset();
-    }
-    return *this;
-  }
-
-  // Move assignment. Uses T's move assignment if both sides have a value, T's
-  // move constructor if only the right-hand side has a value. The state of m
-  // after it's been moved from is as for the move constructor.
-  Optional& operator=(Optional&& m) {
-    if (m.has_value_) {
-      if (has_value_) {
-        value_ = std::move(m.value_);  // T's move assignment.
-      } else {
-        UnpoisonValue();
-        new (&value_) T(std::move(m.value_));  // T's move constructor.
-        has_value_ = true;
-      }
-    } else {
-      reset();
-    }
-    return *this;
-  }
-
-  // Swap the values if both m1 and m2 have values; move the value if only one
-  // of them has one.
-  friend void swap(Optional& m1, Optional& m2) {
-    if (m1.has_value_) {
-      if (m2.has_value_) {
-        // Both have values: swap.
-        using std::swap;
-        swap(m1.value_, m2.value_);
-      } else {
-        // Only m1 has a value: move it to m2.
-        m2.UnpoisonValue();
-        new (&m2.value_) T(std::move(m1.value_));
-        m1.value_.~T();  // Destroy the moved-from value.
-        m1.has_value_ = false;
-        m2.has_value_ = true;
-        m1.PoisonValue();
-      }
-    } else if (m2.has_value_) {
-      // Only m2 has a value: move it to m1.
-      m1.UnpoisonValue();
-      new (&m1.value_) T(std::move(m2.value_));
-      m2.value_.~T();  // Destroy the moved-from value.
-      m1.has_value_ = true;
-      m2.has_value_ = false;
-      m2.PoisonValue();
-    }
-  }
-
-  // Destroy any contained value. Has no effect if we have no value.
-  void reset() {
-    if (!has_value_)
-      return;
-    value_.~T();
-    has_value_ = false;
-    PoisonValue();
-  }
-
-  template <class... Args>
-  void emplace(Args&&... args) {
-    if (has_value_)
-      value_.~T();
-    else
-      UnpoisonValue();
-    new (&value_) T(std::forward<Args>(args)...);
-    has_value_ = true;
-  }
-
-  // Conversion to bool to test if we have a value.
-  explicit operator bool() const { return has_value_; }
-  bool has_value() const { return has_value_; }
-
-  // Dereferencing. Only allowed if we have a value.
-  const T* operator->() const {
-    RTC_DCHECK(has_value_);
-    return &value_;
-  }
-  T* operator->() {
-    RTC_DCHECK(has_value_);
-    return &value_;
-  }
-  const T& operator*() const {
-    RTC_DCHECK(has_value_);
-    return value_;
-  }
-  T& operator*() {
-    RTC_DCHECK(has_value_);
-    return value_;
-  }
-  const T& value() const {
-    RTC_DCHECK(has_value_);
-    return value_;
-  }
-  T& value() {
-    RTC_DCHECK(has_value_);
-    return value_;
-  }
-
-  // Dereference with a default value in case we don't have a value.
-  const T& value_or(const T& default_val) const {
-    // The no-op call prevents the compiler from generating optimized code that
-    // reads value_ even if !has_value_, but only if FunctionThatDoesNothing is
-    // not completely inlined; see its declaration.).
-    return has_value_ ? *optional_internal::FunctionThatDoesNothing(&value_)
-                      : default_val;
-  }
-
-  // Dereference and move value.
-  T MoveValue() {
-    RTC_DCHECK(has_value_);
-    return std::move(value_);
-  }
-
-  // Equality tests. Two Optionals are equal if they contain equivalent values,
-  // or if they're both empty.
-  friend bool operator==(const Optional& m1, const Optional& m2) {
-    return m1.has_value_ && m2.has_value_ ? m1.value_ == m2.value_
-                                          : m1.has_value_ == m2.has_value_;
-  }
-  friend bool operator==(const Optional& opt, const T& value) {
-    return opt.has_value_ && opt.value_ == value;
-  }
-  friend bool operator==(const T& value, const Optional& opt) {
-    return opt.has_value_ && value == opt.value_;
-  }
-
-  friend bool operator!=(const Optional& m1, const Optional& m2) {
-    return m1.has_value_ && m2.has_value_ ? m1.value_ != m2.value_
-                                          : m1.has_value_ != m2.has_value_;
-  }
-  friend bool operator!=(const Optional& opt, const T& value) {
-    return !opt.has_value_ || opt.value_ != value;
-  }
-  friend bool operator!=(const T& value, const Optional& opt) {
-    return !opt.has_value_ || value != opt.value_;
-  }
-
- private:
-  // Tell sanitizers that value_ shouldn't be touched.
-  void PoisonValue() {
-    rtc::AsanPoison(rtc::MakeArrayView(&value_, 1));
-    rtc::MsanMarkUninitialized(rtc::MakeArrayView(&value_, 1));
-  }
-
-  // Tell sanitizers that value_ is OK to touch again.
-  void UnpoisonValue() {
-    rtc::AsanUnpoison(rtc::MakeArrayView(&value_, 1));
-  }
-
-  bool has_value_;  // True iff value_ contains a live value.
-  union {
-    // empty_ exists only to make it possible to initialize the union, even when
-    // it doesn't contain any data. If the union goes uninitialized, it may
-    // trigger compiler warnings.
-    char empty_;
-    // By placing value_ in a union, we get to manage its construction and
-    // destruction manually: the Optional constructors won't automatically
-    // construct it, and the Optional destructor won't automatically destroy
-    // it. Basically, this just allocates a properly sized and aligned block of
-    // memory in which we can manually put a T with placement new.
-    T value_;
-  };
-};
-
-#ifdef UNIT_TEST
-namespace optional_internal {
-
-// Checks if there's a valid PrintTo(const T&, std::ostream*) call for T.
-template <typename T>
-struct HasPrintTo {
- private:
-  struct No {};
-
-  template <typename T2>
-  static auto Test(const T2& obj)
-      -> decltype(PrintTo(obj, std::declval<std::ostream*>()));
-
-  template <typename>
-  static No Test(...);
-
- public:
-  static constexpr bool value =
-      !std::is_same<decltype(Test<T>(std::declval<const T&>())), No>::value;
-};
-
-// Checks if there's a valid operator<<(std::ostream&, const T&) call for T.
-template <typename T>
-struct HasOstreamOperator {
- private:
-  struct No {};
-
-  template <typename T2>
-  static auto Test(const T2& obj)
-      -> decltype(std::declval<std::ostream&>() << obj);
-
-  template <typename>
-  static No Test(...);
-
- public:
-  static constexpr bool value =
-      !std::is_same<decltype(Test<T>(std::declval<const T&>())), No>::value;
-};
-
-// Prefer using PrintTo to print the object.
-template <typename T>
-typename std::enable_if<HasPrintTo<T>::value, void>::type OptionalPrintToHelper(
-    const T& value,
-    std::ostream* os) {
-  PrintTo(value, os);
-}
-
-// Fall back to operator<<(std::ostream&, ...) if it exists.
-template <typename T>
-typename std::enable_if<HasOstreamOperator<T>::value && !HasPrintTo<T>::value,
-                        void>::type
-OptionalPrintToHelper(const T& value, std::ostream* os) {
-  *os << value;
-}
-
-inline void OptionalPrintObjectBytes(const unsigned char* bytes,
-                                     size_t size,
-                                     std::ostream* os) {
-  *os << "<optional with " << size << "-byte object [";
-  for (size_t i = 0; i != size; ++i) {
-    *os << (i == 0 ? "" : ((i & 1) ? "-" : " "));
-    *os << std::hex << std::setw(2) << std::setfill('0')
-        << static_cast<int>(bytes[i]);
-  }
-  *os << "]>";
-}
-
-// As a final back-up, just print the contents of the objcets byte-wise.
-template <typename T>
-typename std::enable_if<!HasOstreamOperator<T>::value && !HasPrintTo<T>::value,
-                        void>::type
-OptionalPrintToHelper(const T& value, std::ostream* os) {
-  OptionalPrintObjectBytes(reinterpret_cast<const unsigned char*>(&value),
-                           sizeof(value), os);
-}
-
-}  // namespace optional_internal
-
-// PrintTo is used by gtest to print out the results of tests. We want to ensure
-// the object contained in an Optional can be printed out if it's set, while
-// avoiding touching the object's storage if it is undefined.
-template <typename T>
-void PrintTo(const rtc::Optional<T>& opt, std::ostream* os) {
-  if (opt) {
-    optional_internal::OptionalPrintToHelper(*opt, os);
-  } else {
-    *os << "<empty optional>";
-  }
-}
-
-#endif  // UNIT_TEST
-
-}  // namespace rtc
+#include "webrtc/api/optional.h"
 
 #endif  // WEBRTC_RTC_BASE_OPTIONAL_H_
diff --git a/webrtc/rtc_base/optional_unittest.cc b/webrtc/rtc_base/optional_unittest.cc
deleted file mode 100644
index 8f2f7fd..0000000
--- a/webrtc/rtc_base/optional_unittest.cc
+++ /dev/null
@@ -1,831 +0,0 @@
-/*
- *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
- *
- *  Use of this source code is governed by a BSD-style license
- *  that can be found in the LICENSE file in the root of the source
- *  tree. An additional intellectual property rights grant can be found
- *  in the file PATENTS.  All contributing project authors may
- *  be found in the AUTHORS file in the root of the source tree.
- */
-
-#include <memory>
-#include <sstream>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include "webrtc/rtc_base/gunit.h"
-#include "webrtc/rtc_base/optional.h"
-
-namespace rtc {
-
-namespace {
-
-struct MyUnprintableType {
-  int value;
-};
-
-struct MyPrintableType {
-  int value;
-};
-
-struct MyOstreamPrintableType {
-  int value;
-};
-
-void PrintTo(const MyPrintableType& mpt, std::ostream* os) {
-  *os << "The value is " << mpt.value;
-}
-
-std::ostream& operator<<(std::ostream& os,
-                         const MyPrintableType& mpt) {
-  os << mpt.value;
-  return os;
-}
-
-std::ostream& operator<<(std::ostream& os,
-                         const MyOstreamPrintableType& mpt) {
-  os << mpt.value;
-  return os;
-}
-
-// Class whose instances logs various method calls (constructor, destructor,
-// etc.). Each instance has a unique ID (a simple global sequence number) and
-// an origin ID. When a copy is made, the new object gets a fresh ID but copies
-// the origin ID from the original. When a new Logger is created from scratch,
-// it gets a fresh ID, and the origin ID is the same as the ID (default
-// constructor) or given as an argument (explicit constructor).
-class Logger {
- public:
-  Logger() : id_(g_next_id++), origin_(id_) { Log("default constructor"); }
-  explicit Logger(int origin) : id_(g_next_id++), origin_(origin) {
-    Log("explicit constructor");
-  }
-  Logger(int origin, const Logger& pass_by_ref, Logger pass_by_value)
-      : id_(g_next_id++), origin_(origin) {
-    Log("multi parameter constructor");
-  }
-  Logger(const Logger& other) : id_(g_next_id++), origin_(other.origin_) {
-    LogFrom("copy constructor", other);
-  }
-  Logger(Logger&& other) : id_(g_next_id++), origin_(other.origin_) {
-    LogFrom("move constructor", other);
-  }
-  ~Logger() { Log("destructor"); }
-  Logger& operator=(const Logger& other) {
-    origin_ = other.origin_;
-    LogFrom("operator= copy", other);
-    return *this;
-  }
-  Logger& operator=(Logger&& other) {
-    origin_ = other.origin_;
-    LogFrom("operator= move", other);
-    return *this;
-  }
-  friend void swap(Logger& a, Logger& b) {
-    using std::swap;
-    swap(a.origin_, b.origin_);
-    Log2("swap", a, b);
-  }
-  friend bool operator==(const Logger& a, const Logger& b) {
-    Log2("operator==", a, b);
-    return a.origin_ == b.origin_;
-  }
-  friend bool operator!=(const Logger& a, const Logger& b) {
-    Log2("operator!=", a, b);
-    return a.origin_ != b.origin_;
-  }
-  void Foo() { Log("Foo()"); }
-  void Foo() const { Log("Foo() const"); }
-  static std::unique_ptr<std::vector<std::string>> Setup() {
-    std::unique_ptr<std::vector<std::string>> s(new std::vector<std::string>);
-    g_log = s.get();
-    g_next_id = 0;
-    return s;
-  }
-
- private:
-  int id_;
-  int origin_;
-  static std::vector<std::string>* g_log;
-  static int g_next_id;
-  void Log(const char* msg) const {
-    std::ostringstream oss;
-    oss << id_ << ':' << origin_ << ". " << msg;
-    g_log->push_back(oss.str());
-  }
-  void LogFrom(const char* msg, const Logger& other) const {
-    std::ostringstream oss;
-    oss << id_ << ':' << origin_ << ". " << msg << " (from " << other.id_ << ':'
-        << other.origin_ << ")";
-    g_log->push_back(oss.str());
-  }
-  static void Log2(const char* msg, const Logger& a, const Logger& b) {
-    std::ostringstream oss;
-    oss << msg << ' ' << a.id_ << ':' << a.origin_ << ", " << b.id_ << ':'
-        << b.origin_;
-    g_log->push_back(oss.str());
-  }
-};
-
-std::vector<std::string>* Logger::g_log = nullptr;
-int Logger::g_next_id = 0;
-
-// Append all the other args to the vector pointed to by the first arg.
-template <typename T>
-void VectorAppend(std::vector<T>* v) {}
-template <typename T, typename... Ts>
-void VectorAppend(std::vector<T>* v, const T& e, Ts... es) {
-  v->push_back(e);
-  VectorAppend(v, es...);
-}
-
-// Create a vector of strings. Because we're not allowed to use
-// std::initializer_list.
-template <typename... Ts>
-std::vector<std::string> V(Ts... es) {
-  std::vector<std::string> strings;
-  VectorAppend(&strings, static_cast<std::string>(es)...);
-  return strings;
-}
-
-}  // namespace
-
-TEST(OptionalTest, TestConstructDefault) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    EXPECT_FALSE(x);
-    EXPECT_FALSE(x.has_value());
-  }
-  EXPECT_EQ(V(), *log);
-}
-
-TEST(OptionalTest, TestConstructCopyEmpty) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    EXPECT_FALSE(x);
-    EXPECT_FALSE(x.has_value());
-    auto y = x;
-    EXPECT_FALSE(y);
-    EXPECT_FALSE(y.has_value());
-  }
-  EXPECT_EQ(V(), *log);
-}
-
-TEST(OptionalTest, TestConstructCopyFull) {
-  auto log = Logger::Setup();
-  {
-    Logger a;
-    Optional<Logger> x(a);
-    EXPECT_TRUE(x);
-    EXPECT_TRUE(x.has_value());
-    log->push_back("---");
-    auto y = x;
-    EXPECT_TRUE(y);
-    EXPECT_TRUE(y.has_value());
-    log->push_back("---");
-  }
-  EXPECT_EQ(V("0:0. default constructor", "1:0. copy constructor (from 0:0)",
-              "---", "2:0. copy constructor (from 1:0)", "---",
-              "2:0. destructor", "1:0. destructor", "0:0. destructor"),
-            *log);
-}
-
-TEST(OptionalTest, TestConstructMoveEmpty) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    EXPECT_FALSE(x);
-    EXPECT_FALSE(x.has_value());
-    auto y = std::move(x);
-    EXPECT_FALSE(y);
-    EXPECT_FALSE(y.has_value());
-  }
-  EXPECT_EQ(V(), *log);
-}
-
-TEST(OptionalTest, TestConstructMoveFull) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    EXPECT_TRUE(x);
-    EXPECT_TRUE(x.has_value());
-    log->push_back("---");
-    auto y = std::move(x);
-    EXPECT_TRUE(x);
-    EXPECT_TRUE(x.has_value());
-    EXPECT_TRUE(y);
-    EXPECT_TRUE(y.has_value());
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "---", "2:17. move constructor (from 1:17)", "---",
-        "2:17. destructor", "1:17. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestCopyAssignToEmptyFromEmpty) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x, y;
-    x = y;
-  }
-  EXPECT_EQ(V(), *log);
-}
-
-TEST(OptionalTest, TestCopyAssignToFullFromEmpty) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Optional<Logger> y;
-    log->push_back("---");
-    x = y;
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "---", "1:17. destructor", "---"),
-      *log);
-}
-
-TEST(OptionalTest, TestCopyAssignToEmptyFromFull) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    Optional<Logger> y(Logger(17));
-    log->push_back("---");
-    x = y;
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "---", "2:17. copy constructor (from 1:17)", "---",
-        "1:17. destructor", "2:17. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestCopyAssignToFullFromFull) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Optional<Logger> y(Logger(42));
-    log->push_back("---");
-    x = y;
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "2:42. explicit constructor",
-        "3:42. move constructor (from 2:42)", "2:42. destructor", "---",
-        "1:42. operator= copy (from 3:42)", "---", "3:42. destructor",
-        "1:42. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestCopyAssignToEmptyFromT) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    Logger y(17);
-    log->push_back("---");
-    x = Optional<Logger>(y);
-    log->push_back("---");
-  }
-  EXPECT_EQ(V("0:17. explicit constructor", "---",
-              "1:17. copy constructor (from 0:17)",
-              "2:17. move constructor (from 1:17)", "1:17. destructor", "---",
-              "0:17. destructor", "2:17. destructor"),
-            *log);
-}
-
-TEST(OptionalTest, TestCopyAssignToFullFromT) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Logger y(42);
-    log->push_back("---");
-    x = Optional<Logger>(y);
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "2:42. explicit constructor", "---",
-        "3:42. copy constructor (from 2:42)",
-        "1:42. operator= move (from 3:42)", "3:42. destructor", "---",
-        "2:42. destructor", "1:42. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestMoveAssignToEmptyFromEmpty) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x, y;
-    x = std::move(y);
-  }
-  EXPECT_EQ(V(), *log);
-}
-
-TEST(OptionalTest, TestMoveAssignToFullFromEmpty) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Optional<Logger> y;
-    log->push_back("---");
-    x = std::move(y);
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "---", "1:17. destructor", "---"),
-      *log);
-}
-
-TEST(OptionalTest, TestMoveAssignToEmptyFromFull) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    Optional<Logger> y(Logger(17));
-    log->push_back("---");
-    x = std::move(y);
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "---", "2:17. move constructor (from 1:17)", "---",
-        "1:17. destructor", "2:17. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestMoveAssignToFullFromFull) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Optional<Logger> y(Logger(42));
-    log->push_back("---");
-    x = std::move(y);
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "2:42. explicit constructor",
-        "3:42. move constructor (from 2:42)", "2:42. destructor", "---",
-        "1:42. operator= move (from 3:42)", "---", "3:42. destructor",
-        "1:42. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestMoveAssignToEmptyFromT) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    Logger y(17);
-    log->push_back("---");
-    x = Optional<Logger>(std::move(y));
-    log->push_back("---");
-  }
-  EXPECT_EQ(V("0:17. explicit constructor", "---",
-              "1:17. move constructor (from 0:17)",
-              "2:17. move constructor (from 1:17)", "1:17. destructor", "---",
-              "0:17. destructor", "2:17. destructor"),
-            *log);
-}
-
-TEST(OptionalTest, TestMoveAssignToFullFromT) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Logger y(42);
-    log->push_back("---");
-    x = Optional<Logger>(std::move(y));
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "2:42. explicit constructor", "---",
-        "3:42. move constructor (from 2:42)",
-        "1:42. operator= move (from 3:42)", "3:42. destructor", "---",
-        "2:42. destructor", "1:42. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestResetEmpty) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    x.reset();
-  }
-  EXPECT_EQ(V(), *log);
-}
-
-TEST(OptionalTest, TestResetFull) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    log->push_back("---");
-    x.reset();
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:17. move constructor (from 0:17)",
-        "0:17. destructor", "---", "1:17. destructor", "---"),
-      *log);
-}
-
-TEST(OptionalTest, TestEmplaceEmptyWithExplicit) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    log->push_back("---");
-    x.emplace(42);
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("---",
-              "0:42. explicit constructor",
-              "---",
-              "0:42. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestEmplaceEmptyWithMultipleParameters) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    Logger ref(21);
-    Logger value(35);
-    log->push_back("---");
-    x.emplace(42, ref, std::move(value));
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:21. explicit constructor",
-              "1:35. explicit constructor",
-              "---",
-              "2:35. move constructor (from 1:35)",
-              "3:42. multi parameter constructor",
-              "2:35. destructor",
-              "---",
-              "1:35. destructor",
-              "0:21. destructor",
-              "3:42. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestEmplaceEmptyWithCopy) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    Logger y(42);
-    log->push_back("---");
-    x.emplace(y);
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:42. explicit constructor",
-              "---",
-              "1:42. copy constructor (from 0:42)",
-              "---",
-              "0:42. destructor",
-              "1:42. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestEmplaceEmptyWithMove) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x;
-    Logger y(42);
-    log->push_back("---");
-    x.emplace(std::move(y));
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:42. explicit constructor",
-              "---",
-              "1:42. move constructor (from 0:42)",
-              "---",
-              "0:42. destructor",
-              "1:42. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestEmplaceFullWithExplicit) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    log->push_back("---");
-    x.emplace(42);
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(
-      V("0:17. explicit constructor",
-        "1:17. move constructor (from 0:17)",
-        "0:17. destructor",
-        "---",
-        "1:17. destructor",
-        "2:42. explicit constructor",
-        "---",
-        "2:42. destructor"),
-      *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestEmplaceFullWithMultipleParameters) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Logger ref(21);
-    Logger value(35);
-    log->push_back("---");
-    x.emplace(42, ref, std::move(value));
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:17. explicit constructor",
-              "1:17. move constructor (from 0:17)",
-              "0:17. destructor",
-              "2:21. explicit constructor",
-              "3:35. explicit constructor",
-              "---",
-              "1:17. destructor",
-              "4:35. move constructor (from 3:35)",
-              "5:42. multi parameter constructor",
-              "4:35. destructor",
-              "---",
-              "3:35. destructor",
-              "2:21. destructor",
-              "5:42. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestEmplaceFullWithCopy) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Logger y(42);
-    log->push_back("---");
-    x.emplace(y);
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:17. explicit constructor",
-              "1:17. move constructor (from 0:17)",
-              "0:17. destructor",
-              "2:42. explicit constructor",
-              "---",
-              "1:17. destructor",
-              "3:42. copy constructor (from 2:42)",
-              "---",
-              "2:42. destructor",
-              "3:42. destructor"),
-           *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestEmplaceFullWithMove) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(17));
-    Logger y(42);
-    log->push_back("---");
-    x.emplace(std::move(y));
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:17. explicit constructor",
-              "1:17. move constructor (from 0:17)",
-              "0:17. destructor",
-              "2:42. explicit constructor",
-              "---",
-              "1:17. destructor",
-              "3:42. move constructor (from 2:42)",
-              "---",
-              "2:42. destructor",
-              "3:42. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestDereference) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(42));
-    const auto& y = x;
-    log->push_back("---");
-    x->Foo();
-    y->Foo();
-    std::move(x)->Foo();
-    std::move(y)->Foo();
-    log->push_back("---");
-    (*x).Foo();
-    (*y).Foo();
-    (*std::move(x)).Foo();
-    (*std::move(y)).Foo();
-    log->push_back("---");
-    x.value().Foo();
-    y.value().Foo();
-    std::move(x).value().Foo();
-    std::move(y).value().Foo();
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:42. explicit constructor",
-              "1:42. move constructor (from 0:42)",
-              "0:42. destructor",
-              "---",
-              "1:42. Foo()",
-              "1:42. Foo() const",
-              "1:42. Foo()",
-              "1:42. Foo() const",
-              "---",
-              "1:42. Foo()",
-              "1:42. Foo() const",
-              "1:42. Foo()",
-              "1:42. Foo() const",
-              "---",
-              "1:42. Foo()",
-              "1:42. Foo() const",
-              "1:42. Foo()",
-              "1:42. Foo() const",
-              "---",
-              "1:42. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestDereferenceWithDefault) {
-  auto log = Logger::Setup();
-  {
-    const Logger a(17), b(42);
-    Optional<Logger> x(a);
-    Optional<Logger> y;
-    log->push_back("-1-");
-    EXPECT_EQ(a, x.value_or(Logger(42)));
-    log->push_back("-2-");
-    EXPECT_EQ(b, y.value_or(Logger(42)));
-    log->push_back("-3-");
-    EXPECT_EQ(a, Optional<Logger>(Logger(17)).value_or(b));
-    log->push_back("-4-");
-    EXPECT_EQ(b, Optional<Logger>().value_or(b));
-    log->push_back("-5-");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:42. explicit constructor",
-        "2:17. copy constructor (from 0:17)", "-1-",
-        "3:42. explicit constructor", "operator== 0:17, 2:17",
-        "3:42. destructor", "-2-", "4:42. explicit constructor",
-        "operator== 1:42, 4:42", "4:42. destructor", "-3-",
-        "5:17. explicit constructor", "6:17. move constructor (from 5:17)",
-        "operator== 0:17, 6:17", "6:17. destructor", "5:17. destructor", "-4-",
-        "operator== 1:42, 1:42", "-5-", "2:17. destructor", "1:42. destructor",
-        "0:17. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestEquality) {
-  auto log = Logger::Setup();
-  {
-    Logger a(17), b(42);
-    Optional<Logger> ma1(a), ma2(a), mb(b), me1, me2;
-    log->push_back("---");
-    EXPECT_EQ(ma1, ma1);
-    EXPECT_EQ(ma1, ma2);
-    EXPECT_NE(ma1, mb);
-    EXPECT_NE(ma1, me1);
-    EXPECT_EQ(me1, me1);
-    EXPECT_EQ(me1, me2);
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:17. explicit constructor", "1:42. explicit constructor",
-        "2:17. copy constructor (from 0:17)",
-        "3:17. copy constructor (from 0:17)",
-        "4:42. copy constructor (from 1:42)", "---", "operator== 2:17, 2:17",
-        "operator== 2:17, 3:17", "operator!= 2:17, 4:42", "---",
-        "4:42. destructor", "3:17. destructor", "2:17. destructor",
-        "1:42. destructor", "0:17. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestEqualityWithObject) {
-  auto log = Logger::Setup();
-  {
-    Logger a(17), b(42);
-    Optional<Logger> ma(a), me;
-    // Using operator== and operator!= explicetly instead of EXPECT_EQ/EXPECT_NE
-    // macros because those operators are under test.
-    log->push_back("---");
-
-    EXPECT_TRUE(ma == a);
-    EXPECT_TRUE(a == ma);
-    EXPECT_FALSE(ma == b);
-    EXPECT_FALSE(b == ma);
-    EXPECT_FALSE(me == a);
-    EXPECT_FALSE(a == me);
-
-    EXPECT_FALSE(ma != a);
-    EXPECT_FALSE(a != ma);
-    EXPECT_TRUE(ma != b);
-    EXPECT_TRUE(b != ma);
-    EXPECT_TRUE(me != a);
-    EXPECT_TRUE(a != me);
-
-    log->push_back("---");
-  }
-  // clang-format off
-  EXPECT_EQ(V("0:17. explicit constructor",
-              "1:42. explicit constructor",
-              "2:17. copy constructor (from 0:17)",
-              "---",
-              "operator== 2:17, 0:17",
-              "operator== 0:17, 2:17",
-              "operator== 2:17, 1:42",
-              "operator== 1:42, 2:17",
-              // No operator should be called when comparing to empty.
-              "operator!= 2:17, 0:17",
-              "operator!= 0:17, 2:17",
-              "operator!= 2:17, 1:42",
-              "operator!= 1:42, 2:17",
-              // No operator should be called when comparing to empty.
-              "---",
-              "2:17. destructor",
-              "1:42. destructor",
-              "0:17. destructor"),
-            *log);
-  // clang-format on
-}
-
-TEST(OptionalTest, TestSwap) {
-  auto log = Logger::Setup();
-  {
-    Logger a(17), b(42);
-    Optional<Logger> x1(a), x2(b), y1(a), y2, z1, z2;
-    log->push_back("---");
-    swap(x1, x2);  // Swap full <-> full.
-    swap(y1, y2);  // Swap full <-> empty.
-    swap(z1, z2);  // Swap empty <-> empty.
-    log->push_back("---");
-  }
-  EXPECT_EQ(V("0:17. explicit constructor", "1:42. explicit constructor",
-              "2:17. copy constructor (from 0:17)",
-              "3:42. copy constructor (from 1:42)",
-              "4:17. copy constructor (from 0:17)", "---", "swap 2:42, 3:17",
-              "5:17. move constructor (from 4:17)", "4:17. destructor", "---",
-              "5:17. destructor", "3:17. destructor", "2:42. destructor",
-              "1:42. destructor", "0:17. destructor"),
-            *log);
-}
-
-TEST(OptionalTest, TestMoveValue) {
-  auto log = Logger::Setup();
-  {
-    Optional<Logger> x(Logger(42));
-    log->push_back("---");
-    Logger moved = x.MoveValue();
-    log->push_back("---");
-  }
-  EXPECT_EQ(
-      V("0:42. explicit constructor", "1:42. move constructor (from 0:42)",
-        "0:42. destructor", "---", "2:42. move constructor (from 1:42)", "---",
-        "2:42. destructor", "1:42. destructor"),
-      *log);
-}
-
-TEST(OptionalTest, TestPrintTo) {
-  constexpr char kEmptyOptionalMessage[] = "<empty optional>";
-  const Optional<MyUnprintableType> empty_unprintable;
-  const Optional<MyPrintableType> empty_printable;
-  const Optional<MyOstreamPrintableType> empty_ostream_printable;
-  EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_unprintable));
-  EXPECT_EQ(kEmptyOptionalMessage, ::testing::PrintToString(empty_printable));
-  EXPECT_EQ(kEmptyOptionalMessage,
-            ::testing::PrintToString(empty_ostream_printable));
-  EXPECT_NE("1", ::testing::PrintToString(Optional<MyUnprintableType>({1})));
-  EXPECT_NE("1", ::testing::PrintToString(Optional<MyPrintableType>({1})));
-  EXPECT_EQ("The value is 1",
-            ::testing::PrintToString(Optional<MyPrintableType>({1})));
-  EXPECT_EQ("1",
-            ::testing::PrintToString(Optional<MyOstreamPrintableType>({1})));
-}
-
-void UnusedFunctionWorkaround() {
-  // These are here to ensure we don't get warnings about ostream and PrintTo
-  // for MyPrintableType never getting called.
-  const MyPrintableType dont_warn{17};
-  const MyOstreamPrintableType dont_warn2{18};
-  std::stringstream sstr;
-  sstr << dont_warn;
-  PrintTo(dont_warn, &sstr);
-  sstr << dont_warn2;
-}
-
-}  // namespace rtc
diff --git a/webrtc/rtc_base/rate_statistics.h b/webrtc/rtc_base/rate_statistics.h
index 7c4daea..87f6c35 100644
--- a/webrtc/rtc_base/rate_statistics.h
+++ b/webrtc/rtc_base/rate_statistics.h
@@ -13,7 +13,7 @@
 
 #include <memory>
 
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
 #include "webrtc/typedefs.h"
 
 namespace webrtc {
diff --git a/webrtc/rtc_base/rtccertificategenerator.h b/webrtc/rtc_base/rtccertificategenerator.h
index 272a5d6..4e0437c 100644
--- a/webrtc/rtc_base/rtccertificategenerator.h
+++ b/webrtc/rtc_base/rtccertificategenerator.h
@@ -11,7 +11,7 @@
 #ifndef WEBRTC_RTC_BASE_RTCCERTIFICATEGENERATOR_H_
 #define WEBRTC_RTC_BASE_RTCCERTIFICATEGENERATOR_H_
 
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
 #include "webrtc/rtc_base/refcount.h"
 #include "webrtc/rtc_base/rtccertificate.h"
 #include "webrtc/rtc_base/scoped_ref_ptr.h"
diff --git a/webrtc/rtc_base/rtccertificategenerator_unittest.cc b/webrtc/rtc_base/rtccertificategenerator_unittest.cc
index df820d9..829a6ee 100644
--- a/webrtc/rtc_base/rtccertificategenerator_unittest.cc
+++ b/webrtc/rtc_base/rtccertificategenerator_unittest.cc
@@ -12,10 +12,10 @@
 
 #include <memory>
 
+#include "webrtc/api/optional.h"
 #include "webrtc/rtc_base/checks.h"
 #include "webrtc/rtc_base/gunit.h"
 #include "webrtc/rtc_base/logging.h"
-#include "webrtc/rtc_base/optional.h"
 #include "webrtc/rtc_base/thread.h"
 
 namespace rtc {
diff --git a/webrtc/rtc_base/string_to_number.h b/webrtc/rtc_base/string_to_number.h
index c61d1ed..93bd850 100644
--- a/webrtc/rtc_base/string_to_number.h
+++ b/webrtc/rtc_base/string_to_number.h
@@ -14,7 +14,7 @@
 #include <string>
 #include <limits>
 
-#include "webrtc/rtc_base/optional.h"
+#include "webrtc/api/optional.h"
 
 namespace rtc {