├── .clang-format ├── .clangd ├── .doxygen ├── .gclient ├── .gitattributes ├── .github ├── lock.yml └── workflows │ ├── ci.yml │ ├── clang-doc.yml │ ├── hdoc.yml │ ├── subdoc.yml │ └── try.yml ├── .gitignore ├── .gitmodules ├── .hdoc.toml ├── .vscode └── launch.json ├── BUILD.md ├── CMakeLists.txt ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── PRINCIPLES.md ├── README.md ├── STYLE.md ├── USAGE.md ├── bench ├── CMakeLists.txt ├── bench_simd_chunks.cc └── bench_vec_map.cc ├── subdoc ├── CMakeLists.txt ├── README.md ├── gen_tests │ ├── function-overloads │ │ ├── index.html │ │ ├── n-fn.multiple.0.html │ │ ├── n-fn.multiple.1.html │ │ ├── namespace.n.html │ │ └── test.cc │ ├── gen_tests.cc │ ├── markdown │ │ ├── N.html │ │ ├── S.html │ │ ├── Syntax.html │ │ ├── index.html │ │ └── test.cc │ ├── nested-namespace │ │ ├── index.html │ │ ├── namespace.outer_namespace.html │ │ ├── outer_namespace-fn.outer.html │ │ ├── outer_namespace-inner_namespace-fn.inner.html │ │ ├── outer_namespace-namespace.empty.html │ │ ├── outer_namespace-namespace.inner_namespace.html │ │ └── test.cc │ ├── struct-basic │ │ ├── S.html │ │ ├── index.html │ │ └── test.cc │ ├── struct-complex │ │ ├── Base.html │ │ ├── OtherType.html │ │ ├── S.html │ │ ├── index.html │ │ ├── macro.sus_macro_for_test.html │ │ ├── macro.sus_macro_for_test_fn.html │ │ └── test.cc │ ├── subdoc-test-style.css │ ├── templates │ │ ├── Concept.html │ │ ├── S.html │ │ ├── TemplateMethods.html │ │ ├── TemplateStruct.html │ │ ├── fn.requires_overload.html │ │ ├── fn.return_template.html │ │ ├── fn.template_function.html │ │ ├── index.html │ │ └── test.cc │ └── typenames-across-paths │ │ ├── index.html │ │ ├── n-FunctionParams.html │ │ ├── n-HoldS.html │ │ ├── n-fn.pass_s.html │ │ ├── n-fn.return_nested.html │ │ ├── n-fn.return_s.html │ │ ├── namespace.n.html │ │ ├── namespace.other.html │ │ ├── other-namespace.subother.html │ │ ├── other-subother-C.html │ │ ├── other-subother-S-FirstNested-Nested.html │ │ ├── other-subother-S-FirstNested.html │ │ ├── other-subother-S.html │ │ ├── other-subother-fn.subother_func.html │ │ ├── other-subother-fn.subother_func_with_overload.hasanoverload.html │ │ └── test.cc ├── lib │ ├── clang_resource_dir.cc │ ├── clang_resource_dir.h │ ├── database.h │ ├── doc_attributes.h │ ├── friendly_names.h │ ├── gen │ │ ├── files.h │ │ ├── generate.cc │ │ ├── generate.h │ │ ├── generate_alias.cc │ │ ├── generate_alias.h │ │ ├── generate_concept.cc │ │ ├── generate_concept.h │ │ ├── generate_cpp_path.cc │ │ ├── generate_cpp_path.h │ │ ├── generate_function.cc │ │ ├── generate_function.h │ │ ├── generate_head.cc │ │ ├── generate_head.h │ │ ├── generate_macro.cc │ │ ├── generate_macro.h │ │ ├── generate_namespace.cc │ │ ├── generate_namespace.h │ │ ├── generate_nav.cc │ │ ├── generate_nav.h │ │ ├── generate_record.cc │ │ ├── generate_record.h │ │ ├── generate_requires.cc │ │ ├── generate_requires.h │ │ ├── generate_search.cc │ │ ├── generate_search.h │ │ ├── generate_source_link.cc │ │ ├── generate_source_link.h │ │ ├── generate_type.cc │ │ ├── generate_type.h │ │ ├── html_writer.h │ │ ├── json_writer.h │ │ ├── markdown_to_html.cc │ │ ├── markdown_to_html.h │ │ ├── options.h │ │ ├── search.cc │ │ └── search.h │ ├── linked_type.cc │ ├── linked_type.h │ ├── method_qualifier.h │ ├── parse_comment.cc │ ├── parse_comment.h │ ├── path.cc │ ├── path.h │ ├── record_type.h │ ├── requires.cc │ ├── requires.h │ ├── run.cc │ ├── run.h │ ├── run_options.h │ ├── stmt_to_string.cc │ ├── stmt_to_string.h │ ├── type.cc │ ├── type.h │ ├── unique_symbol.h │ ├── visit.cc │ └── visit.h ├── llvm.h ├── subdoc_main.cc └── tests │ ├── access_unittest.cc │ ├── aliases_unittest.cc │ ├── concepts_unittest.cc │ ├── cpp_version.h │ ├── doc_attributes_unittest.cc │ ├── fields_unittest.cc │ ├── functions_unittest.cc │ ├── include_exclude_unittest.cc │ ├── macros_unittest.cc │ ├── methods_unittest.cc │ ├── namespaces_unittest.cc │ ├── records_unittest.cc │ ├── self_name_replace_unittest.cc │ ├── source_link_unittest.cc │ ├── styles_unittest.cc │ ├── subdoc_gen_test.h │ ├── subdoc_test.h │ ├── test_main.cc │ ├── test_main.h │ └── type_unittest.cc ├── sus ├── CMakeLists.txt ├── assertions │ ├── check.h │ ├── check_unittest.cc │ ├── debug_check.h │ ├── panic.cc │ ├── panic.h │ ├── panic_unittest.cc │ ├── unreachable.h │ └── unreachable_unittest.cc ├── boxed │ ├── __private │ │ └── string_error.h │ ├── box.h │ ├── box_unittest.cc │ ├── boxed.h │ ├── dyn.h │ ├── dyn_unittest.cc │ └── macros.h ├── choice │ ├── __private │ │ ├── all_values_are_unique.h │ │ ├── index_of_value.h │ │ ├── index_type.h │ │ ├── nothing.h │ │ ├── ops_concepts.h │ │ ├── pack_index.h │ │ ├── storage.h │ │ └── type_list.h │ ├── choice.h │ ├── choice_types.h │ ├── choice_types_unittest.cc │ ├── choice_unittest.cc │ └── macros.h ├── cmp │ ├── __private │ │ └── void_concepts.h │ ├── cmp.h │ ├── eq.h │ ├── eq_unittest.cc │ ├── ord.h │ ├── ord_unittest.cc │ ├── reverse.h │ └── reverse_unittest.cc ├── collections │ ├── __private │ │ ├── slice_methods.inc │ │ ├── slice_methods_impl.inc │ │ ├── slice_mut_methods.inc │ │ └── sort.h │ ├── array.h │ ├── array_unittest.cc │ ├── collections.h │ ├── compat_deque.h │ ├── compat_deque_unittest.cc │ ├── compat_forward_list.h │ ├── compat_forward_list_unittest.cc │ ├── compat_list.h │ ├── compat_list_unittest.cc │ ├── compat_map.h │ ├── compat_map_unittest.cc │ ├── compat_pair_concept.h │ ├── compat_queue.h │ ├── compat_queue_unittest.cc │ ├── compat_set.h │ ├── compat_set_unittest.cc │ ├── compat_stack.h │ ├── compat_stack_unittest.cc │ ├── compat_unordered_map.h │ ├── compat_unordered_map_unittest.cc │ ├── compat_unordered_set.h │ ├── compat_unordered_set_unittest.cc │ ├── compat_vector.h │ ├── compat_vector_unittest.cc │ ├── concat.h │ ├── invalidation_off_size_unittest.cc │ ├── invalidation_on_size_unittest.cc │ ├── iterators │ │ ├── array_iter.h │ │ ├── chunks.h │ │ ├── drain.h │ │ ├── slice_iter.h │ │ ├── split.h │ │ ├── vec_iter.h │ │ └── windows.h │ ├── join.h │ ├── slice.h │ ├── slice_unittest.cc │ ├── vec.h │ └── vec_unittest.cc ├── construct │ ├── __private │ │ └── into_ref.h │ ├── cast.h │ ├── cast_unittest.cc │ ├── construct.h │ ├── default.h │ ├── default_unittest.cc │ ├── from.h │ ├── from_unittest.cc │ ├── into.h │ ├── into_unittest.cc │ └── safe_from_reference.h ├── env │ ├── env.h │ ├── var.cc │ ├── var.h │ └── var_unittest.cc ├── error │ ├── compat_error.h │ ├── error.h │ └── error_unittest.cc ├── fn │ ├── __private │ │ └── signature.h │ ├── fn.h │ ├── fn_concepts.h │ ├── fn_concepts_unittest.cc │ ├── fn_dyn.h │ └── fn_dyn_unittest.cc ├── iter │ ├── __private │ │ ├── into_iterator_archetype.h │ │ ├── is_generator.h │ │ ├── iter_compare.h │ │ ├── iterator_archetype.h │ │ ├── iterator_end.h │ │ ├── range_begin.h │ │ └── step.h │ ├── adaptors │ │ ├── by_ref.h │ │ ├── chain.h │ │ ├── cloned.h │ │ ├── copied.h │ │ ├── cycle.h │ │ ├── enumerate.h │ │ ├── filter.h │ │ ├── filter_map.h │ │ ├── flat_map.h │ │ ├── flatten.h │ │ ├── fuse.h │ │ ├── inspect.h │ │ ├── map.h │ │ ├── map_while.h │ │ ├── moved.h │ │ ├── peekable.h │ │ ├── reverse.h │ │ ├── scan.h │ │ ├── skip.h │ │ ├── skip_while.h │ │ ├── step_by.h │ │ ├── take.h │ │ ├── take_while.h │ │ └── zip.h │ ├── compat_ranges.h │ ├── compat_ranges_unittest.cc │ ├── empty.h │ ├── empty_unittest.cc │ ├── extend.h │ ├── from_iterator.h │ ├── generator.h │ ├── generator_unittest.cc │ ├── into_iterator.h │ ├── iterator.h │ ├── iterator_concept.h │ ├── iterator_defn.h │ ├── iterator_impl.h │ ├── iterator_loop.h │ ├── iterator_ref.h │ ├── iterator_unittest.cc │ ├── once.h │ ├── once_unittest.cc │ ├── once_with.h │ ├── once_with_unittest.cc │ ├── product.h │ ├── repeat.h │ ├── repeat_unittest.cc │ ├── repeat_with.h │ ├── repeat_with_unittest.cc │ ├── size_hint.h │ ├── size_hint_impl.h │ ├── successors.h │ ├── successors_unittest.cc │ ├── sum.h │ ├── try_from_iterator.h │ └── zip.h ├── lib │ ├── __private │ │ └── forward_decl.h │ ├── lib.cc │ └── lib.h ├── macros │ ├── __private │ │ └── compiler_bugs.h │ ├── arch.h │ ├── assume.h │ ├── builtin.h │ ├── compiler.h │ ├── eval_and_concat.h │ ├── eval_macro.h │ ├── for_each.h │ ├── inline.h │ ├── lifetimebound.h │ ├── no_unique_address.h │ ├── nodebug.h │ ├── nonnull.h │ ├── pure.h │ └── remove_parens.h ├── marker │ ├── empty.h │ ├── unsafe.h │ └── unsafe_unittest.cc ├── mem │ ├── __private │ │ ├── data_size_finder.h │ │ └── ref_concepts.h │ ├── addressof.h │ ├── addressof_unittest.cc │ ├── clone.h │ ├── clone_unittest.cc │ ├── copy.h │ ├── forward.h │ ├── move.h │ ├── move_unittest.cc │ ├── never_value.h │ ├── never_value_macros.h │ ├── relocate.h │ ├── relocate_macros.h │ ├── relocate_unittest.cc │ ├── remove_rvalue_reference.h │ ├── replace.h │ ├── replace_unittest.cc │ ├── size_of.h │ ├── size_of_unittest.cc │ ├── swap.h │ ├── swap_unittest.cc │ ├── take.h │ └── take_unittest.cc ├── num │ ├── __private │ │ ├── check_integer_overflow.h │ │ ├── float_consts.inc │ │ ├── float_methods.inc │ │ ├── float_methods_impl.inc │ │ ├── float_ordering.h │ │ ├── int_log10.h │ │ ├── intrinsics.h │ │ ├── literals.h │ │ ├── literals_unittest.cc │ │ ├── primitive_type.h │ │ ├── signed_integer_consts.inc │ │ ├── signed_integer_methods.inc │ │ ├── signed_integer_methods_impl.inc │ │ ├── unsigned_integer_consts.inc │ │ ├── unsigned_integer_methods.inc │ │ └── unsigned_integer_methods_impl.inc │ ├── cast.h │ ├── cast_unittest.cc │ ├── f32_unittest.cc │ ├── f64_unittest.cc │ ├── float.h │ ├── float_concepts.h │ ├── float_impl.h │ ├── fp_category.h │ ├── i16_overflow_unittest.cc │ ├── i16_unittest.cc │ ├── i32_overflow_unittest.cc │ ├── i32_unittest.cc │ ├── i64_overflow_unittest.cc │ ├── i64_unittest.cc │ ├── i8_overflow_unittest.cc │ ├── i8_unittest.cc │ ├── integer_concepts.h │ ├── isize_overflow_unittest.cc │ ├── isize_unittest.cc │ ├── num_concepts.h │ ├── overflow_integer.h │ ├── overflow_integer_unittest.cc │ ├── signed_integer.h │ ├── signed_integer_impl.h │ ├── try_from_int_error.h │ ├── try_from_int_error_impl.h │ ├── types.h │ ├── u16_overflow_unittest.cc │ ├── u16_unittest.cc │ ├── u32_overflow_unittest.cc │ ├── u32_unittest.cc │ ├── u64_overflow_unittest.cc │ ├── u64_unittest.cc │ ├── u8_overflow_unittest.cc │ ├── u8_unittest.cc │ ├── unsigned_integer.h │ ├── unsigned_integer_consts.h │ ├── unsigned_integer_impl.h │ ├── uptr_overflow_unittest.cc │ ├── uptr_unittest.cc │ ├── usize_overflow_unittest.cc │ └── usize_unittest.cc ├── ops │ ├── range.h │ ├── range_literals.h │ ├── range_unittest.cc │ └── try.h ├── option │ ├── __private │ │ ├── is_option_type.h │ │ ├── is_tuple_type.h │ │ ├── marker.h │ │ └── storage.h │ ├── compat_option.h │ ├── compat_option_unittest.cc │ ├── option.h │ ├── option_iter.h │ ├── option_types_unittest.cc │ ├── option_unittest.cc │ └── state.h ├── prelude.h ├── project.md ├── ptr │ ├── as_ref.h │ ├── as_ref_unittest.cc │ ├── copy.h │ ├── nonnull.h │ ├── nonnull_types_unittest.cc │ ├── nonnull_unittest.cc │ ├── subclass.h │ ├── subclass_unittest.cc │ ├── swap.h │ └── swap_unittest.cc ├── result │ ├── __private │ │ ├── is_result_type.h │ │ ├── marker.h │ │ ├── result_state.h │ │ └── storage.h │ ├── ok_void.h │ ├── result.h │ ├── result_types_unittest.cc │ └── result_unittest.cc ├── string │ ├── __private │ │ ├── any_formatter.h │ │ ├── bytes_formatter.h │ │ ├── format_to_stream.h │ │ └── format_to_stream_unittest.cc │ ├── compat_string.h │ └── compat_string_unittest.cc ├── test │ ├── behaviour_types.h │ ├── behaviour_types_unittest.cc │ ├── ensure_use.cc │ ├── ensure_use.h │ └── no_copy_move.h └── tuple │ ├── __private │ └── storage.h │ ├── tuple.h │ ├── tuple_types_unittest.cc │ └── tuple_unittest.cc └── tools ├── clang-doc ├── clang-doc-default-stylesheet.css └── index.js ├── cmake └── OptionIf.cmake ├── configure.cmd ├── configure.sh ├── configure_clangd.cmd ├── run_subdoc.bat └── run_subdoc.sh /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | Standard: Latest 3 | AllowShortCaseLabelsOnASingleLine: true 4 | DerivePointerAlignment: false 5 | PointerAlignment: Left 6 | ReflowComments: false 7 | IndentPPDirectives: AfterHash 8 | 9 | # This would be nice but it also changes requires() and not wrapping requires clauses with () introduces clang-tidy bugs like: 10 | #``` 11 | # template 12 | # requires I <= sizeof 13 | # ...(Ts) constexpr inline const auto& at() const& noexcept { 14 | # sus_check(!moved_from(I)); 15 | # return __private::find_storage(storage_).value; 16 | # } 17 | #``` 18 | #AlignAfterOpenBracket: BlockIndent 19 | 20 | # TODO: Turn these one and reformat everything. 21 | #BreakBeforeBinaryOperators: true 22 | #BreakBeforeConceptDeclarations: Always 23 | #RequiresClausePosition: OwnLine 24 | #IndentRequiresClause: true 25 | #RequiresExpressionIndentation: OuterScope 26 | #QualifierOrder: ['inline', 'static', 'const', 'volatile', 'constexpr', 'type' ] 27 | #SortUsingDeclarations: true 28 | -------------------------------------------------------------------------------- /.clangd: -------------------------------------------------------------------------------- 1 | Diagnostics: 2 | # Disabled due to C++ concepts bug: https://github.com/llvm/llvm-project/issues/60702 3 | UnusedIncludes: None 4 | # Disabled since many errors in C++20 that don't match what the compiler produces. 5 | Suppress: "*" 6 | InlayHints: 7 | # Disabled due to UDLs getting hints: https://github.com/llvm/llvm-project/issues/63898 8 | ParameterNames: No 9 | # Disabled due noise in arrays. 10 | Designators: No 11 | # Disabled due to destroying visual line wrapping, since parameters don't start 12 | # on the next line. 13 | DeducedTypes: No 14 | -------------------------------------------------------------------------------- /.gclient: -------------------------------------------------------------------------------- 1 | # Copy this file to /.gclient to bootstrap gclient in a 2 | # standalone checkout of Subspace. 3 | 4 | solutions = [ 5 | { "name" : ".", 6 | "url" : "https://github.com/chromium/subspace", 7 | "deps_file" : "DEPS", 8 | "managed" : False, 9 | }, 10 | ] 11 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | *.cc text eol=lf 2 | *.h text eol=lf 3 | *.html text eol=lf 4 | *.md text eol=lf 5 | *.txt text eol=lf 6 | *.vcproj text eol=lf 7 | *.yml text eol=lf 8 | *.json text eol=lf 9 | *.txt text eol=lf 10 | -------------------------------------------------------------------------------- /.github/lock.yml: -------------------------------------------------------------------------------- 1 | name: 'Lock Threads' 2 | 3 | on: 4 | schedule: 5 | - cron: '0 0 * * *' 6 | workflow_dispatch: 7 | 8 | permissions: 9 | issues: write 10 | pull-requests: write 11 | 12 | concurrency: 13 | group: lock 14 | 15 | jobs: 16 | action: 17 | runs-on: ubuntu-latest 18 | steps: 19 | - uses: dessant/lock-threads@v4 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | docs 2 | out 3 | .cache 4 | .vscode/c_cpp_properties.json 5 | .vscode/settings.json 6 | .gclient_previous_sync_commits 7 | .gclient_entries 8 | third_party/llvm 9 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "third_party/googletest"] 2 | path = third_party/googletest 3 | url = https://github.com/google/googletest 4 | [submodule "third_party/fmt"] 5 | path = third_party/fmt 6 | url = https://github.com/fmtlib/fmt 7 | [submodule "third_party/buildtools"] 8 | path = third_party/buildtools 9 | url = https://chromium.googlesource.com/chromium/src/buildtools 10 | [submodule "third_party/nanobench"] 11 | path = third_party/nanobench 12 | url = https://github.com/martinus/nanobench 13 | [submodule "third_party/md4c"] 14 | path = third_party/md4c 15 | url = https://github.com/danakj/md4c 16 | [submodule "logos"] 17 | path = web/logos 18 | url = https://github.com/subspace-cpp/logos.git 19 | -------------------------------------------------------------------------------- /.hdoc.toml: -------------------------------------------------------------------------------- 1 | [project] 2 | name = "subspace" 3 | version = "0.0.1" 4 | 5 | # Optional, adding this will enable direct links from the documentation 6 | # to your source code. 7 | git_repo_url = "https://github.com/chromium/subspace/" 8 | git_default_branch = "main" 9 | 10 | [paths] 11 | compile_commands = "out/compile_commands.json" 12 | 13 | [pages] 14 | #homepage = "README.md" 15 | paths = [ 16 | "README.md", 17 | ] 18 | 19 | [ignore] 20 | ignore_private_members = true 21 | paths = [ 22 | "/third_party/", 23 | "_unittest.cc", 24 | "/__private/", 25 | ] 26 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We'd love to accept your patches and contributions to this project. There are 4 | just a few small guidelines you need to follow. 5 | 6 | ## Setting up a Development Environment 7 | 8 | See [BUILD.md](BUILD.md) for instructions on building Subspace and running 9 | tests. 10 | 11 | ## Contributor License Agreement 12 | 13 | Contributions to this project must be accompanied by a Contributor License 14 | Agreement (CLA). You (or your employer) retain the copyright to your 15 | contribution; this simply gives us permission to use and redistribute your 16 | contributions as part of the project. Head over to 17 | to see your current agreements on file or 18 | to sign a new one. 19 | 20 | You generally only need to submit a CLA once, so if you've already submitted one 21 | (even if it was for a different project), you probably don't need to do it 22 | again. 23 | 24 | ## Code Reviews 25 | 26 | All submissions, including submissions by project members, require review. We 27 | use GitHub pull requests for this purpose. Consult 28 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 29 | information on using pull requests. 30 | 31 | ## Community Guidelines 32 | 33 | This project follows 34 | [Google's Open Source Community Guidelines](https://opensource.google/conduct/). 35 | 36 | # Copyright 37 | 38 | All source files must include this header: 39 | ``` 40 | // Copyright 2023 Google LLC 41 | // 42 | // Licensed under the Apache License, Version 2.0 (the "License"); 43 | // you may not use this file except in compliance with the License. 44 | // You may obtain a copy of the License at 45 | // 46 | // https://www.apache.org/licenses/LICENSE-2.0 47 | // 48 | // Unless required by applicable law or agreed to in writing, software 49 | // distributed under the License is distributed on an "AS IS" BASIS, 50 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 51 | // See the License for the specific language governing permissions and 52 | // limitations under the License. 53 | ``` 54 | -------------------------------------------------------------------------------- /USAGE.md: -------------------------------------------------------------------------------- 1 | # Using Subspace 2 | 3 | Subspace can be integrated into any other project built with CMake. [Git 4 | submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules) can be 5 | used to pull the Subspace code into your project. 6 | 7 | ## Add `subspace` as a submodule 8 | 9 | ``` 10 | git submodule add https://github.com/chromium/subspace third_party/subspace 11 | ``` 12 | 13 | And grab its dependencies as submodules. 14 | ``` 15 | git submodule update --init --recursive 16 | ``` 17 | 18 | ## CMakeLists.txt 19 | 20 | ``` 21 | cmake_minimum_required(VERSION 3.25) 22 | 23 | project(subspace-example VERSION 0.1.0) 24 | 25 | set(CMAKE_CXX_STANDARD 20) 26 | set(CMAKE_CXX_STANDARD_REQUIRED True) 27 | 28 | add_subdirectory(third_party/subspace) 29 | 30 | add_executable(subspace-example src/main.cc) 31 | target_link_libraries(subspace-example PRIVATE subspace::lib) 32 | ``` 33 | 34 | ## src/main.cc 35 | ``` 36 | #include "sus/assertions/check.h" 37 | #include "sus/prelude.h" // Import core types. 38 | 39 | int main() { 40 | i32 a = 6; 41 | i32 b = 9; 42 | sus_check(a < b); 43 | return 0; 44 | } 45 | ``` 46 | 47 | ## Building 48 | 49 | Configure your build: 50 | ``` 51 | mkdir -p out/Debug 52 | CXX=clang++ cmake -GNinja -B out/Debug -BCMAKE_BUILD_TYPE=Debug 53 | ``` 54 | 55 | The above uses Ninja, which requires it to be installed. Drop the `-GNinja` otherwise. 56 | 57 | Compile: 58 | ``` 59 | cmake --build out/Debug 60 | ``` 61 | -------------------------------------------------------------------------------- /bench/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | add_executable(bench 16 | "bench_simd_chunks.cc" 17 | "bench_vec_map.cc" 18 | ) 19 | 20 | subspace_test_default_compile_options(bench) 21 | target_link_libraries(bench 22 | subspace::lib 23 | nanobench 24 | gtest_main 25 | ) 26 | -------------------------------------------------------------------------------- /subdoc/gen_tests/function-overloads/test.cc: -------------------------------------------------------------------------------- 1 | namespace n { 2 | 3 | /// Function with overloads 4 | /// #[doc.overloads=0] 5 | void multiple(); 6 | /// #[doc.overloads=0] 7 | void multiple(int); 8 | /// #[doc.overloads=0] 9 | void multiple(float); 10 | 11 | /// Separated overload 12 | /// #[doc.overloads=1] 13 | void multiple(unsigned); 14 | 15 | } // namespace n 16 | -------------------------------------------------------------------------------- /subdoc/gen_tests/gen_tests.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/tests/subdoc_gen_test.h" 16 | 17 | TEST_F(SubDocGenTest, FunctionOverloads) { 18 | EXPECT_TRUE(run_gen_test("function-overloads")); 19 | } 20 | 21 | TEST_F(SubDocGenTest, Markdown) { 22 | EXPECT_TRUE(run_gen_test("markdown")); 23 | } 24 | 25 | TEST_F(SubDocGenTest, NestedNamespace) { 26 | EXPECT_TRUE(run_gen_test("nested-namespace")); 27 | } 28 | 29 | TEST_F(SubDocGenTest, StructBasic) { 30 | EXPECT_TRUE(run_gen_test("struct-basic")); 31 | } 32 | 33 | TEST_F(SubDocGenTest, StructComplex) { 34 | EXPECT_TRUE(run_gen_test("struct-complex")); 35 | } 36 | 37 | TEST_F(SubDocGenTest, Templates) { 38 | EXPECT_TRUE(run_gen_test("templates")); 39 | } 40 | 41 | TEST_F(SubDocGenTest, TypenamesAcrossPaths) { 42 | EXPECT_TRUE(run_gen_test("typenames-across-paths")); 43 | } 44 | -------------------------------------------------------------------------------- /subdoc/gen_tests/markdown/test.cc: -------------------------------------------------------------------------------- 1 | // This tests a struct in the global namespace, and a struct in a private 2 | // namespace. The former is generated and the latter is not. 3 | 4 | /// The summary has html tags in it. 5 | struct S {}; 6 | 7 | /// The summary has 8 | /// newlines in 9 | /// it. 10 | struct N {}; 11 | 12 | /// A code block with syntax highlighting. 13 | /// ``` 14 | /// A "comment"; // At the end of the line. 15 | /// // At the start of the line. 16 | /// // And after some whitespace with "a string inside". 17 | /// A 'c' char // Not a 'c' char. 18 | /// A "st\"\\ring" or '\''.foo(); 19 | /// auto int; return keywords; 20 | /// not_auto a keyword nor auto_; 21 | /// []() { punctuation[0] + -0; } 22 | /// ``` 23 | struct Syntax {}; 24 | -------------------------------------------------------------------------------- /subdoc/gen_tests/nested-namespace/test.cc: -------------------------------------------------------------------------------- 1 | /// Outer namespace 2 | /// 3 | /// Outer details. 4 | namespace outer_namespace { 5 | 6 | /// Outer function 7 | void outer(); 8 | 9 | /// Inner namespace 10 | /// 11 | /// Inner details. 12 | namespace inner_namespace {} 13 | 14 | /// Empty namespace 15 | namespace empty {} 16 | 17 | } // namespace outer_namespace 18 | 19 | namespace outer_namespace::inner_namespace { 20 | 21 | /// Inner function 22 | void inner(); 23 | 24 | } // namespace outer_namespace::inner_namespace 25 | -------------------------------------------------------------------------------- /subdoc/gen_tests/struct-basic/test.cc: -------------------------------------------------------------------------------- 1 | // This tests a struct in the global namespace, and a struct in a private 2 | // namespace. The former is generated and the latter is not. 3 | 4 | /// Comment headline S 5 | struct S { 6 | /// #[doc.hidden] 7 | void f(); // This function will be ignored. 8 | 9 | /// This function will also be ignored. 10 | /// #[doc.hidden] 11 | void g(); 12 | }; 13 | 14 | namespace __private { 15 | /// This struct will be ignored. 16 | struct P {}; 17 | } 18 | -------------------------------------------------------------------------------- /subdoc/gen_tests/struct-complex/test.cc: -------------------------------------------------------------------------------- 1 | // This tests static and non-static members. They are intermixed and will 2 | // come out grouped and in alphabetical order. 3 | 4 | /// Comment headline on sus_macro_for_test_fn. 5 | /// 6 | /// Also refers to [the other]($sus_macro_for_test) 7 | #define sus_macro_for_test_fn(a, b, c, ...) 8 | /// Comment headline on sus_macro_for_test. 9 | /// 10 | /// Also refers to [the other]($sus_macro_for_test_fn) 11 | #define sus_macro_for_test 12 | 13 | /// A private macro which is not in the docs. 14 | #define _sus_macro_for_test 15 | 16 | struct OtherType {}; 17 | 18 | namespace __private { struct Private {}; } 19 | 20 | struct Base { 21 | /// Comment headline BaseMethod 22 | void BaseMethod(); 23 | }; 24 | 25 | /// Comment headline S 26 | struct S : Base { 27 | /// Comment headline void_method 28 | void void_method() const&; 29 | /// Comment headline static_type_method 30 | static OtherType static_type_method(); 31 | /// Comment headline type_method 32 | OtherType type_method(); 33 | /// Comment headline static_bool_method 34 | static bool static_bool_method(); 35 | 36 | // Overload should be grouped with the other int_method(). 37 | void int_method() volatile; 38 | 39 | /// Parameter isn't linked. 40 | void unlinked_param(__private::Private) {} 41 | 42 | /// Comment headline type_field 43 | const OtherType type_field; 44 | /// Comment headline static_type_member 45 | static volatile OtherType static_type_member; 46 | /// Comment headline bool_field 47 | bool bool_field; 48 | /// Comment headline static_bool_member 49 | static bool static_bool_member; 50 | 51 | /// Call operator with two overloads. 52 | int operator()() const&; 53 | bool operator()() &; 54 | float operator()() &&; 55 | }; 56 | -------------------------------------------------------------------------------- /subdoc/gen_tests/templates/test.cc: -------------------------------------------------------------------------------- 1 | template 3 | requires(AutoValue != ValueOfDependentType) 4 | struct TemplateStruct {}; 5 | 6 | template 7 | concept Concept = true; 8 | 9 | template 10 | struct S {}; 11 | 12 | template 13 | struct TemplateMethods { 14 | TemplateMethods(); 15 | 16 | T template_params(T t) 17 | requires(Concept); 18 | 19 | template 20 | U local_template_params(T t, U u); 21 | 22 | Concept auto concept_return(); 23 | 24 | void concept_param(Concept auto var); 25 | 26 | template 27 | requires(Concept) 28 | void requires_func(); 29 | 30 | template 31 | operator U(); 32 | 33 | template 34 | static U member; 35 | 36 | S> template_field; 37 | }; 38 | 39 | // Returns template parameter. 40 | template 41 | U template_function(U); 42 | 43 | /// Returns template instantiation. 44 | S return_template(); 45 | 46 | /// Has two overloads. 47 | template 48 | void requires_overload(A, B) 49 | requires(Concept); 50 | template 51 | void requires_overload(A, B) 52 | requires(!Concept); 53 | -------------------------------------------------------------------------------- /subdoc/lib/clang_resource_dir.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #include "sus/prelude.h" 22 | 23 | namespace subdoc { 24 | 25 | /// Find, store, and return the "resource dir" for finding system headers from 26 | /// Clang. 27 | /// 28 | /// Clang tools need to know where the "resource dir" is in order to find 29 | /// system headers there, if Clang was the compiler that's being used for 30 | /// building the target. 31 | /// 32 | /// For other compilers, the headers come from the system header location, but 33 | /// Clang has a resource dir that is known to the compiler, and which Subdoc 34 | /// can't know apriori. So it has to query the Clang compiler to get it. 35 | class ClangResourceDir { 36 | public: 37 | Option find_resource_dir(std::string_view tool); 38 | 39 | std::map> 40 | cache_; 41 | }; 42 | 43 | } // namespace subdoc 44 | -------------------------------------------------------------------------------- /subdoc/lib/doc_attributes.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/llvm.h" 18 | #include "sus/choice/choice.h" 19 | #include "sus/collections/vec.h" 20 | #include "sus/option/option.h" 21 | #include "sus/prelude.h" 22 | 23 | namespace subdoc { 24 | 25 | enum InheritPathElementType { 26 | InheritPathNamespace, 27 | InheritPathRecord, 28 | InheritPathFunction, 29 | }; 30 | 31 | using InheritPathElement = 32 | sus::Choice; 35 | 36 | struct DocAttributes { 37 | Option overload_set; 38 | clang::SourceLocation location; 39 | Option> inherit; 40 | bool hidden = false; 41 | 42 | DocAttributes clone() const { 43 | return DocAttributes(sus::clone(overload_set), // 44 | sus::clone(location), // 45 | sus::clone(inherit), // 46 | sus::clone(hidden)); 47 | } 48 | }; 49 | 50 | } // namespace subdoc 51 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_alias.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/database.h" 18 | #include "subdoc/lib/gen/html_writer.h" 19 | #include "subdoc/lib/gen/json_writer.h" 20 | #include "subdoc/lib/gen/markdown_to_html.h" 21 | #include "sus/prelude.h" 22 | #include "sus/result/result.h" 23 | 24 | namespace subdoc::gen { 25 | 26 | sus::Result generate_alias_reference( 27 | HtmlWriter::OpenUl& items_list, const AliasElement& element, 28 | ParseMarkdownPageState& page_state) noexcept; 29 | 30 | sus::Result generate_alias_json( 31 | const Database& db, JsonWriter::JsonArray& search_documents, 32 | std::string_view parent_full_name, const AliasElement& element, 33 | const Options& options) noexcept; 34 | 35 | } // namespace subdoc::gen 36 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_concept.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/database.h" 18 | #include "subdoc/lib/gen/html_writer.h" 19 | #include "subdoc/lib/gen/json_writer.h" 20 | #include "subdoc/lib/gen/markdown_to_html.h" 21 | #include "subdoc/lib/gen/options.h" 22 | #include "sus/prelude.h" 23 | #include "sus/result/result.h" 24 | 25 | namespace subdoc::gen { 26 | 27 | sus::Result generate_concept( 28 | const Database& db, const ConceptElement& element, 29 | sus::Slice namespaces, 30 | JsonWriter::JsonArray& search_documents, const Options& options) noexcept; 31 | 32 | sus::Result generate_concept_reference( 33 | HtmlWriter::OpenUl& items_list, const ConceptElement& element, 34 | ParseMarkdownPageState& page_state) noexcept; 35 | 36 | } // namespace subdoc::gen 37 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_function.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/database.h" 18 | #include "subdoc/lib/gen/html_writer.h" 19 | #include "subdoc/lib/gen/json_writer.h" 20 | #include "subdoc/lib/gen/markdown_to_html.h" 21 | #include "subdoc/lib/gen/options.h" 22 | #include "sus/prelude.h" 23 | #include "sus/result/result.h" 24 | 25 | namespace subdoc::gen { 26 | 27 | sus::Result generate_function( 28 | const Database& db, const FunctionElement& e, 29 | sus::Slice namespaces, 30 | JsonWriter::JsonArray& search_documents, const Options& options) noexcept; 31 | 32 | sus::Result generate_function_reference( 33 | HtmlWriter::OpenUl& items_list, const FunctionElement& e, 34 | ParseMarkdownPageState& page_state) noexcept; 35 | 36 | sus::Result generate_function_method_reference( 37 | HtmlWriter::OpenDiv& items_list, const FunctionElement& e, 38 | bool with_constraints, ParseMarkdownPageState& page_state) noexcept; 39 | 40 | } // namespace subdoc::gen 41 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_head.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "subdoc/lib/gen/html_writer.h" 20 | #include "subdoc/lib/gen/options.h" 21 | 22 | namespace subdoc::gen { 23 | 24 | void generate_head(HtmlWriter& html, std::string_view title, 25 | std::string_view description, 26 | const std::filesystem::path& file_path, 27 | const Options& options) noexcept; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_macro.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/database.h" 18 | #include "subdoc/lib/gen/html_writer.h" 19 | #include "subdoc/lib/gen/json_writer.h" 20 | #include "subdoc/lib/gen/markdown_to_html.h" 21 | #include "subdoc/lib/gen/options.h" 22 | #include "sus/prelude.h" 23 | #include "sus/result/result.h" 24 | 25 | namespace subdoc::gen { 26 | 27 | sus::Result generate_macro( 28 | const Database& db, const MacroElement& e, 29 | sus::Slice namespaces, 30 | JsonWriter::JsonArray& search_documents, const Options& options) noexcept; 31 | 32 | sus::Result generate_macro_reference( 33 | HtmlWriter::OpenUl& items_list, const MacroElement& e, 34 | ParseMarkdownPageState& page_state) noexcept; 35 | 36 | } // namespace subdoc::gen 37 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_namespace.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/database.h" 18 | #include "subdoc/lib/gen/html_writer.h" 19 | #include "subdoc/lib/gen/json_writer.h" 20 | #include "subdoc/lib/gen/markdown_to_html.h" 21 | #include "subdoc/lib/gen/options.h" 22 | #include "sus/prelude.h" 23 | #include "sus/result/result.h" 24 | 25 | namespace subdoc::gen { 26 | 27 | sus::Result generate_namespace( 28 | const Database& db, const NamespaceElement& element, 29 | Vec ancestors, 30 | JsonWriter::JsonArray& search_documents, 31 | const Options& options) noexcept; 32 | 33 | sus::Result generate_namespace_reference( 34 | HtmlWriter::OpenLi& li, const NamespaceElement& element, 35 | ParseMarkdownPageState& page_state) noexcept; 36 | 37 | } // namespace subdoc::gen 38 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_nav.h: -------------------------------------------------------------------------------- 1 | 2 | // Copyright 2023 Google LLC 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // https://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | 16 | #pragma once 17 | 18 | #include "subdoc/lib/database.h" 19 | #include "subdoc/lib/gen/html_writer.h" 20 | #include "subdoc/lib/gen/options.h" 21 | #include "sus/prelude.h" 22 | 23 | namespace subdoc::gen { 24 | 25 | enum class SidebarLinkStyle { 26 | Parent, 27 | GroupHeader, 28 | Item, 29 | NestedItem, 30 | }; 31 | 32 | struct SidebarLink { 33 | SidebarLinkStyle style; 34 | std::string text; 35 | std::string href; 36 | }; 37 | 38 | void generate_nav(HtmlWriter::OpenBody& body, const Database& db, 39 | std::string_view pretitle, std::string_view title, 40 | std::string_view subtitle, Vec links, 41 | const Options& options) noexcept; 42 | 43 | } // namespace subdoc::gen 44 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_record.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/database.h" 18 | #include "subdoc/lib/gen/html_writer.h" 19 | #include "subdoc/lib/gen/json_writer.h" 20 | #include "subdoc/lib/gen/markdown_to_html.h" 21 | #include "subdoc/lib/gen/options.h" 22 | #include "sus/prelude.h" 23 | #include "sus/result/result.h" 24 | 25 | namespace subdoc::gen { 26 | 27 | sus::Result generate_record( 28 | const Database& db, const RecordElement& element, 29 | sus::Slice namespaces, 30 | Vec type_ancestors, 31 | JsonWriter::JsonArray& search_documents, const Options& options) noexcept; 32 | 33 | sus::Result generate_record_reference( 34 | HtmlWriter::OpenUl& items_list, const RecordElement& element, 35 | ParseMarkdownPageState& page_state) noexcept; 36 | 37 | sus::Result generate_field_reference( 38 | HtmlWriter::OpenDiv& div, const FieldElement& element, bool static_fields, 39 | ParseMarkdownPageState& page_state) noexcept; 40 | 41 | } // namespace subdoc::gen 42 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_requires.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/lib/database.h" 16 | #include "subdoc/lib/gen/html_writer.h" 17 | 18 | namespace subdoc::gen { 19 | 20 | void generate_requires_constraints(HtmlWriter::OpenDiv& div, 21 | const RequiresConstraints& constraints); 22 | 23 | } 24 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_search.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/lib/gen/generate_search.h" 16 | 17 | #include "sus/prelude.h" 18 | 19 | namespace subdoc::gen { 20 | 21 | void generate_search_header(HtmlWriter::OpenMain& main) noexcept { 22 | { 23 | auto search_nav = main.open_nav(); 24 | search_nav.add_class("search-nav"); 25 | 26 | auto search_form = search_nav.open_form(); 27 | search_form.add_class("search-form"); 28 | 29 | { 30 | auto search_input = search_form.open_input(); 31 | search_input.add_class("search-input"); 32 | search_input.add_name("search"); 33 | search_input.add_autocomplete("off"); 34 | search_input.add_spellcheck("false"); 35 | search_input.add_placeholder("Click or press 'S' to search..."); 36 | } 37 | } 38 | 39 | { 40 | auto search_results = main.open_section(); 41 | search_results.add_class("search-results"); 42 | 43 | { 44 | auto header = search_results.open_h(1u); 45 | header.add_class("search-results-header"); 46 | } 47 | { 48 | auto div = search_results.open_div(); 49 | div.add_class("search-results-content"); 50 | } 51 | } 52 | } 53 | 54 | void generate_search_result_loading( 55 | HtmlWriter::OpenSection& main_content) noexcept { 56 | main_content.write_html(""); 57 | } 58 | 59 | } // namespace subdoc::gen 60 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_search.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "subdoc/lib/gen/generate_cpp_path.h" 20 | #include "subdoc/lib/gen/html_writer.h" 21 | #include "subdoc/lib/gen/options.h" 22 | #include "sus/iter/iterator.h" 23 | #include "sus/prelude.h" 24 | 25 | namespace subdoc::gen { 26 | 27 | void generate_search_header(HtmlWriter::OpenMain& main) noexcept; 28 | 29 | // Synchronously show search results, replacing the main content of the 30 | // page, if a search is being done. Must be after the main-content section 31 | // is created, since it refers to it. So that is the expected parameter here. 32 | void generate_search_result_loading( 33 | HtmlWriter::OpenSection& main_content) noexcept; 34 | 35 | } // namespace subdoc::gen 36 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_source_link.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/lib/gen/generate_source_link.h" 16 | 17 | namespace subdoc::gen { 18 | 19 | void generate_source_link(HtmlWriter::OpenDiv& div, 20 | const CommentElement& element) { 21 | if (element.source_link.is_some()) { 22 | const SourceLink& link = element.source_link.as_value(); 23 | 24 | auto source_link_div = div.open_div(HtmlWriter::SingleLine); 25 | source_link_div.add_class("src"); 26 | source_link_div.add_class("rightside"); 27 | { 28 | auto a = source_link_div.open_a(); 29 | { 30 | std::ostringstream str; 31 | str << link.file_path << "#" << link.line; 32 | a.add_href(sus::move(str).str()); 33 | } 34 | a.write_text("source"); 35 | } 36 | } 37 | } 38 | 39 | } // namespace subdoc::gen 40 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_source_link.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/gen/options.h" 18 | #include "subdoc/lib/database.h" 19 | #include "subdoc/lib/gen/html_writer.h" 20 | 21 | namespace subdoc::gen { 22 | 23 | void generate_source_link(HtmlWriter::OpenDiv& div, 24 | const CommentElement& element); 25 | 26 | } // namespace subdoc::gen 27 | -------------------------------------------------------------------------------- /subdoc/lib/gen/generate_type.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/lib/database.h" 18 | #include "subdoc/lib/gen/html_writer.h" 19 | #include "subdoc/lib/type.h" 20 | #include "sus/fn/fn.h" 21 | #include "sus/prelude.h" 22 | 23 | namespace subdoc::gen { 24 | 25 | void generate_type(HtmlWriter::OpenDiv& div, const LinkedType& linked_type, 26 | Option&> 27 | var_name_fn) noexcept; 28 | 29 | } // namespace subdoc::gen 30 | -------------------------------------------------------------------------------- /subdoc/lib/gen/markdown_to_html.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "subdoc/lib/database.h" 20 | #include "subdoc/lib/gen/options.h" 21 | #include "sus/error/error.h" 22 | #include "sus/result/result.h" 23 | 24 | namespace subdoc::gen { 25 | 26 | struct ParseMarkdownPageState { 27 | const Database& db; 28 | const Options& options; 29 | std::unordered_map self_link_counts; 30 | }; 31 | 32 | struct MarkdownToHtmlError { 33 | std::string message; 34 | }; 35 | 36 | struct MarkdownToHtml { 37 | std::string full_html; 38 | std::string summary_html; 39 | std::string summary_text; 40 | }; 41 | 42 | sus::Result markdown_to_html( 43 | const Comment& comment, ParseMarkdownPageState& page_state) noexcept; 44 | 45 | } // namespace subdoc::gen 46 | 47 | // `sus::error::Error` implementation. 48 | template <> 49 | struct sus::error::ErrorImpl { 50 | using MarkdownToHtmlError = subdoc::gen::MarkdownToHtmlError; 51 | static std::string display(const MarkdownToHtmlError& e) noexcept { 52 | return fmt::format("markdown parsing failed: {}", e.message); 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /subdoc/lib/gen/search.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/lib/gen/search.h" 16 | 17 | #include 18 | 19 | #include "sus/prelude.h" 20 | 21 | namespace subdoc::gen { 22 | 23 | std::string split_for_search(std::string_view s) noexcept { 24 | if (s.size() == 0u) return std::string(); 25 | 26 | bool is_camel_case = false; 27 | for (usize i; i < s.size() - 1u; i += 1u) { 28 | if (s[i] >= 'a' && s[i] <= 'z' && s[i + 1u] >= 'A' && s[i + 1u] <= 'Z') { 29 | is_camel_case = true; 30 | } 31 | } 32 | 33 | std::stringstream split_string; 34 | const char* query = ":_"; 35 | if (is_camel_case) { 36 | query = ":_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 37 | } 38 | auto last = 0_usize, start = 0_usize; 39 | while (true) { 40 | auto found = s.find_first_of(query, start); 41 | if (found == std::string::npos) { 42 | split_string << std::string_view(s).substr(last); 43 | break; 44 | } 45 | auto append = std::string_view(s).substr(last, found - last); 46 | if (append.size() > 0u) split_string << append << std::string_view(" "); 47 | 48 | auto view = std::string_view(s).substr(found); 49 | if (view.starts_with(":") || view.starts_with("_")) { 50 | last = found + 1u; 51 | start = found + 1u; 52 | } else { 53 | last = found; 54 | start = found + 1; 55 | } 56 | } 57 | 58 | return sus::move(split_string).str(); 59 | } 60 | 61 | } // namespace subdoc::gen 62 | -------------------------------------------------------------------------------- /subdoc/lib/gen/search.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | #include 19 | 20 | namespace subdoc::gen { 21 | 22 | std::string split_for_search(std::string_view s) noexcept; 23 | 24 | } 25 | -------------------------------------------------------------------------------- /subdoc/lib/method_qualifier.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | namespace subdoc { 18 | 19 | enum class MethodQualifier { 20 | Const, 21 | ConstLValue, 22 | ConstRValue, 23 | Mutable, 24 | MutableLValue, 25 | MutableRValue, 26 | }; 27 | 28 | } 29 | -------------------------------------------------------------------------------- /subdoc/lib/parse_comment.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "subdoc/lib/doc_attributes.h" 20 | #include "subdoc/llvm.h" 21 | #include "sus/error/error.h" 22 | #include "sus/result/result.h" 23 | 24 | namespace subdoc { 25 | 26 | struct ParsedComment { 27 | DocAttributes attributes; 28 | std::string text; 29 | }; 30 | 31 | struct ParseCommentError { 32 | std::string message; 33 | }; 34 | 35 | sus::Result parse_comment( 36 | clang::ASTContext& ast_cx, const clang::RawComment& raw, 37 | std::string_view self_name) noexcept; 38 | 39 | } // namespace subdoc 40 | 41 | // `sus::error::Error` implementation. 42 | template <> 43 | struct sus::error::ErrorImpl { 44 | using ParseCommentError = subdoc::ParseCommentError; 45 | static std::string display(const ParseCommentError& e) noexcept { 46 | return fmt::format("doc comment was invalid: {}", e.message); 47 | } 48 | }; 49 | -------------------------------------------------------------------------------- /subdoc/lib/record_type.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | namespace subdoc { 18 | 19 | enum class RecordType { Class, Struct, Union }; 20 | 21 | } 22 | -------------------------------------------------------------------------------- /subdoc/lib/requires.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/llvm.h" 18 | #include "sus/choice/choice.h" 19 | #include "sus/prelude.h" 20 | 21 | namespace subdoc { 22 | 23 | struct RequiresConceptConstraint { 24 | // TODO: Make this a reference to a Concept in the Database when it's present 25 | // there. 26 | std::string concept_name; 27 | // TODO: These can be types that are in the Database, so they could be linked? 28 | Vec args; 29 | }; 30 | 31 | enum class RequiresConstraintTag { 32 | Concept, 33 | Text, 34 | }; 35 | // clang-format off 36 | using RequiresConstraint = sus::choice_type::Choice; 40 | // clang-format on 41 | 42 | struct RequiresConstraints { 43 | Vec list; 44 | }; 45 | 46 | void requires_constraints_add_expr(RequiresConstraints& constraints, 47 | const clang::ASTContext& context, 48 | clang::Preprocessor& preprocessor, 49 | const clang::Expr* e) noexcept; 50 | 51 | } // namespace subdoc 52 | -------------------------------------------------------------------------------- /subdoc/lib/run.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "subdoc/lib/database.h" 20 | #include "subdoc/lib/run_options.h" 21 | #include "subdoc/llvm.h" 22 | #include "sus/collections/vec.h" 23 | #include "sus/error/error.h" 24 | #include "sus/prelude.h" 25 | #include "sus/result/result.h" 26 | 27 | namespace subdoc { 28 | 29 | struct DiagnosticResults { 30 | Vec locations; 31 | }; 32 | static_assert(sus::mem::Move); 33 | 34 | sus::Result run_test( 35 | std::string pretend_file_name, std::string content, 36 | sus::Slice command_line_args, 37 | const RunOptions& options) noexcept; 38 | 39 | sus::Result run_files( 40 | const clang::tooling::CompilationDatabase& compdb, 41 | Vec paths, 42 | llvm::IntrusiveRefCntPtr fs, 43 | const RunOptions& options) noexcept; 44 | 45 | } // namespace subdoc 46 | 47 | template <> 48 | struct sus::error::ErrorImpl { 49 | using DiagnosticResults = subdoc::DiagnosticResults; 50 | static std::string display(const DiagnosticResults& e) noexcept { 51 | return fmt::format("errors occured at: {}", e.locations); 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /subdoc/lib/stmt_to_string.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/llvm.h" 16 | 17 | namespace subdoc { 18 | 19 | // TODO: Instead of making a string, we should build a data structure that can 20 | // be stringified but also contains info on all types so that we can look them 21 | // up in the database. 22 | std::string stmt_to_string(const clang::Stmt& stmt, 23 | const clang::SourceManager& sm, 24 | clang::Preprocessor& preprocessor) { 25 | const char* start = sm.getCharacterData(stmt.getBeginLoc()); 26 | const char* end = 27 | sm.getCharacterData(preprocessor.getLocForEndOfToken(stmt.getEndLoc())); 28 | return std::string((std::string_view(start, end - start))); 29 | } 30 | 31 | } // namespace subdoc 32 | -------------------------------------------------------------------------------- /subdoc/lib/stmt_to_string.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "subdoc/llvm.h" 18 | 19 | namespace subdoc { 20 | 21 | // TODO: Instead of making a string, we should build a data structure that can 22 | // be stringified but also contains info on all types so that we can look them 23 | // up in the database. 24 | std::string stmt_to_string(const clang::Stmt& stmt, 25 | const clang::SourceManager& sm, 26 | clang::Preprocessor& preprocessor); 27 | 28 | } // namespace subdoc 29 | -------------------------------------------------------------------------------- /subdoc/lib/unique_symbol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "subdoc/llvm.h" 20 | #include "sus/prelude.h" 21 | 22 | namespace subdoc { 23 | 24 | struct UniqueSymbol { 25 | std::array bytes; 26 | 27 | std::string to_string() const noexcept { 28 | std::ostringstream s; 29 | for (uint8_t b : bytes) { 30 | s << uint32_t{b}; 31 | } 32 | return s.str(); 33 | } 34 | 35 | bool operator==(const UniqueSymbol& other) const = default; 36 | }; 37 | 38 | inline UniqueSymbol unique_from_decl(const clang::Decl* decl) noexcept { 39 | std::ostringstream s; 40 | 41 | // The USR, or Unique Symbol Resolution, is a unique value for a Decl across 42 | // all translation units. It does not differentiate on overlods well 43 | // (specifically on requires clauses). However the UniqueSymbol is not used 44 | // for functions/mehhods anyhow as we collapse overloads. 45 | llvm::SmallString<128> usr; 46 | if (!clang::index::generateUSRForDecl(decl, usr)) { 47 | s << std::string(sus::move(usr)); 48 | } else { 49 | s << decl->getCanonicalDecl()->getBeginLoc().getRawEncoding(); 50 | } 51 | 52 | return UniqueSymbol{ 53 | .bytes = llvm::SHA1::hash(llvm::arrayRefFromStringRef(s.str())), 54 | }; 55 | } 56 | 57 | } // namespace subdoc 58 | 59 | namespace std { 60 | 61 | template <> 62 | struct hash { 63 | auto operator()(const subdoc::UniqueSymbol& u) const { 64 | size_t h = 0; 65 | for (auto b : u.bytes) h += b; 66 | return h; 67 | } 68 | }; 69 | 70 | } // namespace std 71 | -------------------------------------------------------------------------------- /subdoc/tests/concepts_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/tests/subdoc_test.h" 16 | 17 | namespace { 18 | 19 | TEST_F(SubDocTest, ConceptInToplevel) { 20 | auto result = run_code(R"( 21 | /// Comment headline 22 | template 23 | concept S = true; 24 | )"); 25 | ASSERT_TRUE(result.is_ok()); 26 | subdoc::Database db = sus::move(result).unwrap(); 27 | EXPECT_TRUE(has_concept_comment(db, "2:5", "

