From c5298fe7379c0a4ec520b1589c536dc584d77fe0 Mon Sep 17 00:00:00 2001 From: Junwang Zhao Date: Sun, 26 Jan 2025 22:35:47 +0800 Subject: [PATCH 1/9] backport c++23 std::expected After studied [0], [1] and [2], I'm inclined to backport [0] to iceberg-cpp, the main reason is that [0] has exactly the same APIs as std::expected, [1] provide some extra member functions like `map` and `map_error`, [2] has different API names like `then` and `thenOrThrow`. We want users to use iceberg::expected the same way as std::expected, and we will replace iceberg::expected with std::expected when we decide to move to c++23 some day, by backporting [0], we can facilitate a smoother transition process. We discussed about `Exceptions vs Expected` in #14, while backporting [0], I had the feeling we shouldn't choose one over the other, we can use both approaches effectively. expected provide monadic operations like `and_then`, `transform`, `or_else`, `transform_error`, let us do method chaining, which is a good sign. [0] https://github.com/zeus-cpp/expected [1] https://github.com/TartanLlama/expected [2] https://github.com/facebook/folly/blob/main/folly/Expected.h [3] https://en.cppreference.com/w/cpp/utility/expected Signed-off-by: Junwang Zhao --- .github/workflows/cpp-linter.yml | 1 + src/iceberg/expected.h | 2332 ++++++++++++++++++++++++++++++ test/core/CMakeLists.txt | 6 + test/core/expected_test.cc | 627 ++++++++ 4 files changed, 2966 insertions(+) create mode 100644 src/iceberg/expected.h create mode 100644 test/core/expected_test.cc diff --git a/.github/workflows/cpp-linter.yml b/.github/workflows/cpp-linter.yml index cc1055537..da58dca94 100644 --- a/.github/workflows/cpp-linter.yml +++ b/.github/workflows/cpp-linter.yml @@ -44,6 +44,7 @@ jobs: with: style: file tidy-checks: '' + version: 19 files-changed-only: true lines-changed-only: true thread-comments: true diff --git a/src/iceberg/expected.h b/src/iceberg/expected.h new file mode 100644 index 000000000..f68326e01 --- /dev/null +++ b/src/iceberg/expected.h @@ -0,0 +1,2332 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include +#include +#include +#include + +#include "iceberg/iceberg_export.h" + +namespace iceberg { + +namespace expected_detail { + +template class Template> +inline constexpr bool is_specialization_v = + false; // true if and only if T is a specialization of Template +template