Comment headline

")); 28 | } 29 | 30 | TEST_F(SubDocTest, ConceptInNamespace) { 31 | auto result = run_code(R"( 32 | namespace a { 33 | /// Comment headline 34 | template 35 | concept S = true; 36 | } 37 | )"); 38 | ASSERT_TRUE(result.is_ok()); 39 | subdoc::Database db = sus::move(result).unwrap(); 40 | EXPECT_TRUE(has_concept_comment(db, "3:5", "

Comment headline

")); 41 | } 42 | 43 | } // namespace 44 | -------------------------------------------------------------------------------- /subdoc/tests/cpp_version.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/assertions/unreachable.h" 20 | 21 | namespace subdoc::tests { 22 | 23 | enum class SubDocCppVersion { 24 | Cpp20, 25 | }; 26 | 27 | inline std::string_view cpp_version_flag(SubDocCppVersion v) noexcept { 28 | switch (v) { 29 | case SubDocCppVersion::Cpp20: return "-std=c++20"; 30 | } 31 | sus::unreachable(); 32 | } 33 | 34 | } // namespace subdoc::tests 35 | -------------------------------------------------------------------------------- /subdoc/tests/doc_attributes_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/tests/subdoc_test.h" 16 | 17 | TEST_F(SubDocTest, DocAttributesInheritFunction) { 18 | auto result = run_code(R"( 19 | /// Comment headline 20 | void a() {} 21 | /// #[doc.inherit=[f]a] 22 | void b() {} 23 | )"); 24 | ASSERT_TRUE(result.is_ok()); 25 | subdoc::Database db = sus::move(result).unwrap(); 26 | EXPECT_TRUE(has_function_comment(db, "2:5", "

Comment headline

")); 27 | EXPECT_TRUE(has_function_comment(db, "4:5", "

Comment headline

")); 28 | } 29 | 30 | TEST_F(SubDocTest, DocAttributesSelf) { 31 | auto result = run_code(R"( 32 | struct S { 33 | /// Comment @doc.self headline 34 | void a() {} 35 | }; 36 | )"); 37 | ASSERT_TRUE(result.is_ok()); 38 | subdoc::Database db = sus::move(result).unwrap(); 39 | EXPECT_TRUE(has_method_comment(db, "3:7", "

Comment S headline

")); 40 | } 41 | 42 | TEST_F(SubDocTest, DocAttributesSelfOnFriend) { 43 | auto result = run_code(R"( 44 | struct S { 45 | /// Comment @doc.self headline 46 | friend void a() {} 47 | }; 48 | )"); 49 | ASSERT_TRUE(result.is_ok()); 50 | subdoc::Database db = sus::move(result).unwrap(); 51 | EXPECT_TRUE(has_function_comment(db, "3:7", "

Comment S headline

")); 52 | } 53 | -------------------------------------------------------------------------------- /subdoc/tests/styles_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/tests/subdoc_test.h" 16 | 17 | TEST_F(SubDocTest, Mixed) { 18 | auto result = run_code(R"( 19 | /// Comment headline 20 | // Implementation details. 21 | struct S {}; 22 | )"); 23 | ASSERT_TRUE(result.is_ok()); 24 | subdoc::Database db = sus::move(result).unwrap(); 25 | EXPECT_TRUE(has_record_comment(db, "2:5", "

Comment headline

")); 26 | } 27 | 28 | TEST_F(SubDocTest, CppStyle) { 29 | auto result = run_code(R"( 30 | // Implementation details. 31 | struct S {}; 32 | )"); 33 | ASSERT_TRUE(result.is_ok()); 34 | subdoc::Database db = sus::move(result).unwrap(); 35 | EXPECT_FALSE(db.has_any_comments()); 36 | } 37 | 38 | TEST_F(SubDocTest, JavaDocStyle) { 39 | auto result = run_code(R"( 40 | /** Comment headline */ 41 | struct S {}; 42 | )"); 43 | ASSERT_TRUE(result.is_ok()); 44 | subdoc::Database db = sus::move(result).unwrap(); 45 | EXPECT_TRUE(has_record_comment(db, "2:5", "

Comment headline

")); 46 | } 47 | 48 | TEST_F(SubDocTest, JavaDocStyleBody) { 49 | auto result = run_code(R"( 50 | /** Comment headline 51 | * 52 | * Comment body. 53 | */ 54 | struct S {}; 55 | )"); 56 | ASSERT_TRUE(result.is_ok()); 57 | subdoc::Database db = sus::move(result).unwrap(); 58 | EXPECT_TRUE(has_record_comment(db, "2:5", "

Comment headline

")); 59 | } 60 | -------------------------------------------------------------------------------- /subdoc/tests/test_main.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "subdoc/tests/test_main.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/iter/iterator.h" 19 | #include "sus/prelude.h" 20 | 21 | namespace { 22 | Vec* args; 23 | } 24 | 25 | sus::Slice test_main_command_line_args() { return *args; } 26 | 27 | int main(int argc, char** argv) { 28 | testing::InitGoogleTest(&argc, argv); 29 | 30 | auto slice = sus::SliceMut::from_raw_parts_mut( 31 | unsafe_fn, argv, usize::try_from(argc).unwrap()); 32 | args = new Vec( 33 | slice.iter() 34 | .map([](char* arg) { return std::string_view(arg); }) 35 | .collect>()); 36 | 37 | return RUN_ALL_TESTS(); 38 | } 39 | -------------------------------------------------------------------------------- /subdoc/tests/test_main.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/prelude.h" 18 | 19 | /// The command line arguments passed when running the tests. 20 | /// 21 | /// The arguments have a static lifetime, so the returned reference, and the 22 | /// pointers in the slice, are never invalidated. 23 | sus::Slice test_main_command_line_args(); 24 | -------------------------------------------------------------------------------- /sus/assertions/check.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/assertions/panic.h" 18 | 19 | /// Verifies that the input, evaluated to a `bool`, is true. Otherwise, it will 20 | /// [`panic`]($sus::panic), printing a message and terminating the program. 21 | /// 22 | /// See [`sus_check_with_message`]($sus_check_with_message) to add a 23 | /// message to the display of the panic. 24 | /// 25 | /// The displayed output can be controlled by overriding the behaviour of 26 | /// [`sus::panic`]($sus::panic) as described there. 27 | #define sus_check(...) \ 28 | if (![](bool x) { return x; }(__VA_ARGS__)) [[unlikely]] { \ 29 | ::sus::panic(); \ 30 | } \ 31 | static_assert(true) 32 | 33 | /// Verifies that the input `cond`, evaluated to a `bool`, is true. Otherwise, 34 | /// it will [`panic`]($sus::panic), printing a customized message, and 35 | /// terminating the program. 36 | /// 37 | /// Use [`sus_check`]($sus_check) when there's nothing useful to add 38 | /// in the message. 39 | /// 40 | /// The displayed output can be controlled by overriding the behaviour of 41 | /// [`sus::panic`]($sus::panic) as described there. If the 42 | /// `SUS_PROVIDE_PRINT_PANIC_MESSAGE_HANDLER` macro does not consume the `msg`, 43 | /// this macro will avoid instantiating it at all. 44 | #define sus_check_with_message(cond, msg) \ 45 | if (!(cond)) [[unlikely]] { \ 46 | ::sus::panic(msg); \ 47 | } \ 48 | static_assert(true) 49 | -------------------------------------------------------------------------------- /sus/assertions/check_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/assertions/check.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | 19 | // Incredibly, on Posix we can use [0-9] but on Windows we can't. Yet on Windows 20 | // we can use `\d` and on Posix we can't (or it doesn't match). 21 | #if GTEST_USES_SIMPLE_RE 22 | # define DIGIT "\\d" 23 | #else 24 | # define DIGIT "[0-9]" 25 | #endif 26 | 27 | namespace sus { 28 | namespace { 29 | 30 | TEST(Check, CheckPasses) { 31 | sus_check(true); 32 | sus_check_with_message(true, "hello world"); 33 | } 34 | 35 | TEST(Check, CheckFails) { 36 | #if GTEST_HAS_DEATH_TEST 37 | EXPECT_DEATH(sus_check(false), 38 | "at .*check_unittest.cc:" DIGIT "+:" DIGIT "+\n$"); 39 | #endif 40 | } 41 | 42 | TEST(Check, WithMessage) { 43 | #if GTEST_HAS_DEATH_TEST 44 | EXPECT_DEATH(sus_check_with_message(false, "hello world"), 45 | "'hello world', .*check_unittest.cc:" DIGIT "+:" DIGIT "+\n$"); 46 | #endif 47 | #if GTEST_HAS_DEATH_TEST 48 | // Verify it does not read past the string_view's end. 49 | EXPECT_DEATH(sus_check_with_message( 50 | false, std::string_view("hello world123").substr(0u, 11u)), 51 | "'hello world', .*check_unittest.cc:" DIGIT "+:" DIGIT "+\n$"); 52 | #endif 53 | #if GTEST_HAS_DEATH_TEST 54 | EXPECT_DEATH(sus_check_with_message(false, std::string("hello world")), 55 | "'hello world', .*check_unittest.cc:" DIGIT "+:" DIGIT "+\n$"); 56 | #endif 57 | } 58 | 59 | } // namespace 60 | } // namespace sus 61 | -------------------------------------------------------------------------------- /sus/assertions/debug_check.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/assertions/check.h" 18 | #include "sus/macros/assume.h" 19 | #include "sus/macros/compiler.h" 20 | 21 | /// Check a condition in debug builds, causing a `sus::panic()` if the condition 22 | /// fails. Nothing is checked in release builds. 23 | /// 24 | /// The condition must not have side effects, and should not call any functions 25 | /// or methods (including operator methods), as it will often not have any 26 | /// effect in that case (especially in MSVC https://godbolt.org/z/7T7E3P598) 27 | #define sus_debug_check(...) _sus__debug_check_impl(__VA_ARGS__) 28 | 29 | #if !defined(NDEBUG) 30 | #define _sus__debug_check_impl(...) sus_check(__VA_ARGS__) 31 | #else 32 | #define _sus__debug_check_impl(...) 33 | #endif 34 | -------------------------------------------------------------------------------- /sus/assertions/panic.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/assertions/panic.h" 16 | 17 | #include 18 | 19 | #include "sus/num/cast.h" 20 | 21 | namespace sus::assertions::__private { 22 | 23 | static void print_panic_location(const PanicLocation& location) noexcept { 24 | fprintf(stderr, "PANIC! at %s:%u:%u\n", location.file_name, location.line, 25 | location.column); 26 | } 27 | 28 | // Defined outside the header to avoid fprintf in the header. 29 | void print_panic_message(std::string_view msg, 30 | const PanicLocation& location) noexcept { 31 | if (msg.empty()) { 32 | print_panic_location(location); 33 | } else { 34 | fprintf(stderr, "PANIC! at '"); 35 | for (char c : msg) fprintf(stderr, "%c", c); 36 | fprintf(stderr, "', %s:%u:%u\n", location.file_name, location.line, 37 | location.column); 38 | } 39 | } 40 | 41 | } // namespace sus::assertions::__private 42 | -------------------------------------------------------------------------------- /sus/assertions/unreachable_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/assertions/unreachable.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/prelude.h" 19 | 20 | // Incredibly, on Posix we can use [0-9] but on Windows we can't. Yet on Windows 21 | // we can use `\d` and on Posix we can't (well, it causes UBSan to fire during 22 | // compilation and then it doesn't match). 23 | #if GTEST_USES_SIMPLE_RE 24 | # define DIGIT "\\d" 25 | #else 26 | # define DIGIT "[0-9]" 27 | #endif 28 | 29 | namespace sus { 30 | namespace { 31 | 32 | TEST(UnreachableDeathTest, Unreachable) { 33 | #if GTEST_HAS_DEATH_TEST 34 | EXPECT_DEATH(sus::unreachable(), 35 | "^PANIC! at .*unreachable_unittest.cc:" DIGIT "+:" DIGIT "+\n$"); 36 | #endif 37 | } 38 | 39 | TEST(Unreachable, Unchecked) { 40 | switch (0) { 41 | case 0: return; 42 | } 43 | // We can't actually land here or we'd introduce UB, but the test confirms 44 | // we can write it and it compiles without warnings. 45 | sus::unreachable_unchecked(unsafe_fn); 46 | } 47 | 48 | } // namespace 49 | } // namespace sus 50 | -------------------------------------------------------------------------------- /sus/boxed/__private/string_error.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/error/error.h" 20 | #include "sus/mem/clone.h" 21 | 22 | namespace sus::boxed::__private { 23 | 24 | struct StringError { 25 | std::string s; 26 | }; 27 | 28 | } // namespace sus::boxed::__private 29 | 30 | template <> 31 | struct sus::error::ErrorImpl { 32 | using StringError = sus::boxed::__private::StringError; 33 | constexpr static std::string display(const StringError& e) noexcept { 34 | return sus::clone(e.s); 35 | } 36 | }; 37 | -------------------------------------------------------------------------------- /sus/boxed/boxed.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/boxed/.*" 17 | #pragma once 18 | 19 | namespace sus { 20 | 21 | /// The [`Box`]($sus::boxed::Box) type for heap allocation and other 22 | /// tools for [type-erasure of concepts]($sus::boxed::DynConcept). 23 | namespace boxed {} 24 | 25 | } // namespace sus 26 | -------------------------------------------------------------------------------- /sus/choice/__private/all_values_are_unique.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::choice_type::__private { 20 | 21 | template 22 | struct AllValuesAreUniqueHelper; 23 | 24 | template 25 | struct AllValuesAreUniqueHelper { 26 | // One value left, it's unique with itself. 27 | static constexpr bool value = true; 28 | }; 29 | 30 | template 31 | struct AllValuesAreUniqueHelper { 32 | // Two different values are unique. 33 | static constexpr bool value = !(V == V2); 34 | }; 35 | 36 | template 37 | requires(sizeof...(Vs) > 0) 38 | struct AllValuesAreUniqueHelper { 39 | // Two different values, now compare them to the rest. 40 | static constexpr bool value = !(V == V2) && 41 | AllValuesAreUniqueHelper::value && 42 | AllValuesAreUniqueHelper::value; 43 | }; 44 | 45 | template 46 | concept AllValuesAreUnique = AllValuesAreUniqueHelper::value; 47 | 48 | } // namespace sus::choice_type::__private 49 | -------------------------------------------------------------------------------- /sus/choice/__private/nothing.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | #include 21 | 22 | namespace sus::choice_type::__private { 23 | 24 | struct Nothing { 25 | friend constexpr bool operator==(const Nothing&, const Nothing&) noexcept { 26 | return true; 27 | } 28 | friend constexpr std::strong_ordering operator<=>(const Nothing&, const Nothing&) noexcept { 29 | return std::strong_ordering::equivalent; 30 | } 31 | }; 32 | 33 | template 34 | concept StorageIsVoid = std::same_as; 35 | 36 | /// Maps the storage type back to the public type, which means it maps `Nothing` 37 | /// back to `void`. 38 | template 39 | using PublicTypeForStorageType = 40 | std::conditional_t, void, StorageType>; 41 | 42 | } // namespace sus::choice_type::__private 43 | -------------------------------------------------------------------------------- /sus/choice/__private/pack_index.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::choice_type::__private { 20 | 21 | template 22 | struct PackFirstHelper; 23 | 24 | template 25 | struct PackFirstHelper { 26 | using type = T; 27 | }; 28 | 29 | template 30 | using PackFirst = PackFirstHelper::type; 31 | 32 | template 33 | struct PackIthHelper; 34 | 35 | template 36 | struct PackIthHelper { 37 | using type = PackIthHelper::type; 38 | }; 39 | 40 | template 41 | struct PackIthHelper<0, T, Ts...> { 42 | using type = T; 43 | }; 44 | 45 | template 46 | using PackIth = PackIthHelper::type; 47 | 48 | } // namespace sus::choice_type::__private 49 | -------------------------------------------------------------------------------- /sus/choice/__private/type_list.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::choice_type::__private { 20 | 21 | template 22 | struct TypeList {}; 23 | 24 | } // namespace sus::choice_type::__private 25 | -------------------------------------------------------------------------------- /sus/choice/choice_types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/choice/choice.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/choice/__private/storage.h" 20 | #include "sus/choice/__private/type_list.h" 21 | #include "sus/choice/macros.h" 22 | #include "sus/tuple/tuple.h" 23 | -------------------------------------------------------------------------------- /sus/cmp/__private/void_concepts.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "sus/cmp/eq.h" 22 | #include "sus/cmp/ord.h" 23 | 24 | namespace sus::cmp::__private { 25 | 26 | template 27 | concept VoidOrEq = 28 | (std::is_void_v && std::is_void_v) || ::sus::cmp::Eq; 29 | 30 | template 31 | concept VoidOrOrd = 32 | (std::is_void_v && std::is_void_v) || ::sus::cmp::StrongOrd; 33 | 34 | template 35 | concept VoidOrWeakOrd = 36 | (std::is_void_v && std::is_void_v) || ::sus::cmp::Ord; 37 | 38 | template 39 | concept VoidOrPartialOrd = 40 | (std::is_void_v && std::is_void_v) || ::sus::cmp::PartialOrd; 41 | 42 | } // namespace sus::cmp::__private 43 | -------------------------------------------------------------------------------- /sus/cmp/eq.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/cmp/cmp.h" // For docs. 20 | 21 | namespace sus::cmp { 22 | 23 | /// Concept for types that can be compared for equality with the `==` and `!=` 24 | /// operators. There is no guarantee of a full equivalence relation, and a 25 | /// partial equivalence is possible, which allows for values that compare 26 | /// not-equal to themselves (such as NaN). 27 | /// 28 | /// Implementations must ensure that `operator==` and `operator!=` are 29 | /// consistent with each other: 30 | /// 31 | /// * `a != b` if and only if `!(a == b)`. 32 | /// 33 | /// The default implementation of `operator!=` provides this consistency 34 | /// and is almost always sufficient. It should not be overridden without very 35 | /// good reason. 36 | /// 37 | /// This maps to the [`PartialEq`]( 38 | /// https://doc.rust-lang.org/stable/std/cmp/trait.PartialEq.html) trait in Rust 39 | /// rather than the `Eq` trait. Since C++ does not understand 40 | /// equivalent vs partial equivalence, we are unable to differentiate and 41 | /// provide a stronger relationship than partial equivalence. 42 | template 43 | concept Eq = requires(const std::remove_reference_t& lhs, 44 | const std::remove_reference_t& rhs) { 45 | { lhs == rhs } -> std::same_as; 46 | { lhs != rhs } -> std::same_as; 47 | }; 48 | 49 | } // namespace sus::cmp 50 | -------------------------------------------------------------------------------- /sus/cmp/eq_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/cmp/eq.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | 19 | namespace { 20 | 21 | using sus::cmp::Eq; 22 | 23 | struct CComp {}; 24 | struct C { 25 | friend bool operator==(const C&, const C&) noexcept { return true; } 26 | friend bool operator==(const C&, const CComp&) noexcept { return true; } 27 | }; 28 | 29 | struct E { 30 | // Not noexcept. 31 | friend bool operator==(const E&, const E&) { return true; } 32 | friend bool operator==(const E&, const CComp&) { return true; } 33 | }; 34 | 35 | // These types are comparable. 36 | static_assert(Eq); 37 | static_assert(Eq); 38 | static_assert(Eq); 39 | static_assert(Eq); 40 | static_assert(Eq); 41 | 42 | // Not noexcept. Allowed for compat with std. 43 | static_assert(Eq); 44 | static_assert(Eq); 45 | 46 | struct S {}; // No operator==. 47 | 48 | // These types are not comparable. 49 | static_assert(!Eq); 50 | 51 | } // namespace 52 | -------------------------------------------------------------------------------- /sus/cmp/reverse_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/cmp/reverse.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/prelude.h" 19 | 20 | namespace { 21 | using namespace sus::cmp; 22 | 23 | TEST(Reverse, Example_ReverseKey) { 24 | using sus::cmp::Reverse; 25 | 26 | auto v = sus::Vec(1, 2, 3, 4, 5, 6); 27 | v.sort_by_key([](i32 num) { return sus::Tuple(num > 3, Reverse(num)); }); 28 | sus_check(v == sus::Vec(3, 2, 1, 6, 5, 4)); 29 | } 30 | 31 | } // namespace 32 | -------------------------------------------------------------------------------- /sus/collections/__private/slice_methods_impl.inc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | 18 | /////////////////////////////////////////////////////////////////////////// 19 | // 20 | // Out-of-line definitions for methods defined in `slice_methods.inc` that 21 | // provide const access or no access to the contained objects. These methods are 22 | // suitable to be exposed from a Slice for const access. 23 | // 24 | // TO USE THIS INC FILE: 25 | // 26 | // Include it after the body of your class. 27 | // 28 | // Define `_ptr_expr`, `_len_expr` `_iter_refs_expr` and `_iter_refs_view_expr` 29 | // when including this file to the expressions of getting the slice's data 30 | // pointer and length. 31 | // 32 | // Define `_self` as the type declaring the methods including template 33 | // parameters, such as `S`. 34 | // 35 | // Define `_self_template` as the template parameter declarations for the type, 36 | // such as `class A, class B`. 37 | /////////////////////////////////////////////////////////////////////////// 38 | 39 | template <_self_template> 40 | Vec _self::to_vec() const& noexcept 41 | requires(::sus::mem::Clone) 42 | { 43 | auto v = Vec::with_capacity(_len_expr); 44 | for (::sus::usize i; i < _len_expr; i += 1u) { 45 | v.push(::sus::clone(*(_ptr_expr + i))); 46 | } 47 | return v; 48 | } 49 | 50 | #undef _self 51 | #undef _self_template 52 | #undef _ptr_expr 53 | #undef _len_expr 54 | #undef _delete_rvalue 55 | #undef _iter_refs_expr 56 | #undef _iter_refs_view_expr 57 | -------------------------------------------------------------------------------- /sus/collections/compat_deque.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/from_iterator.h" 20 | #include "sus/iter/iterator.h" 21 | 22 | template 23 | struct sus::iter::FromIteratorImpl> { 24 | static constexpr std::deque from_iter( 25 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 26 | requires(sus::mem::IsMoveRef) 27 | { 28 | auto&& iter = sus::move(into_iter).into_iter(); 29 | auto [lower, upper] = iter.size_hint(); 30 | auto v = std::deque(); 31 | for (T&& t : iter) v.push_back(::sus::move(t)); 32 | return v; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /sus/collections/compat_deque_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/collections/compat_deque.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/iter/compat_ranges.h" 19 | #include "sus/prelude.h" 20 | 21 | namespace { 22 | 23 | // For any `usize`. 24 | static_assert(sus::iter::FromIterator, usize>); 25 | 26 | TEST(CompatDeque, FromIterator) { 27 | auto in = std::deque{1, 2, 3, 4, 5, 6, 7}; 28 | auto out = sus::iter::from_range(in) 29 | .moved(unsafe_fn) 30 | .filter([](const i32& i) { return i % 2 == 0; }) 31 | .collect>(); 32 | sus_check(out == std::deque{2, 4, 6}); 33 | } 34 | 35 | } // namespace 36 | -------------------------------------------------------------------------------- /sus/collections/compat_forward_list.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/from_iterator.h" 20 | #include "sus/iter/iterator.h" 21 | 22 | template 23 | struct sus::iter::FromIteratorImpl> { 24 | static constexpr std::forward_list from_iter( 25 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 26 | requires(sus::mem::IsMoveRef) 27 | { 28 | auto&& iter = sus::move(into_iter).into_iter(); 29 | auto [lower, upper] = iter.size_hint(); 30 | auto v = std::forward_list(); 31 | constexpr bool is_double_end = 32 | sus::iter::DoubleEndedIterator, T>; 33 | if constexpr (is_double_end) { 34 | for (T&& t : sus::move(iter).rev()) v.push_front(::sus::move(t)); 35 | } else { 36 | for (T&& t : sus::move(iter)) v.push_front(::sus::move(t)); 37 | v.reverse(); 38 | } 39 | return v; 40 | } 41 | }; 42 | -------------------------------------------------------------------------------- /sus/collections/compat_list.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/from_iterator.h" 20 | #include "sus/iter/iterator.h" 21 | 22 | template 23 | struct sus::iter::FromIteratorImpl> { 24 | static constexpr std::list from_iter( 25 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 26 | requires(sus::mem::IsMoveRef) 27 | { 28 | auto&& iter = sus::move(into_iter).into_iter(); 29 | auto [lower, upper] = iter.size_hint(); 30 | auto v = std::list(); 31 | for (T&& t : iter) v.push_back(::sus::move(t)); 32 | return v; 33 | } 34 | }; 35 | -------------------------------------------------------------------------------- /sus/collections/compat_list_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/collections/compat_list.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/iter/compat_ranges.h" 19 | #include "sus/prelude.h" 20 | 21 | namespace { 22 | 23 | // For any `usize`. 24 | static_assert(sus::iter::FromIterator, usize>); 25 | 26 | TEST(CompatList, FromIterator) { 27 | auto in = std::list{1, 2, 3, 4, 5, 6, 7}; 28 | auto out = sus::iter::from_range(in) 29 | .moved(unsafe_fn) 30 | .filter([](const i32& i) { return i % 2 == 0; }) 31 | .collect>(); 32 | sus_check(out == std::list{2, 4, 6}); 33 | } 34 | 35 | } // namespace 36 | -------------------------------------------------------------------------------- /sus/collections/compat_pair_concept.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/mem/move.h" 20 | 21 | namespace sus::collections::compat { 22 | 23 | template 24 | concept Pair = requires(P&& p) { 25 | { get<0>(::sus::move(p)) } -> std::convertible_to; 26 | { get<1>(::sus::move(p)) } -> std::convertible_to; 27 | }; 28 | 29 | } 30 | -------------------------------------------------------------------------------- /sus/collections/compat_queue.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/from_iterator.h" 20 | #include "sus/iter/iterator.h" 21 | 22 | template 23 | struct sus::iter::FromIteratorImpl> { 24 | static constexpr std::queue from_iter( 25 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 26 | requires(sus::mem::IsMoveRef) 27 | { 28 | auto&& iter = sus::move(into_iter).into_iter(); 29 | auto v = std::queue(); 30 | for (T&& t : iter) v.push(::sus::move(t)); 31 | return v; 32 | } 33 | }; 34 | 35 | template 36 | struct sus::iter::FromIteratorImpl< 37 | std::priority_queue> { 38 | static constexpr std::priority_queue from_iter( 39 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 40 | requires(sus::mem::IsMoveRef) 41 | { 42 | auto&& iter = sus::move(into_iter).into_iter(); 43 | auto v = std::priority_queue(); 44 | for (T&& t : iter) v.push(::sus::move(t)); 45 | return v; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /sus/collections/compat_set.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/compat_ranges.h" 20 | #include "sus/iter/from_iterator.h" 21 | #include "sus/iter/iterator.h" 22 | 23 | template 24 | struct sus::iter::FromIteratorImpl> { 25 | static constexpr std::set from_iter( 26 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 27 | requires(sus::mem::IsMoveRef) 28 | { 29 | auto&& iter = sus::move(into_iter).into_iter(); 30 | auto s = std::set(); 31 | for (Key k : iter) s.insert(k); 32 | return s; 33 | } 34 | }; 35 | 36 | template 37 | struct sus::iter::FromIteratorImpl> { 38 | static constexpr std::multiset from_iter( 39 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 40 | requires(sus::mem::IsMoveRef) 41 | { 42 | auto&& iter = sus::move(into_iter).into_iter(); 43 | auto s = std::multiset(); 44 | for (Key k : iter) s.insert(k); 45 | return s; 46 | } 47 | }; 48 | -------------------------------------------------------------------------------- /sus/collections/compat_set_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/collections/compat_set.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/iter/compat_ranges.h" 19 | #include "sus/option/option.h" 20 | #include "sus/prelude.h" 21 | 22 | namespace { 23 | 24 | // For any `usize`. 25 | static_assert(sus::iter::FromIterator, usize>); 26 | 27 | TEST(CompatSet, FromIterator) { 28 | auto in = std::vector{3, 4, 2, 7, 6, 1, 5}; 29 | auto out = sus::iter::from_range(in) 30 | .moved(unsafe_fn) 31 | .filter([](const i32& i) { return i % 2 == 0; }) 32 | .collect>(); 33 | static_assert(std::same_as>); 34 | sus_check(out == std::set{2, 4, 6}); 35 | } 36 | 37 | TEST(CompaMultiSet, FromIterator) { 38 | auto in = std::vector{3, 4, 2, 7, 6, 2, 1, 5, 2}; 39 | auto out = sus::iter::from_range(in) 40 | .moved(unsafe_fn) 41 | .filter([](const i32& i) { return i % 2 == 0; }) 42 | .collect>(); 43 | static_assert(std::same_as>); 44 | sus_check(out == std::multiset{2, 2, 2, 4, 6}); 45 | } 46 | 47 | } // namespace 48 | -------------------------------------------------------------------------------- /sus/collections/compat_stack.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/from_iterator.h" 20 | #include "sus/iter/iterator.h" 21 | 22 | template 23 | struct sus::iter::FromIteratorImpl> { 24 | static constexpr std::stack from_iter( 25 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 26 | requires(sus::mem::IsMoveRef) 27 | { 28 | auto&& iter = sus::move(into_iter).into_iter(); 29 | auto v = std::stack(); 30 | for (T&& t : iter) v.push(::sus::move(t)); 31 | return v; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /sus/collections/compat_stack_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/collections/compat_stack.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/iter/compat_ranges.h" 19 | #include "sus/prelude.h" 20 | 21 | namespace { 22 | 23 | // For any `usize`. 24 | static_assert(sus::iter::FromIterator, usize>); 25 | 26 | TEST(CompatStack, FromIterator) { 27 | auto in = std::vector{1, 2, 3, 4, 5, 6, 7}; 28 | auto out = sus::iter::from_range(in) 29 | .moved(unsafe_fn) 30 | .filter([](const i32& i) { return i % 2 == 0; }) 31 | .collect>(); 32 | static_assert(std::same_as>); 33 | sus_check(out == std::stack(std::deque{2, 4, 6})); 34 | } 35 | 36 | TEST(CompatStack, FromIteratorRef) { 37 | auto in = std::vector{1, 2, 3, 4, 5, 6, 7}; 38 | auto out = sus::iter::from_range(in) 39 | .moved(unsafe_fn) 40 | .filter([](const i32& i) { return i % 2 == 0; }) 41 | .rev() 42 | .collect>(); 43 | static_assert(std::same_as>); 44 | sus_check(out == std::stack(std::deque{6, 4, 2})); 45 | } 46 | 47 | } // namespace 48 | -------------------------------------------------------------------------------- /sus/collections/compat_unordered_set.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/compat_ranges.h" 20 | #include "sus/iter/from_iterator.h" 21 | #include "sus/iter/iterator.h" 22 | 23 | template 24 | struct sus::iter::FromIteratorImpl< 25 | std::unordered_set> { 26 | static constexpr std::unordered_set from_iter( 27 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 28 | requires(sus::mem::IsMoveRef) 29 | { 30 | auto&& iter = sus::move(into_iter).into_iter(); 31 | auto s = std::unordered_set(); 32 | for (Key k : iter) s.insert(k); 33 | return s; 34 | } 35 | }; 36 | 37 | template 38 | struct sus::iter::FromIteratorImpl< 39 | std::unordered_multiset> { 40 | static constexpr std::unordered_multiset 41 | from_iter(::sus::iter::IntoIterator auto&& into_iter) noexcept 42 | requires(sus::mem::IsMoveRef) 43 | { 44 | auto&& iter = sus::move(into_iter).into_iter(); 45 | auto s = std::unordered_multiset(); 46 | for (Key k : iter) s.insert(k); 47 | return s; 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /sus/collections/compat_vector.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/from_iterator.h" 20 | #include "sus/iter/iterator.h" 21 | 22 | template 23 | struct sus::iter::FromIteratorImpl> { 24 | static constexpr std::vector from_iter( 25 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 26 | requires(sus::mem::IsMoveRef) 27 | { 28 | auto&& iter = sus::move(into_iter).into_iter(); 29 | // TODO: In C++23, use the `std::from_range` ctor to speed up construction 30 | // here. Then it should match `sus::Vec` on the BenchVecMap benchmark with 31 | // `FromIterator`. 32 | auto v = std::vector(); 33 | v.reserve(iter.size_hint().lower); 34 | for (T&& t : iter) v.push_back(::sus::move(t)); 35 | return v; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /sus/collections/compat_vector_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/collections/compat_vector.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/iter/compat_ranges.h" 19 | #include "sus/prelude.h" 20 | 21 | namespace { 22 | 23 | // For any `usize`. 24 | static_assert(sus::iter::FromIterator, usize>); 25 | 26 | TEST(CompatVector, FromIterator) { 27 | auto in = std::vector{1, 2, 3, 4, 5, 6, 7}; 28 | auto out = sus::iter::from_range(in) 29 | .moved(unsafe_fn) 30 | .filter([](const i32& i) { return i % 2 == 0; }) 31 | .collect>(); 32 | sus_check(out == std::vector{2, 4, 6}); 33 | } 34 | 35 | } // namespace 36 | -------------------------------------------------------------------------------- /sus/collections/concat.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/mem/clone.h" 18 | #include "sus/mem/move.h" 19 | #include "sus/lib/__private/forward_decl.h" 20 | 21 | namespace sus::collections { 22 | 23 | /// Types that support being flattened and concatenated together into a 24 | /// collection. 25 | /// 26 | /// For example, `Slice` is `Concat`, which allows a `Slice>` to be 27 | /// concatenated into a `Slice` of all the original elements. This concept 28 | /// requires `T` to provide `concat_into(T::ConcatOutputType&)` that does the 29 | /// concatenation. 30 | /// 31 | /// TODO: String should satisfy Concat as well. 32 | template 33 | concept Concat = requires(const T& t, const ::sus::num::usize& cap) { 34 | // Concat between types of T must report their lengths. 35 | { t.len() } -> std::same_as<::sus::num::usize>; 36 | // The Concat between types of T produces this type. 37 | typename T::ConcatOutputType; 38 | // The output type can be constructed with a capacity. 39 | { 40 | T::ConcatOutputType::with_capacity(cap) 41 | } -> std::same_as; 42 | // The output type can be extended by each object of type T. 43 | requires requires(typename T::ConcatOutputType & collection) { 44 | { t.concat_into(collection) } -> std::same_as; 45 | }; 46 | }; 47 | 48 | } // namespace sus::collections 49 | -------------------------------------------------------------------------------- /sus/collections/invalidation_off_size_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #define SUS_ITERATOR_INVALIDATION 0 16 | 17 | #include "sus/collections/array.h" 18 | #include "sus/collections/slice.h" 19 | #include "sus/macros/compiler.h" 20 | #include "sus/prelude.h" 21 | 22 | using sus::Array; 23 | using sus::Slice; 24 | 25 | constexpr usize array_padding = 0u 26 | // No support for no_unique_address in clang-cl. 27 | sus_clang_bug_49358(+sizeof(i32)); 28 | 29 | static_assert(alignof(Array) == alignof(i32)); 30 | static_assert(sizeof(Array) == sizeof(i32) * 5 + array_padding); 31 | 32 | constexpr usize slice_padding = 0u 33 | // No support for no_unique_address in clang-cl. 34 | sus_clang_bug_49358(+sizeof(i32*)); 35 | 36 | static_assert(alignof(Slice) == alignof(i32*)); 37 | static_assert(sizeof(Slice) == 38 | sizeof(i32*) + sizeof(usize) + slice_padding); 39 | -------------------------------------------------------------------------------- /sus/collections/invalidation_on_size_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #define SUS_ITERATOR_INVALIDATION 1 16 | 17 | #include "sus/collections/array.h" 18 | #include "sus/collections/slice.h" 19 | #include "sus/prelude.h" 20 | 21 | using sus::Array; 22 | using sus::Slice; 23 | 24 | namespace { 25 | 26 | constexpr usize round_up(usize n, usize multiple) { 27 | if (n % multiple == 0u) return n; 28 | return n + multiple - ((n + multiple) % multiple); 29 | } 30 | 31 | } 32 | 33 | // The Array has a ref-count for iterator invalidation. So it's size is 5 * 34 | // sizeof(i32) = 20 + sizeof(pointer) + padding up to a multiple of 35 | // sizeof(pointer). 36 | static_assert(alignof(Array) == alignof(usize*)); 37 | static_assert(sizeof(Array) == 38 | round_up(sizeof(i32) * 5 + sizeof(usize*), alignof(usize*))); 39 | 40 | static_assert(alignof(Slice) == alignof(usize*)); 41 | static_assert(sizeof(Slice) == 42 | round_up(sizeof(i32*) + sizeof(usize) + sizeof(usize*), 43 | alignof(usize*))); 44 | -------------------------------------------------------------------------------- /sus/construct/__private/into_ref.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "sus/construct/from.h" 22 | #include "sus/mem/forward.h" 23 | 24 | namespace sus::construct ::__private { 25 | 26 | template 27 | struct IntoRef final { 28 | [[nodiscard]] constexpr IntoRef(FromType&& from) noexcept 29 | : from_(::sus::forward(from)) {} 30 | 31 | template > ToType> 32 | constexpr operator ToType() && noexcept { 33 | return static_cast(from_); 34 | } 35 | 36 | template <::sus::construct::From ToType> 37 | requires(!std::same_as, ToType>) 38 | constexpr operator ToType() && noexcept { 39 | return ToType::from(::sus::forward(from_)); 40 | } 41 | 42 | // Doesn't copy or move. `IntoRef` should only be used as a temporary. 43 | IntoRef(const IntoRef&) { 44 | static_assert(std::same_as, 45 | "into() must immediately be converted to a type by forcing a " 46 | "conversion, it can not be stored"); 47 | } 48 | IntoRef& operator=(const IntoRef&) = delete; 49 | 50 | private: 51 | FromType&& from_; 52 | }; 53 | 54 | } // namespace sus::construct::__private 55 | -------------------------------------------------------------------------------- /sus/construct/cast_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/construct/cast.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/prelude.h" 19 | 20 | namespace { 21 | 22 | TEST(Cast, Example_Concept) { 23 | auto add = [](u32 a, const sus::construct::Cast auto& b) -> u32 { 24 | return a.wrapping_add(sus::cast(b)); 25 | }; 26 | sus_check(add(3_u32, -1_i32) == u32::MIN + 2u); 27 | } 28 | 29 | TEST(Cast, Example_Function) { 30 | sus_check(u32::MAX == sus::cast(-1_i64)); 31 | } 32 | 33 | } // namespace 34 | -------------------------------------------------------------------------------- /sus/construct/default.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | namespace sus::construct { 20 | 21 | /// Default types are able to be constructed with a default value. 22 | /// 23 | /// A type `T` satisfies Default if it has a default constructor. 24 | // 25 | // clang-format off 26 | template 27 | concept Default = std::constructible_from; 28 | // clang-format on 29 | 30 | } // namespace sus::construct 31 | -------------------------------------------------------------------------------- /sus/construct/default_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/construct/default.h" 16 | 17 | using sus::construct::Default; 18 | 19 | namespace { 20 | 21 | struct DefaultConstructible { 22 | const int i = 2; 23 | }; 24 | struct NotDefaultConstructible { 25 | constexpr NotDefaultConstructible(int i) noexcept : i(i) {} 26 | const int i = 2; 27 | }; 28 | 29 | static_assert(Default == true); 30 | static_assert(Default == false); 31 | static_assert(Default == false); 32 | 33 | } // namespace 34 | -------------------------------------------------------------------------------- /sus/construct/from_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/construct/from.h" 16 | 17 | #include "sus/construct/into.h" 18 | #include "sus/result/result.h" 19 | 20 | using sus::construct::From; 21 | using sus::construct::TryFrom; 22 | 23 | namespace { 24 | 25 | struct S {}; 26 | 27 | struct WithFromInt { 28 | static auto from(int) { return WithFromInt(); } 29 | }; 30 | struct NotFromInt {}; 31 | struct WithFromS { 32 | static auto from(S) { return WithFromS(); } 33 | }; 34 | struct FromReturnsVoid { 35 | static auto from(int) {} 36 | }; 37 | struct FromReturnsElse { 38 | static auto from(int i) { return i; } 39 | }; 40 | 41 | static_assert(From); 42 | // TODO: Can we prevent type conversion? 43 | static_assert(From); 44 | static_assert(!From); 45 | static_assert(!From); 46 | static_assert(!From); 47 | static_assert(!From); 48 | 49 | enum Error {}; 50 | 51 | struct TryYes { 52 | static sus::Result try_from(S) { return sus::ok(TryYes()); } 53 | }; 54 | struct TryNoResult { 55 | static TryYes try_from(S) { return TryYes(); } 56 | }; 57 | struct TryWrongResult { 58 | static sus::Result try_from(S) { return sus::ok(S()); } 59 | }; 60 | struct TryVoidResult { 61 | static sus::Result try_from(S) { return sus::ok(); } 62 | }; 63 | 64 | static_assert(TryFrom); 65 | static_assert(!TryFrom); 66 | static_assert(!TryFrom); 67 | static_assert(!TryFrom); 68 | 69 | } // namespace 70 | -------------------------------------------------------------------------------- /sus/env/env.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | namespace sus { 18 | 19 | /// Inspection and manipulation of the process's environment. 20 | /// 21 | /// Currently this namespace contains functions to inspect and manipulate 22 | /// environment variables. 23 | /// 24 | /// TODO: This namespace contains functions to inspect or manipulate various 25 | /// aspects such as environment variables, process arguments, the current 26 | /// directory, and various other important directories. 27 | namespace env {} 28 | 29 | } // namespace sus 30 | -------------------------------------------------------------------------------- /sus/env/var_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/env/var.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | 19 | namespace { 20 | 21 | TEST(EnvVar, Present) { 22 | const char VAR_NAME[] = "subspace_test_envvar_present_42389489423423"; 23 | const char VAR_VALUE[] = "HelLo wOrLd"; 24 | sus::env::set_var(VAR_NAME, VAR_VALUE); 25 | auto value = sus::env::var(VAR_NAME).unwrap(); 26 | EXPECT_EQ(value, VAR_VALUE); 27 | } 28 | 29 | TEST(EnvVar, Absent) { 30 | const char VAR_NAME[] = "subspace_test_envvar_present_42389489423424"; 31 | auto value = sus::env::var(VAR_NAME); 32 | EXPECT_EQ(value, sus::err(sus::env::VarError::NotFound)); 33 | } 34 | 35 | } // namespace 36 | -------------------------------------------------------------------------------- /sus/iter/__private/into_iterator_archetype.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/lib/__private/forward_decl.h" 20 | #include "sus/iter/into_iterator.h" 21 | 22 | namespace sus::iter::__private { 23 | 24 | template 25 | struct IntoIteratorArchetype { 26 | template 27 | struct Iter final : public IteratorBase, Item> { 28 | ::sus::option::Option next() noexcept; 29 | SizeHint size_hint() const noexcept; 30 | }; 31 | Iter into_iter() && { return Iter(); } 32 | }; 33 | 34 | } // namespace sus::iter::__private 35 | -------------------------------------------------------------------------------- /sus/iter/__private/is_generator.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "sus/lib/__private/forward_decl.h" 22 | 23 | namespace sus::iter::__private { 24 | 25 | template 26 | struct IsGenerator; 27 | 28 | template 29 | struct IsGenerator final : std::false_type { 30 | using type = void; 31 | }; 32 | template 33 | struct IsGenerator> final : std::true_type { 34 | using type = T; 35 | }; 36 | 37 | } // namespace sus::iter::__private 38 | -------------------------------------------------------------------------------- /sus/iter/__private/iterator_archetype.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/lib/__private/forward_decl.h" 20 | #include "sus/iter/iterator_concept.h" 21 | 22 | namespace sus::iter::__private { 23 | 24 | template 25 | struct IteratorArchetype final 26 | : public ::sus::iter::IteratorBase, Item> { 27 | ::sus::option::Option next() noexcept; 28 | SizeHint size_hint() const noexcept; 29 | }; 30 | 31 | } // namespace sus::iter::__private 32 | -------------------------------------------------------------------------------- /sus/iter/__private/iterator_end.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::iter::__private { 20 | 21 | struct IteratorEnd {}; 22 | 23 | } // namespace sus::iter::__private 24 | -------------------------------------------------------------------------------- /sus/iter/empty_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/iter/empty.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/prelude.h" 19 | 20 | namespace { 21 | 22 | TEST(Empty, Example) { 23 | auto empty = sus::iter::empty(); 24 | sus_check(empty.next().is_none()); 25 | } 26 | 27 | } // namespace 28 | -------------------------------------------------------------------------------- /sus/iter/extend.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/iter/__private/into_iterator_archetype.h" 18 | #include "sus/mem/move.h" 19 | 20 | namespace sus::iter { 21 | 22 | /// Extend a collection with the contents of an iterator. 23 | /// 24 | /// Iterators produce a series of values, and collections can also be thought of 25 | /// as a series of values. The `Extend` concept bridges this gap, allowing you to 26 | /// extend a collection by including the contents of that iterator. When 27 | /// extending a collection with an already existing key, that entry is updated 28 | /// or, in the case of collections that permit multiple entries with equal keys, 29 | /// that entry is inserted. 30 | template 31 | concept Extend = 32 | requires(Collection c, __private::IntoIteratorArchetype&& iter) { 33 | { c.extend(::sus::move(iter)) } -> std::same_as; 34 | }; 35 | 36 | } // namespace sus::iter 37 | -------------------------------------------------------------------------------- /sus/iter/iterator_impl.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/iter/iterator.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "sus/iter/try_from_iterator.h" 22 | 23 | namespace sus::iter { 24 | 25 | // Implementation of IteratorBase::try_collect has to live here to avoid 26 | // cyclical includes, as the impl of try_from_iter() needs to see IteratorBase 27 | // in order to make something that can be consumed by FromIterator. 28 | template 29 | template 30 | requires(::sus::ops::Try && // 31 | FromIterator> && 32 | // Void can not be collected from. 33 | !std::is_void_v<::sus::ops::TryOutputType>) 34 | constexpr auto IteratorBase::try_collect() noexcept { 35 | return ::sus::iter::try_from_iter(static_cast(*this)); 36 | } 37 | 38 | } // namespace sus::iter 39 | -------------------------------------------------------------------------------- /sus/iter/once_with_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/iter/once_with.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/prelude.h" 19 | 20 | namespace { 21 | 22 | TEST(OnceWith, Example) { 23 | auto ow = sus::iter::once_with([]() { return 3_u16; }); 24 | sus_check(ow.next().unwrap() == 3_u16); 25 | } 26 | 27 | TEST(OnceWith, Next) { 28 | auto ow = sus::iter::once_with([]() { return 3_u16; }); 29 | EXPECT_EQ(ow.size_hint(), sus::iter::SizeHint(1u, sus::some(1u))); 30 | EXPECT_EQ(ow.exact_size_hint(), 1u); 31 | EXPECT_EQ(ow.next(), sus::some(3_u16)); 32 | EXPECT_EQ(ow.size_hint(), sus::iter::SizeHint(0u, sus::some(0u))); 33 | EXPECT_EQ(ow.exact_size_hint(), 0u); 34 | EXPECT_EQ(ow.next(), sus::none()); 35 | } 36 | 37 | TEST(OnceWith, NextBack) { 38 | auto ow = sus::iter::once_with([]() { return 3_u16; }); 39 | EXPECT_EQ(ow.size_hint(), sus::iter::SizeHint(1u, sus::some(1u))); 40 | EXPECT_EQ(ow.exact_size_hint(), 1u); 41 | EXPECT_EQ(ow.next_back(), sus::some(3_u16)); 42 | EXPECT_EQ(ow.size_hint(), sus::iter::SizeHint(0u, sus::some(0u))); 43 | EXPECT_EQ(ow.exact_size_hint(), 0u); 44 | EXPECT_EQ(ow.next_back(), sus::none()); 45 | } 46 | 47 | // constexpr, and verifies that the return type can be converted to the Item 48 | // type. 49 | static_assert(sus::iter::once_with([] { return 5; }).sum() == 5); 50 | 51 | } // namespace 52 | -------------------------------------------------------------------------------- /sus/iter/product.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/iter/__private/iterator_archetype.h" 18 | #include "sus/iter/iterator_concept.h" 19 | #include "sus/mem/move.h" 20 | 21 | namespace sus::iter { 22 | 23 | /// Trait to represent types that can be created by multiplying elements of an 24 | /// iterator. 25 | /// 26 | /// Types which implement this trait can be generated by using the product() 27 | /// method on an iterator. Like with `FromIterator`, the `from_product()` method 28 | /// should rarely be called directly. 29 | /// 30 | /// When given an empty iterator, the result of `from_product()` should be the 31 | /// "one" value of the type. 32 | template 33 | concept Product = requires(__private::IteratorArchetype&& iter) { 34 | // Use cloned() or copied() to convert an Iterator of references to values. 35 | requires(!std::is_reference_v); 36 | { T::from_product(::sus::move(iter)) } -> std::same_as; 37 | }; 38 | 39 | } // namespace sus::iter 40 | -------------------------------------------------------------------------------- /sus/iter/repeat_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/iter/repeat.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/prelude.h" 19 | 20 | namespace { 21 | 22 | TEST(Repeat, Example) { 23 | auto r = sus::iter::repeat(3_u16); 24 | sus_check(r.next().unwrap() == 3_u16); 25 | sus_check(r.next().unwrap() == 3_u16); 26 | sus_check(r.next().unwrap() == 3_u16); 27 | } 28 | 29 | TEST(Repeat, Next) { 30 | auto o = sus::iter::repeat(3_u16); 31 | EXPECT_EQ(o.size_hint(), sus::iter::SizeHint(usize::MAX, sus::none())); 32 | EXPECT_EQ(o.next(), sus::some(3_u16)); 33 | EXPECT_EQ(o.size_hint(), sus::iter::SizeHint(usize::MAX, sus::none())); 34 | EXPECT_EQ(o.next(), sus::some(3_u16)); 35 | EXPECT_EQ(o.size_hint(), sus::iter::SizeHint(usize::MAX, sus::none())); 36 | EXPECT_EQ(o.next(), sus::some(3_u16)); 37 | } 38 | 39 | TEST(Repeat, NextBack) { 40 | auto o = sus::iter::repeat(3_u16); 41 | EXPECT_EQ(o.size_hint(), sus::iter::SizeHint(usize::MAX, sus::none())); 42 | EXPECT_EQ(o.next_back(), sus::some(3_u16)); 43 | EXPECT_EQ(o.size_hint(), sus::iter::SizeHint(usize::MAX, sus::none())); 44 | EXPECT_EQ(o.next_back(), sus::some(3_u16)); 45 | EXPECT_EQ(o.size_hint(), sus::iter::SizeHint(usize::MAX, sus::none())); 46 | EXPECT_EQ(o.next_back(), sus::some(3_u16)); 47 | } 48 | 49 | } // namespace 50 | -------------------------------------------------------------------------------- /sus/iter/size_hint.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/iter/iterator.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/num/unsigned_integer.h" 20 | #include "sus/option/option.h" 21 | #include "sus/string/__private/format_to_stream.h" 22 | 23 | namespace sus::iter { 24 | 25 | struct SizeHint { 26 | ::sus::num::usize lower; 27 | ::sus::option::Option<::sus::num::usize> upper; 28 | 29 | friend constexpr bool operator==(const SizeHint& lhs, 30 | const SizeHint& rhs) noexcept = default; 31 | 32 | // Stream support. 33 | _sus_format_to_stream(SizeHint); 34 | }; 35 | 36 | } // namespace sus::iter 37 | 38 | // fmt support. 39 | template 40 | struct fmt::formatter<::sus::iter::SizeHint, Char> { 41 | template 42 | constexpr auto parse(ParseContext& ctx) { 43 | return ctx.begin(); 44 | } 45 | 46 | template 47 | FormatContext::iterator format(const ::sus::iter::SizeHint& t, 48 | FormatContext& ctx) const; 49 | }; 50 | -------------------------------------------------------------------------------- /sus/iter/size_hint_impl.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/iter/iterator.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "fmt/format.h" 20 | #include "sus/iter/size_hint.h" 21 | #include "sus/num/unsigned_integer_impl.h" // For formatting usize. 22 | #include "sus/option/option.h" // For formatting option. 23 | #include "sus/string/__private/format_to_stream.h" 24 | 25 | // fmt support. 26 | template 27 | template 28 | FormatContext::iterator fmt::formatter<::sus::iter::SizeHint, Char>::format( 29 | const ::sus::iter::SizeHint& t, FormatContext& ctx) const { 30 | return fmt::format_to(ctx.out(), "SizeHint({}, {})", t.lower, t.upper); 31 | } 32 | -------------------------------------------------------------------------------- /sus/iter/sum.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/iter/__private/iterator_archetype.h" 18 | #include "sus/iter/iterator_concept.h" 19 | #include "sus/mem/move.h" 20 | 21 | namespace sus::iter { 22 | 23 | /// Trait to represent types that can be created by adding elements of an 24 | /// iterator. 25 | /// 26 | /// Types which implement this trait can be generated by using the sum() method 27 | /// on an iterator. Like with `FromIterator`, the `from_sum()` method should 28 | /// rarely be called directly. 29 | /// 30 | /// When given an empty iterator, the result of `from_sum()` should be the 31 | /// "zero" value of the type. 32 | template 33 | concept Sum = requires(__private::IteratorArchetype&& iter) { 34 | // Use cloned() or copied() to convert an Iterator of references to values. 35 | requires(!std::is_reference_v); 36 | { T::from_sum(::sus::move(iter)) } -> std::same_as; 37 | }; 38 | 39 | } // namespace sus::iter 40 | -------------------------------------------------------------------------------- /sus/iter/zip.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/iter/iterator.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/iter/into_iterator.h" 20 | #include "sus/iter/iterator_defn.h" 21 | #include "sus/mem/move.h" 22 | 23 | namespace sus::iter { 24 | 25 | /// Converts the arguments to iterators and zips them. 26 | /// 27 | /// See the documentation of [`Iterator::zip]`(sus::iter::IteratorBase::zip) for 28 | /// more. 29 | /// 30 | /// # Example 31 | /// ``` 32 | /// auto a = sus::Array(2, 3); 33 | /// auto b = sus::Array(3.f, 4.f, 5.f, 6.f, 7.f); 34 | /// auto it = sus::iter::zip(::sus::move(a), ::sus::move(b)); 35 | /// sus_check(it.next() == sus::some(sus::tuple(2, 3.f))); 36 | /// sus_check(it.next() == sus::some(sus::tuple(3, 4.f))); 37 | /// sus_check(it.next() == sus::none()); 38 | /// ``` 39 | inline constexpr auto zip(IntoIteratorAny auto&& iia, 40 | IntoIteratorAny auto&& iib) noexcept 41 | -> Iterator< 42 | ::sus::Tuple::Item, 43 | typename IntoIteratorOutputType::Item>> auto 44 | requires(::sus::mem::IsMoveRef && 45 | ::sus::mem::IsMoveRef) 46 | { 47 | return ::sus::move(iia).into_iter().zip(::sus::move(iib)); 48 | } 49 | 50 | } // namespace sus::iter 51 | -------------------------------------------------------------------------------- /sus/lib/lib.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/lib/lib.h" 16 | 17 | #include 18 | #include 19 | 20 | #include "sus/mem/size_of.h" 21 | #include "sus/num/signed_integer.h" 22 | #include "sus/num/signed_integer_impl.h" 23 | 24 | // Architectural assumptions we make throughout the implementation of Subspace. 25 | static_assert(CHAR_BIT == 8); 26 | // Signed integers are allowed to have padding so they can have a larger size 27 | // thus we don't compare the size of `int` but its max value instead. 28 | static_assert(INT_MAX == INT32_MAX); 29 | static_assert(sus::mem::size_of() >= 4); 30 | static_assert(sus::mem::size_of() <= 8); 31 | 32 | // TODO: Consider if we should only support little endian? We probably make this 33 | // assumption. Support for endian *conversion* is still important for network 34 | // byte order etc. 35 | 36 | // The Vec class, along with any other class with pointer arithmetic, assumes 37 | // isize::MAX == PTRDIFF_MAX. 38 | static_assert(sus::isize::MAX == PTRDIFF_MAX); 39 | 40 | #if defined(__clang__) 41 | // Require clang 16 to avoid bugs and for required features. 42 | static_assert(__clang_major__ >= 16, "requires at least clang 16"); 43 | #endif 44 | -------------------------------------------------------------------------------- /sus/lib/lib.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | /// The root namespace of the Subspace C++ library. 18 | /// 19 | /// Everything in the library lives in nested namespaces, for the most part, but 20 | /// some common things are promoted up to the `sus` namespace as aliases. 21 | namespace sus {} 22 | -------------------------------------------------------------------------------- /sus/macros/arch.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #if defined(__x86_64__) || defined(_M_X64) 18 | #define sus_is_64bit() true // x86_64 19 | #elif defined(__aarch64__) || defined(_M_ARM64) 20 | #define sus_is_64bit() true // aarch64 21 | #else 22 | #define sus_is_64bit() false 23 | #endif 24 | -------------------------------------------------------------------------------- /sus/macros/assume.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/compiler.h" 18 | #include "sus/marker/unsafe.h" 19 | 20 | /// Tells the compiler that condition `expr` is true and to optimize for it. 21 | /// 22 | /// The `expr` must not have side effects, and should not call any functions or 23 | /// methods (including operator methods), as it will often not have any effect 24 | /// in that case (especially in MSVC https://godbolt.org/z/7T7E3P598) 25 | /// 26 | /// `[[assume(expr)]]` will replace this macro in C++23. */ 27 | /// 28 | /// # Safety 29 | /// If the condition `expr` were to actually be false, Undefined Behaviour will 30 | /// result. 31 | // clang-format off 32 | #define _sus_assume(unsafe_fn, expr) \ 33 | static_assert(std::same_as, \ 34 | ::sus::marker::UnsafeFnMarker>); \ 35 | sus_if_clang(_Pragma("clang diagnostic push")) \ 36 | sus_if_clang(_Pragma("clang diagnostic ignored \"-Wassume\"")) \ 37 | sus_if_msvc_else(__assume, sus_if_clang_else(__builtin_assume, if))(expr) \ 38 | sus_if_msvc_else(, sus_if_clang_else(, {} else __builtin_unreachable())) \ 39 | sus_if_clang(_Pragma("clang diagnostic pop")) 40 | // clang-format on 41 | -------------------------------------------------------------------------------- /sus/macros/builtin.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #if !defined(__has_builtin) 18 | #define __has_builtin(X) false 19 | #endif 20 | 21 | #if !defined(__has_extension) 22 | #define __has_extension(X) false 23 | #endif 24 | 25 | #if !defined(__has_feature) 26 | #define __has_feature(X) false 27 | #endif 28 | 29 | #if !defined(__has_attribute) 30 | #define __has_attribute(X) false 31 | #endif 32 | -------------------------------------------------------------------------------- /sus/macros/eval_and_concat.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | /// Evaluates and concatenates two macro expressions. 18 | /// 19 | /// `x##y` will concatenate without evaluating. 20 | #define _sus_eval_and_concat(x, y) _sus__eval_and_concat_impl(x, y) 21 | 22 | #define _sus__eval_and_concat_impl(x, y) x##y 23 | -------------------------------------------------------------------------------- /sus/macros/eval_macro.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | /// A macro that runs a macro against any arguments. 18 | /// 19 | /// This is useful to join together macros, for instance to run one macro 20 | /// against the output of another macro. 21 | /// 22 | /// # Example 23 | /// ``` 24 | /// #define f(s) hello(s) 25 | /// _sus_eval_macro(f, "world") 26 | /// // Evaluates to: `hello("world")` 27 | /// ``` 28 | #define _sus_eval_macro(x, ...) x(__VA_ARGS__) 29 | -------------------------------------------------------------------------------- /sus/macros/inline.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/compiler.h" 18 | 19 | /// Replace the `inline` keyword on a function declaration with 20 | /// `_sus_always_inline` to force the compiler to inline it regardless of its 21 | /// heuristics. 22 | #define _sus_always_inline \ 23 | sus_if_msvc_else(__forceinline, inline __attribute__((always_inline))) 24 | 25 | /// Add `_sus_no_inline` to the start of a function declaration to prevent the 26 | /// compiler from inlining the function. 27 | #define _sus_no_inline \ 28 | sus_if_msvc_else(__declspec(noinline), __attribute__((noinline))) 29 | -------------------------------------------------------------------------------- /sus/macros/lifetimebound.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/compiler.h" 18 | 19 | #define sus_lifetimebound sus_if_clang([[clang::lifetimebound]]) 20 | -------------------------------------------------------------------------------- /sus/macros/no_unique_address.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/__private/compiler_bugs.h" 18 | #include "sus/macros/compiler.h" 19 | 20 | /// A macro to provide a platform-agnostic `no_unique_address` attribute name. 21 | /// 22 | /// This is required at all because MSVC uses its own syntax to force users to 23 | /// opt into it: 24 | /// https://devblogs.microsoft.com/cppblog/msvc-cpp20-and-the-std-cpp20-switch/ 25 | // 26 | // clang-format off 27 | #define _sus_no_unique_address \ 28 | sus_clang_bug_49358_else(sus_if_msvc(msvc::)no_unique_address) 29 | // clang-format on 30 | -------------------------------------------------------------------------------- /sus/macros/nodebug.h: -------------------------------------------------------------------------------- 1 | // Copyright 2025 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/compiler.h" 18 | 19 | /// Add `_sus_nodebug` to the start of a function declaration to prevent the 20 | /// compiler from adding debug info. 21 | #define _sus_nodebug \ 22 | sus_if_clang(__attribute__((nodebug))) 23 | -------------------------------------------------------------------------------- /sus/macros/pure.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/compiler.h" 18 | 19 | /// Used to mark a function as "pure", meaning it does not change any values 20 | /// outside of its own scope. 21 | /// 22 | /// A pure function is allowed to dereference pointers, and access global 23 | /// memory, but it may not change them. To do so can cause Undefined Behaviour. 24 | #define _sus_pure sus_if_msvc_else([[nodiscard]], __attribute__((pure))) 25 | 26 | /// Used to mark a function as "const", meaning it does not change any values 27 | /// outside of its own scope, and does not read global memory. 28 | /// 29 | /// Functions that dereference pointers can not be marked `__sus_pure_const`. 30 | /// 31 | /// A const function is allowed to only read from its inputs and determine an 32 | /// output from them, without going through pointers or accessing global memory. 33 | /// To do so anyway can cause Undefined Behaviour. 34 | #define __sus_pure_const sus_if_msvc_else([[nodiscard]], __attribute__((const))) 35 | -------------------------------------------------------------------------------- /sus/macros/remove_parens.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/eval_macro.h" 18 | 19 | /// Remove parentheses around one or more arguments, if they are present. 20 | /// 21 | /// It performs the following transformations, for any number of values inside 22 | /// the parens. 23 | /// * `x` => `x` 24 | /// * `(x)` => `x` 25 | /// * `(x, y)` => `x, y` 26 | /// 27 | /// Based on: https://stackoverflow.com/a/62984543 28 | #define _sus_remove_parens(x) _sus__remove_inner_rename(_sus__remove_inner x) 29 | 30 | #define _sus_remove_parens_and_eval(e, x) \ 31 | _sus_eval_macro(e, _sus_remove_parens(x)) 32 | 33 | // Implementation of _sus_remove_parens(x). 34 | 35 | // Step 1: If the input had brackets, now it no longer does. The result will 36 | // always be `_sus__remove_inner x` at the end. 37 | #define _sus__remove_inner(...) _sus__remove_inner __VA_ARGS__ 38 | // Step 2: Now that `x` has no parentheses, expand `x` into all of its 39 | // arguments, which we denote `x...`. 40 | #define _sus__remove_inner_rename(...) _sus__remove_inner_rename_(__VA_ARGS__) 41 | // Step 3: Concat to the start of the content, resulting in 42 | // `_sus_remove_outer_sus__remove_inner x...`. 43 | #define _sus__remove_inner_rename_(...) _sus__remove_outer##__VA_ARGS__ 44 | // Step 4: Remove the `_sus_remove_outer_sus__remove_inner`, leaving just 45 | // `x...`. 46 | #define _sus__remove_outer_sus__remove_inner 47 | -------------------------------------------------------------------------------- /sus/marker/unsafe_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/marker/unsafe.h" 16 | 17 | #include 18 | 19 | #include "googletest/include/gtest/gtest.h" 20 | 21 | namespace { 22 | 23 | using sus::marker::unsafe_fn; 24 | 25 | TEST(UnsafeFnMarker, fmt) { 26 | EXPECT_EQ(fmt::format("{}", unsafe_fn), "unsafe_fn"); 27 | } 28 | 29 | TEST(UnsafeFnMarker, Stream) { 30 | std::stringstream s; 31 | s << unsafe_fn; 32 | EXPECT_EQ(s.str(), "unsafe_fn"); 33 | } 34 | 35 | TEST(UnsafeFnMarker, GTest) { 36 | EXPECT_EQ(testing::PrintToString(unsafe_fn), "unsafe_fn"); 37 | } 38 | 39 | } // namespace 40 | -------------------------------------------------------------------------------- /sus/mem/__private/ref_concepts.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::mem::__private { 20 | 21 | template 22 | concept IsTrivialDtorOrRef = 23 | std::is_trivially_destructible_v || std::is_reference_v; 24 | 25 | template 26 | concept IsTrivialCopyCtorOrRef = 27 | std::is_trivially_copy_constructible_v || std::is_reference_v; 28 | 29 | template 30 | concept IsTrivialCopyAssignOrRef = 31 | std::is_trivially_copy_assignable_v || std::is_reference_v; 32 | 33 | template 34 | concept IsTrivialMoveCtorOrRef = 35 | std::is_trivially_move_constructible_v || std::is_reference_v; 36 | 37 | template 38 | concept IsTrivialMoveAssignOrRef = 39 | std::is_trivially_move_assignable_v || std::is_reference_v; 40 | 41 | } // namespace sus::mem::__private 42 | -------------------------------------------------------------------------------- /sus/mem/addressof.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/pure.h" 18 | 19 | namespace sus::mem { 20 | 21 | /// Returns the address of an object, regardless of whether a class type 22 | /// overrides `operator&`. 23 | /// 24 | /// This is like 25 | /// [`std::addressof`](https://en.cppreference.com/w/cpp/memory/addressof), 26 | /// without pulling in all of the `` header. It also supports Obj-C ARC 27 | /// pointers. 28 | template 29 | __sus_pure_const inline constexpr T* addressof(T& arg) noexcept { 30 | // __builtin_addressof also handles Obj-C ARC pointers. 31 | return __builtin_addressof(arg); 32 | } 33 | 34 | template 35 | T* addressof(T&& arg) = delete; 36 | 37 | } // namespace sus::mem 38 | -------------------------------------------------------------------------------- /sus/mem/addressof_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/mem/addressof.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | #include "sus/macros/__private/compiler_bugs.h" 19 | 20 | namespace { 21 | 22 | using sus::mem::addressof; 23 | 24 | TEST(AddressOf, Object) { 25 | struct S { 26 | sus_clang_bug_54040(constexpr inline S(int i) : i(i){}) int i; 27 | }; 28 | auto s = S(0); 29 | static_assert(std::is_object_v, ""); 30 | EXPECT_EQ(::sus::mem::addressof(s), &s); 31 | 32 | const auto cs = S(0); 33 | static_assert(std::is_object_v, ""); 34 | EXPECT_EQ(::sus::mem::addressof(cs), &cs); 35 | } 36 | 37 | TEST(AddressOf, OperatorOverride) { 38 | struct T { 39 | int i; 40 | unsigned char* operator&() const { return 0; } 41 | }; 42 | struct S { 43 | T t; 44 | }; 45 | S s; 46 | EXPECT_EQ(0, &s.t); 47 | EXPECT_EQ(reinterpret_cast(::sus::mem::addressof(s.t)), 48 | reinterpret_cast(&s)); 49 | } 50 | 51 | TEST(AddressOf, NonObject) { 52 | struct S {}; 53 | auto s = S(); 54 | S& r = s; 55 | static_assert(!std::is_object_v, ""); 56 | EXPECT_EQ(::sus::mem::addressof(r), &s); 57 | 58 | const S& cr = s; 59 | static_assert(!std::is_object_v, ""); 60 | EXPECT_EQ(::sus::mem::addressof(cr), &s); 61 | } 62 | 63 | } // namespace 64 | -------------------------------------------------------------------------------- /sus/mem/remove_rvalue_reference.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | namespace sus::mem { 18 | 19 | namespace __private { 20 | template 21 | struct remove_rvalue_reference_helper { 22 | using type = T; 23 | }; 24 | 25 | template 26 | struct remove_rvalue_reference_helper { 27 | using type = T; 28 | }; 29 | 30 | } // namespace __private 31 | 32 | /// Performs reference collapsing, removing rvalue references but leaving lvalue 33 | /// references untouched. 34 | template 35 | using remove_rvalue_reference = __private::remove_rvalue_reference_helper::type; 36 | 37 | } // namespace sus::mem 38 | -------------------------------------------------------------------------------- /sus/num/__private/check_integer_overflow.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | // SUS_CHECK_INTEGER_OVERFLOW can be defined to false to disable overflow checks 20 | // in integer arithmetic and shifting operations. 21 | #if !defined(SUS_CHECK_INTEGER_OVERFLOW) 22 | #define SUS_CHECK_INTEGER_OVERFLOW true 23 | #endif 24 | static_assert(SUS_CHECK_INTEGER_OVERFLOW == false || 25 | SUS_CHECK_INTEGER_OVERFLOW == true); 26 | -------------------------------------------------------------------------------- /sus/num/__private/literals_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include 16 | 17 | #include "sus/num/types.h" 18 | 19 | namespace { 20 | 21 | // Regression test for MSVC compilation bug: 22 | // https://developercommunity.visualstudio.com/t/MSVC-Compiler-bug-with:-numeric-literal/10108160 23 | template 24 | void TestOperationInTemplateFunction() { 25 | (void)(0_u32 + 1_u32); 26 | } 27 | 28 | } // namespace 29 | -------------------------------------------------------------------------------- /sus/num/__private/primitive_type.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "sus/lib/__private/forward_decl.h" 22 | #include "sus/mem/size_of.h" 23 | 24 | namespace sus::num::__private { 25 | 26 | template ()> 27 | struct ptr_type; 28 | 29 | template <> 30 | struct ptr_type<4> { 31 | using unsigned_type = uint32_t; 32 | using signed_type = int32_t; 33 | using pointer_sized_type = u32; 34 | }; 35 | 36 | template <> 37 | struct ptr_type<8> { 38 | using unsigned_type = uint64_t; 39 | using signed_type = int64_t; 40 | using pointer_sized_type = u64; 41 | }; 42 | 43 | template ()> 44 | struct addr_type; 45 | 46 | template <> 47 | struct addr_type<4> { 48 | using unsigned_type = uint32_t; 49 | using signed_type = int32_t; 50 | }; 51 | 52 | template <> 53 | struct addr_type<8> { 54 | using unsigned_type = uint64_t; 55 | using signed_type = int64_t; 56 | }; 57 | 58 | } // namespace sus::num::__private 59 | -------------------------------------------------------------------------------- /sus/num/__private/signed_integer_consts.inc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | 18 | /////////////////////////////////////////////////////////////////////////// 19 | // 20 | // Defines constants in an integer type. 21 | // 22 | // TO USE THIS INC FILE: 23 | // 24 | // Include it beside the class, in the same namespace. 25 | // 26 | // Define `_self` to the name of the integer type. 27 | // Define `_primitive` to the primitive it holds inside. 28 | /////////////////////////////////////////////////////////////////////////// 29 | 30 | inline constexpr _self _self::MIN = _self(_self::MIN_PRIMITIVE); 31 | inline constexpr _self _self::MAX = _self(_self::MAX_PRIMITIVE); 32 | inline constexpr u32 _self::BITS = u32(__private::num_bits<_primitive>()); 33 | 34 | #undef _self 35 | #undef _primitive 36 | -------------------------------------------------------------------------------- /sus/num/__private/unsigned_integer_consts.inc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | 18 | /////////////////////////////////////////////////////////////////////////// 19 | // 20 | // Defines constants in an integer type. 21 | // 22 | // TO USE THIS INC FILE: 23 | // 24 | // Include it beside the class, in the same namespace. 25 | // 26 | // Define `_self` to the name of the integer type. 27 | // Define `_primitive` to the primitive it holds inside. 28 | // Define `_pointer` to true if the type is `uptr` or false otherwise. 29 | /////////////////////////////////////////////////////////////////////////// 30 | 31 | inline constexpr _self _self::MIN = _self(_self::MIN_PRIMITIVE); 32 | 33 | #if _pointer 34 | inline constexpr _self _self::MAX_BIT_PATTERN = _self(_self::MAX_PRIMITIVE); 35 | #else 36 | inline constexpr _self _self::MAX = _self(_self::MAX_PRIMITIVE); 37 | #endif 38 | 39 | inline constexpr u32 _self::BITS = u32(__private::num_bits<_primitive>()); 40 | 41 | #undef _self 42 | #undef _primitive 43 | #undef _pointer 44 | -------------------------------------------------------------------------------- /sus/num/float_concepts.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/num/types.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "sus/lib/__private/forward_decl.h" 22 | 23 | namespace sus::num { 24 | 25 | template 26 | concept Float = 27 | std::same_as || std::same_as; 28 | 29 | template 30 | concept PrimitiveFloat = std::same_as || std::same_as || 31 | std::same_as; 32 | 33 | } // namespace sus::num 34 | -------------------------------------------------------------------------------- /sus/num/float_impl.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/num/types.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/collections/array.h" 20 | #include "sus/num/float.h" 21 | 22 | #define _self f32 23 | #define _primitive float 24 | #define _unsigned u32 25 | #include "sus/num/__private/float_methods_impl.inc" 26 | 27 | #define _self f64 28 | #define _primitive double 29 | #define _unsigned u64 30 | #include "sus/num/__private/float_methods_impl.inc" 31 | -------------------------------------------------------------------------------- /sus/num/fp_category.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/num/types.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::num { 20 | 21 | /// A classification of floating point numbers. 22 | /// 23 | /// This enum is used as the return type for `f32::classify()` and 24 | /// `f64::classify()`. See their documentation for more. 25 | enum class FpCategory { Nan, Infinite, Zero, Subnormal, Normal }; 26 | 27 | } // namespace sus::num 28 | -------------------------------------------------------------------------------- /sus/num/signed_integer_impl.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/num/types.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/collections/array.h" 20 | #include "sus/num/signed_integer.h" 21 | #include "sus/ptr/copy.h" 22 | 23 | #define _self i8 24 | #define _primitive int8_t 25 | #define _unsigned u8 26 | #include "sus/num/__private/signed_integer_methods_impl.inc" 27 | 28 | #define _self i16 29 | #define _primitive int16_t 30 | #define _unsigned u16 31 | #include "sus/num/__private/signed_integer_methods_impl.inc" 32 | 33 | #define _self i32 34 | #define _primitive int32_t 35 | #define _unsigned u32 36 | #include "sus/num/__private/signed_integer_methods_impl.inc" 37 | 38 | #define _self i64 39 | #define _primitive int64_t 40 | #define _unsigned u64 41 | #include "sus/num/__private/signed_integer_methods_impl.inc" 42 | 43 | #define _self isize 44 | #define _primitive ::sus::num::__private::addr_type<>::signed_type 45 | #define _unsigned usize 46 | #include "sus/num/__private/signed_integer_methods_impl.inc" 47 | -------------------------------------------------------------------------------- /sus/num/try_from_int_error.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/num/types.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/macros/pure.h" 20 | 21 | namespace sus::num { 22 | 23 | /// The error type returned when a checked integral type conversion fails. 24 | class TryFromIntError { 25 | public: 26 | /// The type of error which occured. 27 | enum class Kind { 28 | OutOfBounds, 29 | }; 30 | 31 | /// Constructs a TryFromIntError with kind `OutOfBounds`. 32 | constexpr static TryFromIntError with_out_of_bounds() noexcept; 33 | 34 | /// Gives the kind of error that occured. 35 | _sus_pure constexpr Kind kind() const noexcept; 36 | 37 | /// Satisfies the [`Eq`]($sus::cmp::Eq) concept. 38 | _sus_pure friend constexpr bool operator==(TryFromIntError lhs, 39 | TryFromIntError rhs) noexcept { 40 | return lhs.kind_ == rhs.kind_; 41 | } 42 | 43 | private: 44 | enum Construct { CONSTRUCT }; 45 | constexpr explicit TryFromIntError(Construct, Kind k) noexcept; 46 | 47 | Kind kind_; 48 | }; 49 | 50 | } // namespace sus::num 51 | -------------------------------------------------------------------------------- /sus/num/try_from_int_error_impl.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/num/types.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include 20 | 21 | #include "sus/assertions/unreachable.h" 22 | #include "sus/error/error.h" 23 | #include "sus/macros/pure.h" 24 | #include "sus/marker/unsafe.h" 25 | #include "sus/num/try_from_int_error.h" 26 | 27 | namespace sus::num { 28 | 29 | constexpr TryFromIntError TryFromIntError::with_out_of_bounds() noexcept { 30 | return TryFromIntError(CONSTRUCT, Kind::OutOfBounds); 31 | } 32 | 33 | _sus_pure constexpr TryFromIntError::Kind TryFromIntError::kind() 34 | const noexcept { 35 | return kind_; 36 | } 37 | 38 | constexpr TryFromIntError::TryFromIntError(Construct, Kind k) noexcept 39 | : kind_(k) {} 40 | 41 | } // namespace sus::num 42 | 43 | // sus::error::Error implementation. 44 | template <> 45 | struct sus::error::ErrorImpl { 46 | constexpr static std::string display( 47 | const sus::num::TryFromIntError& e) noexcept { 48 | switch (e.kind()) { 49 | case sus::num::TryFromIntError::Kind::OutOfBounds: return "out of bounds"; 50 | } 51 | ::sus::unreachable_unchecked(::sus::marker::unsafe_fn); 52 | } 53 | }; 54 | 55 | static_assert(sus::error::Error); 56 | -------------------------------------------------------------------------------- /sus/num/unsigned_integer_consts.h: -------------------------------------------------------------------------------- 1 | // Copyright 2025 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/num/types.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/num/unsigned_integer.h" 20 | 21 | namespace sus::num { 22 | 23 | #define _self u32 24 | #define _pointer false 25 | #define _primitive uint32_t 26 | #include "sus/num/__private/unsigned_integer_consts.inc" 27 | 28 | #define _self u8 29 | #define _pointer false 30 | #define _primitive uint8_t 31 | #include "sus/num/__private/unsigned_integer_consts.inc" 32 | 33 | #define _self u16 34 | #define _pointer false 35 | #define _primitive uint16_t 36 | #include "sus/num/__private/unsigned_integer_consts.inc" 37 | 38 | #define _self u64 39 | #define _pointer false 40 | #define _primitive uint64_t 41 | #include "sus/num/__private/unsigned_integer_consts.inc" 42 | 43 | #define _self usize 44 | #define _pointer false 45 | #define _primitive \ 46 | ::sus::num::__private::ptr_type<::sus::mem::size_of()>::unsigned_type 47 | #include "sus/num/__private/unsigned_integer_consts.inc" 48 | 49 | #define _self uptr 50 | #define _pointer true 51 | #define _primitive \ 52 | ::sus::num::__private::ptr_type< \ 53 | ::sus::mem::size_of()>::unsigned_type 54 | #include "sus/num/__private/unsigned_integer_consts.inc" 55 | 56 | } // namespace sus::num 57 | -------------------------------------------------------------------------------- /sus/option/__private/is_option_type.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/lib/__private/forward_decl.h" 20 | 21 | namespace sus::option::__private { 22 | 23 | template 24 | struct IsOptionType final : std::false_type { 25 | using inner_type = void; 26 | }; 27 | 28 | template 29 | struct IsOptionType> final : std::true_type { 30 | using inner_type = U; 31 | }; 32 | 33 | } // namespace sus::option::__private 34 | -------------------------------------------------------------------------------- /sus/option/__private/is_tuple_type.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/lib/__private/forward_decl.h" 20 | 21 | namespace sus::option::__private { 22 | 23 | template 24 | struct IsTupleOfSizeTwo final { 25 | static constexpr bool value = false; 26 | using first_type = void; 27 | using second_type = void; 28 | }; 29 | 30 | template 31 | struct IsTupleOfSizeTwo<::sus::tuple_type::Tuple> final { 32 | static constexpr bool value = true; 33 | using first_type = U; 34 | using second_type = V; 35 | }; 36 | 37 | } // namespace sus::option::__private 38 | -------------------------------------------------------------------------------- /sus/option/compat_option.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/option/option.h" 20 | #include "sus/ops/try.h" 21 | 22 | // Implements sus::ops::Try for std::optional. 23 | template 24 | struct sus::ops::TryImpl> { 25 | using Output = T; 26 | template 27 | using RemapOutput = std::optional; 28 | constexpr static bool is_success(const std::optional& t) { 29 | return t.has_value(); 30 | } 31 | constexpr static Output into_output(std::optional t) { 32 | // SAFETY: `operator*` causes UB if the optional is empty. But the optional 33 | // is verified to be holding a value by `sus::ops::try_into_output()` before 34 | // it calls here. 35 | return *::sus::move(t); 36 | } 37 | constexpr static std::optional from_output(Output t) { 38 | return std::optional(std::in_place, ::sus::move(t)); 39 | } 40 | template 41 | constexpr static std::optional preserve_error(std::optional) noexcept { 42 | // The incoming optional is known to be empty (the error state) and this is 43 | // checked by try_preserve_error() before coming here. So we can just return 44 | // another empty optional. 45 | return std::optional(); 46 | } 47 | 48 | // Implements sus::ops::TryDefault for `std::optional` if `T` satisfies 49 | // `Default`. 50 | constexpr static std::optional from_default() noexcept 51 | requires(sus::construct::Default) 52 | { 53 | return std::optional(std::in_place, T()); 54 | } 55 | }; 56 | 57 | -------------------------------------------------------------------------------- /sus/option/state.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private, include "sus/option/option.h" 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::option { 20 | 21 | /// The representation of an Option's state, which can either be #None to 22 | /// represent it has no value, or #Some for when it is holding a value. 23 | enum class State : bool { 24 | /// The Option is not holding any value. 25 | None, 26 | /// The Option is holding a value. 27 | Some, 28 | }; 29 | 30 | } // namespace sus::option 31 | -------------------------------------------------------------------------------- /sus/project.md: -------------------------------------------------------------------------------- 1 | # The Subspace C++ Library 2 | 3 | The Subspace C++ Library provides a concept-centered abstraction on top of 4 | the bare-metal C++ standard library. It provides the tools to build 5 | stable applications quickly, and to make your application performant through 6 | explicitly leveraging compiler optimizations without accidentally tripping 7 | over Undefined Behaviour and miscompilation. Stop spending time debugging 8 | tricky heisenbugs and start relying on the compiler to produce the program 9 | you wrote. 10 | 11 | Find Subspace on Github here: 12 | [https://github.com/chromium/subspace](https://github.com/chromium/subspace) 13 | 14 | ## Prelude 15 | 16 | The `sus/prelude.h` header imports the most commonly used types into the 17 | global namespace. All types in the [`prelude`]($sus::prelude) namespace are 18 | included: 19 | * [`Option`]($sus::option::Option) 20 | * [`Vec`]($sus::collections::Vec) 21 | * [`i32`]($sus::num::i32) and other integers 22 | * [`f32`]($sus::num::f32) and other floating point types 23 | * [`unsafe_fn`]($sus::marker::unsafe_fn) marker 24 | 25 | ### Example 26 | ``` 27 | #include "sus/prelude.h" 28 | 29 | int main() { 30 | auto v = Vec(1, 2, 3, 4, 5); 31 | return sus::move(v).into_iter().sum(); 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /sus/ptr/as_ref.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/macros/lifetimebound.h" 18 | #include "sus/option/option.h" 19 | 20 | namespace sus::ptr { 21 | 22 | /// Returns an empty [`Option`]($sus::option::Option) if the `T*` pointer is 23 | /// null, or else returns a reference to the value `T` wrapped in an 24 | /// [`Option`]($sus::option::Option). 25 | /// 26 | /// This is the [`pointer::as_ref`]( 27 | /// https://doc.rust-lang.org/stable/std/primitive.pointer.html#method.as_ref) 28 | /// operation in Rust. However it is not unsafe in C++ as no lifetime is 29 | /// created as part of the operation. 30 | template 31 | constexpr inline sus::Option as_ref(T* pointer sus_lifetimebound) noexcept { 32 | if (pointer != nullptr) 33 | return sus::Option(*pointer); 34 | else 35 | return sus::Option(); 36 | } 37 | 38 | } // namespace sus::ptr 39 | -------------------------------------------------------------------------------- /sus/ptr/as_ref_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/ptr/as_ref.h" 16 | 17 | #include 18 | 19 | #include "googletest/include/gtest/gtest.h" 20 | #include "sus/prelude.h" 21 | 22 | namespace { 23 | using sus::Option; 24 | 25 | TEST(PtrAsRef, Test) { 26 | // Mutable. 27 | { 28 | i32 i = 2; 29 | i32* p = &i; 30 | i32* n = nullptr; 31 | auto o = sus::ptr::as_ref(p); 32 | static_assert(std::same_as>); 33 | EXPECT_EQ(&*o, &i); 34 | auto o2 = sus::ptr::as_ref(n); 35 | static_assert(std::same_as>); 36 | EXPECT_EQ(o2.is_none(), true); 37 | } 38 | // Const. 39 | { 40 | const i32 i = 2; 41 | const i32* p = &i; 42 | const i32* n = nullptr; 43 | auto o = sus::ptr::as_ref(p); 44 | static_assert(std::same_as>); 45 | EXPECT_EQ(&*o, &i); 46 | auto o2 = sus::ptr::as_ref(n); 47 | static_assert(std::same_as>); 48 | EXPECT_EQ(o2.is_none(), true); 49 | } 50 | 51 | // Constexpr. 52 | static_assert(sus::ptr::as_ref(nullptr).is_none()); 53 | static_assert([]() { 54 | const i32 i = 2; 55 | auto o = sus::ptr::as_ref(&i); 56 | return o.is_some(); 57 | }()); 58 | } 59 | 60 | } // namespace 61 | -------------------------------------------------------------------------------- /sus/ptr/subclass.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | namespace sus::ptr { 20 | 21 | /// `SameOrSubclassOf` is true if `T` is the same type as `U` or 22 | /// inherits from `U`. 23 | /// 24 | /// This can replace the use of std::is_convertible_v for handling compatible 25 | /// pointers. 26 | /// 27 | /// The inputs must be pointer types, which helps avoid accidental conversions 28 | /// from arrays, as they are not pointers. Also helps prevent the decay from the 29 | /// array to a pointer before the concept is called. 30 | // 31 | // clang-format off 32 | template 33 | concept SameOrSubclassOf = 34 | std::is_pointer_v && 35 | std::is_pointer_v && 36 | std::is_convertible_v; 37 | // clang-format on 38 | 39 | } // namespace sus::ptr 40 | -------------------------------------------------------------------------------- /sus/ptr/subclass_unittest.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/ptr/subclass.h" 16 | 17 | #include "googletest/include/gtest/gtest.h" 18 | 19 | namespace { 20 | 21 | using sus::ptr::SameOrSubclassOf; 22 | 23 | struct Base {}; 24 | struct Sub : public Base {}; 25 | struct Other {}; 26 | 27 | static_assert(SameOrSubclassOf); 28 | static_assert(SameOrSubclassOf); 29 | static_assert(!SameOrSubclassOf); 30 | 31 | // Only works with pointers. 32 | static_assert(!SameOrSubclassOf); 33 | static_assert(!SameOrSubclassOf); 34 | static_assert(!SameOrSubclassOf); 35 | static_assert(!SameOrSubclassOf); 36 | 37 | template X> 38 | void fn(X) {} 39 | 40 | TEST(Subclass, ConceptGuard) { 41 | auto s = Sub(); 42 | fn(&s); 43 | } 44 | 45 | } // namespace 46 | -------------------------------------------------------------------------------- /sus/result/__private/is_result_type.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | #include "sus/lib/__private/forward_decl.h" 20 | 21 | namespace sus::result::__private { 22 | 23 | template 24 | struct IsResultType final : std::false_type { 25 | using ok_type = void; 26 | using err_type = void; 27 | }; 28 | 29 | template 30 | struct IsResultType<::sus::result::Result> final : std::true_type { 31 | using ok_type = U; 32 | using err_type = V; 33 | }; 34 | 35 | template 36 | concept IsResult = IsResultType::value; 37 | 38 | template 39 | concept IsResultWithOkType = 40 | IsResult && std::same_as::ok_type>; 41 | 42 | template 43 | concept IsResultWithErrType = 44 | IsResult && std::same_as::err_type>; 45 | 46 | } // namespace sus::result::__private 47 | -------------------------------------------------------------------------------- /sus/result/__private/result_state.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | // IWYU pragma: private 16 | // IWYU pragma: friend "sus/.*" 17 | #pragma once 18 | 19 | namespace sus::result::__private { 20 | 21 | enum class ResultState { IsErr = 0, IsOk = 1, IsMoved = 2 }; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /sus/result/ok_void.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | namespace sus::result { 18 | 19 | /// A marker type for constructing Result with an Ok(void) value. 20 | struct OkVoid {}; 21 | 22 | } // namespace sus::result 23 | -------------------------------------------------------------------------------- /sus/string/compat_string.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include 18 | 19 | #include "sus/iter/from_iterator.h" 20 | #include "sus/iter/iterator.h" 21 | 22 | template 23 | struct sus::iter::FromIteratorImpl> { 24 | static constexpr std::basic_string from_iter( 25 | ::sus::iter::IntoIterator auto&& into_iter) noexcept 26 | requires(sus::mem::IsMoveRef) 27 | { 28 | auto&& iter = sus::move(into_iter).into_iter(); 29 | auto [lower, upper] = iter.size_hint(); 30 | auto s = std::basic_string(); 31 | s.reserve(lower); 32 | for (Char c : iter) s.push_back(c); 33 | return s; 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /sus/test/ensure_use.cc: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "sus/test/ensure_use.h" 16 | 17 | namespace sus::test { 18 | 19 | void ensure_use(void*) {} 20 | 21 | } // namespace sus::test 22 | -------------------------------------------------------------------------------- /sus/test/ensure_use.h: -------------------------------------------------------------------------------- 1 | // Copyright 2023 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | namespace sus::test { 18 | 19 | void ensure_use(void*); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /sus/test/no_copy_move.h: -------------------------------------------------------------------------------- 1 | // Copyright 2022 Google LLC 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // https://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #pragma once 16 | 17 | #include "sus/mem/clone.h" 18 | #include "sus/mem/copy.h" 19 | #include "sus/mem/move.h" 20 | 21 | namespace sus::test { 22 | 23 | struct NoCopyMove { 24 | constexpr NoCopyMove() {} // Non-trivial. 25 | NoCopyMove(NoCopyMove&&) = delete; 26 | NoCopyMove& operator=(NoCopyMove&&) = delete; 27 | constexpr ~NoCopyMove() {} // Non-trivial. 28 | 29 | friend constexpr bool operator==(const NoCopyMove& lhs, 30 | const NoCopyMove& rhs) noexcept { 31 | return &lhs == &rhs; 32 | } 33 | friend constexpr std::strong_ordering operator<=>( 34 | const NoCopyMove& lhs, const NoCopyMove& rhs) noexcept { 35 | return &lhs <=> &rhs; 36 | } 37 | }; 38 | 39 | static_assert(!sus::mem::Copy); 40 | static_assert(!sus::mem::Clone); 41 | static_assert(!sus::mem::Move); 42 | static_assert(NoCopyMove() != NoCopyMove()); 43 | static_assert([]() { 44 | NoCopyMove n; 45 | return n == n; 46 | }()); 47 | 48 | } // namespace sus::test 49 | -------------------------------------------------------------------------------- /tools/cmake/OptionIf.cmake: -------------------------------------------------------------------------------- 1 | # Copyright 2023 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | # option_if_not_defined(name description default) 16 | # 17 | # Behaves like: 18 | # option(name description default) 19 | # 20 | # If a variable is not already defined with the given name, otherwise the 21 | # function does nothing. 22 | # 23 | # Simplifies customization by projects that use Subspace as a dependency. 24 | function (option_if_not_defined name description default) 25 | if(NOT DEFINED ${name}) 26 | option(${name} ${description} ${default}) 27 | endif() 28 | endfunction() 29 | -------------------------------------------------------------------------------- /tools/configure.cmd: -------------------------------------------------------------------------------- 1 | call vcvarsall x64 2 | 3 | @set LLVM_DIR=C:/Users/Admin/Documents/s/llvm/install/lib/cmake/llvm 4 | @set Clang_DIR=C:/Users/Admin/Documents/s/llvm/install/lib/cmake/clang 5 | @cmake -B out -GNinja ^ 6 | -DSUBSPACE_BUILD_SUBDOC=ON ^ 7 | -DSUBSPACE_BUILD_TESTS=ON 8 | -------------------------------------------------------------------------------- /tools/configure_clangd.cmd: -------------------------------------------------------------------------------- 1 | call vcvarsall x64 2 | 3 | @set LLVM_DIR=C:/Users/Admin/Documents/s/llvm/build/install/lib/cmake/llvm 4 | @set Clang_DIR=C:/Users/Admin/Documents/s/llvm/build/install/lib/cmake/clang 5 | @cmake -B out/clangd -GNinja ^ 6 | -DCMAKE_C_COMPILER="C:/Users/Admin/Documents/s/llvm/build/install/bin/clang-cl.exe" ^ 7 | -DCMAKE_CXX_COMPILER="C:/Users/Admin/Documents/s/llvm/build/install/bin/clang-cl.exe" ^ 8 | -DSUBSPACE_BUILD_SUBDOC=ON ^ 9 | -DSUBSPACE_BUILD_TESTS=ON 10 | -------------------------------------------------------------------------------- /tools/run_subdoc.bat: -------------------------------------------------------------------------------- 1 | out\subdoc\subdoc -p out --out docs ^ 2 | --include-file-pattern /sus/ ^ 3 | --exclude-file-pattern /third_party/ ^ 4 | --exclude-file-pattern /test/ ^ 5 | --exclude-file-pattern test.cc ^ 6 | --include-macro-prefix sus_ ^ 7 | --include-macro-prefix SUS_ ^ 8 | --css subdoc-test-style.css ^ 9 | --copy-file subdoc/gen_tests/subdoc-test-style.css ^ 10 | --copy-file "web/logos/logo-512px/subspace unpadded.png;logo.png" ^ 11 | --favicon "logo.png;image/png" ^ 12 | --project-md sus/project.md ^ 13 | --project-logo logo.png ^ 14 | --project-name Subspace ^ 15 | --project-url https://suslib.cc ^ 16 | --project-version 0.1.2 ^ 17 | --ignore-bad-code-links ^ 18 | --remove-source-path-prefix %cd% ^ 19 | --add-source-path-prefix .. ^ 20 | --source-path-line-prefix L ^ 21 | subspace/sus/num/uptr_uni reverse_uni 22 | -------------------------------------------------------------------------------- /tools/run_subdoc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | out/subdoc/subdoc -p out --out docs \ 4 | --include-file-pattern /sus/ \ 5 | --exclude-file-pattern /third_party/ \ 6 | --exclude-file-pattern /test/ \ 7 | --exclude-file-pattern test.cc \ 8 | --include-macro-prefix sus_ \ 9 | --include-macro-prefix SUS_ \ 10 | --css subdoc-test-style.css \ 11 | --copy-file subdoc/gen_tests/subdoc-test-style.css \ 12 | --copy-file "web/logos/logo-512px/subspace unpadded.png;logo.png" \ 13 | --favicon "logo.png;image/png" \ 14 | --project-md sus/project.md \ 15 | --project-logo logo.png \ 16 | --project-name Subspace \ 17 | --project-url https://suslib.cc \ 18 | --project-version 0.1.2 \ 19 | --ignore-bad-code-links \ 20 | --remove-source-path-prefix $PWD \ 21 | --add-source-path-prefix .. \ 22 | --source-path-line-prefix L \ 23 | i8_unittest $* 24 | --------------------------------------------------------------------------------