archery-1.2.2/.cargo_vcs_info.json0000644000000001360000000000100124730ustar { "git": { "sha1": "607e290fcb38d0085c69df0c51bb8c048ec2262e" }, "path_in_vcs": "" }archery-1.2.2/Cargo.lock0000644000000350050000000000100104510ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "aho-corasick" version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] [[package]] name = "anes" version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstyle" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" [[package]] name = "archery" version = "1.2.2" dependencies = [ "criterion", "pretty_assertions", "serde", "serde_json", "static_assertions", "triomphe", ] [[package]] name = "autocfg" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" [[package]] name = "bumpalo" version = "3.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" [[package]] name = "cfg-if" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9" [[package]] name = "ciborium" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" dependencies = [ "ciborium-io", "ciborium-ll", "serde", ] [[package]] name = "ciborium-io" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" [[package]] name = "ciborium-ll" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" dependencies = [ "ciborium-io", "half", ] [[package]] name = "clap" version = "4.5.48" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2134bb3ea021b78629caa971416385309e0131b351b25e01dc16fb54e1b5fae" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" version = "4.5.48" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2ba64afa3c0a6df7fa517765e31314e983f51dda798ffba27b988194fb65dc9" dependencies = [ "anstyle", "clap_lex", ] [[package]] name = "clap_lex" version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b94f61472cee1439c0b966b47e3aca9ae07e45d070759512cd390ea2bebc6675" [[package]] name = "criterion" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1c047a62b0cc3e145fa84415a3191f628e980b194c2755aa12300a4e6cbd928" dependencies = [ "anes", "cast", "ciborium", "clap", "criterion-plot", "itertools", "num-traits", "oorandom", "plotters", "rayon", "regex", "serde", "serde_json", "tinytemplate", "walkdir", ] [[package]] name = "criterion-plot" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b1bcc0dc7dfae599d84ad0b1a55f80cde8af3725da8313b528da95ef783e338" dependencies = [ "cast", "itertools", ] [[package]] name = "crossbeam-deque" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" dependencies = [ "crossbeam-epoch", "crossbeam-utils", ] [[package]] name = "crossbeam-epoch" version = "0.9.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" dependencies = [ "crossbeam-utils", ] [[package]] name = "crossbeam-utils" version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" [[package]] name = "crunchy" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" [[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "half" version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "459196ed295495a68f7d7fe1d84f6c4b7ff0e21fe3017b2f283c6fac3ad803c9" dependencies = [ "cfg-if", "crunchy", ] [[package]] name = "itertools" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "js-sys" version = "0.3.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec48937a97411dcb524a265206ccd4c90bb711fca92b2792c407f268825b9305" dependencies = [ "once_cell", "wasm-bindgen", ] [[package]] name = "log" version = "0.4.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] name = "memchr" version = "2.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" [[package]] name = "num-traits" version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] [[package]] name = "once_cell" version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" [[package]] name = "oorandom" version = "11.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" [[package]] name = "plotters" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" dependencies = [ "num-traits", "plotters-backend", "plotters-svg", "wasm-bindgen", "web-sys", ] [[package]] name = "plotters-backend" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" [[package]] name = "plotters-svg" version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" dependencies = [ "plotters-backend", ] [[package]] name = "pretty_assertions" version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" dependencies = [ "diff", "yansi", ] [[package]] name = "proc-macro2" version = "1.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce25767e7b499d1b604768e7cde645d14cc8584231ea6b295e9c9eb22c02e1d1" dependencies = [ "proc-macro2", ] [[package]] name = "rayon" version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "368f01d005bf8fd9b1206fb6fa653e6c4a81ceb1466406b81792d87c5677a58f" dependencies = [ "either", "rayon-core", ] [[package]] name = "rayon-core" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" dependencies = [ "crossbeam-deque", "crossbeam-utils", ] [[package]] name = "regex" version = "1.11.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b5288124840bee7b386bc413c487869b360b2b4ec421ea56425128692f2a82c" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "833eb9ce86d40ef33cb1306d8accf7bc8ec2bfea4355cbdebb3df68b40925cad" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001" [[package]] name = "rustversion" version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "ryu" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" [[package]] name = "same-file" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" dependencies = [ "winapi-util", ] [[package]] name = "serde" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", "serde_derive", ] [[package]] name = "serde_core" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.145" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" dependencies = [ "itoa", "memchr", "ryu", "serde", "serde_core", ] [[package]] name = "static_assertions" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "syn" version = "2.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tinytemplate" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" dependencies = [ "serde", "serde_json", ] [[package]] name = "triomphe" version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef8f7726da4807b58ea5c96fdc122f80702030edc33b35aff9190a51148ccc85" [[package]] name = "unicode-ident" version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" [[package]] name = "walkdir" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" dependencies = [ "same-file", "winapi-util", ] [[package]] name = "wasm-bindgen" version = "0.2.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1da10c01ae9f1ae40cbfac0bac3b1e724b320abfcf52229f80b547c0d250e2d" dependencies = [ "cfg-if", "once_cell", "rustversion", "wasm-bindgen-macro", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-backend" version = "0.2.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "671c9a5a66f49d8a47345ab942e2cb93c7d1d0339065d4f8139c486121b43b19" dependencies = [ "bumpalo", "log", "proc-macro2", "quote", "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" version = "0.2.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ca60477e4c59f5f2986c50191cd972e3a50d8a95603bc9434501cf156a9a119" dependencies = [ "quote", "wasm-bindgen-macro-support", ] [[package]] name = "wasm-bindgen-macro-support" version = "0.2.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7" dependencies = [ "proc-macro2", "quote", "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" version = "0.2.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bad67dc8b2a1a6e5448428adec4c3e84c43e561d8c9ee8a9e5aabeb193ec41d1" dependencies = [ "unicode-ident", ] [[package]] name = "web-sys" version = "0.3.81" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9367c417a924a74cae129e6a2ae3b47fabb1f8995595ab474029da749a8be120" dependencies = [ "js-sys", "wasm-bindgen", ] [[package]] name = "winapi-util" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ "windows-sys", ] [[package]] name = "windows-link" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "45e46c0661abb7180e7b9c281db115305d49ca1709ab8242adf09666d2173c65" [[package]] name = "windows-sys" version = "0.61.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f109e41dd4a3c848907eb83d5a42ea98b3769495597450cf6d153507b166f0f" dependencies = [ "windows-link", ] [[package]] name = "yansi" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" archery-1.2.2/Cargo.toml0000644000000046550000000000100105030ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2024" rust-version = "1.85.0" name = "archery" version = "1.2.2" authors = ["Diogo Sousa "] build = false include = [ "/src/**/*.rs", "/Cargo.toml", "/LICENSE.md", "/README.md", "/release-notes.md", ] autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "Abstract over the atomicity of reference-counting pointers" homepage = "https://github.com/orium/archery" documentation = "https://docs.rs/archery" readme = "README.md" keywords = [ "rc", "arc", "reference-counting", "no_std", ] categories = [ "concurrency", "memory-management", "rust-patterns", ] license = "MIT" repository = "https://github.com/orium/archery" [package.metadata.docs.rs] features = [ "triomphe", "serde", ] [badges.codecov] branch = "main" repository = "orium/archery" service = "github" [features] fatal-warnings = [] serde = ["dep:serde"] triomphe = ["dep:triomphe"] [lib] name = "archery" path = "src/lib.rs" bench = false [dependencies.serde] version = "1.0.228" optional = true default-features = false [dependencies.triomphe] version = "0.1.14" optional = true default-features = false [dev-dependencies.criterion] version = "0.7.0" features = ["html_reports"] [dev-dependencies.pretty_assertions] version = "1.4.1" [dev-dependencies.serde_json] version = "1.0.145" [dev-dependencies.static_assertions] version = "1.1.0" [lints.clippy] explicit-deref-methods = "allow" if-not-else = "allow" inline-always = "allow" match-bool = "allow" missing-errors-doc = "allow" missing-safety-doc = "allow" module-name-repetitions = "allow" partialeq-ne-impl = "allow" similar-names = "allow" single-match-else = "allow" use-self = "allow" wildcard-imports = "allow" [lints.clippy.all] level = "warn" priority = -2 [lints.clippy.correctness] level = "deny" priority = -1 [lints.clippy.pedantic] level = "warn" priority = -2 [lints.rustdoc] redundant-explicit-links = "allow" archery-1.2.2/Cargo.toml.orig000064400000000000000000000050211046102023000141500ustar 00000000000000[package] name = "archery" description = "Abstract over the atomicity of reference-counting pointers" version = "1.2.2" authors = ["Diogo Sousa "] edition = "2024" rust-version = "1.85.0" homepage = "https://github.com/orium/archery" repository = "https://github.com/orium/archery" documentation = "https://docs.rs/archery" readme = "README.md" keywords = ["rc", "arc", "reference-counting", "no_std"] categories = ["concurrency", "memory-management", "rust-patterns"] license = "MIT" # What to include when packaging. include = ["/src/**/*.rs", "/Cargo.toml", "/LICENSE.md", "/README.md", "/release-notes.md"] [badges] codecov = { repository = "orium/archery", branch = "main", service = "github" } [dependencies] serde = { version = "1.0.228", optional = true, default-features = false } triomphe = { version = "0.1.14", optional = true, default-features = false } [dev-dependencies] criterion = { version = "0.7.0", features = ["html_reports"] } pretty_assertions = "1.4.1" serde_json = "1.0.145" static_assertions = "1.1.0" [features] fatal-warnings = [] triomphe = ["dep:triomphe"] serde = ["dep:serde"] [lints.clippy] all = { level = "warn", priority = -2 } correctness = { level = "deny", priority = -1 } pedantic = { level = "warn", priority = -2 } explicit-deref-methods = "allow" if-not-else = "allow" inline-always = "allow" match-bool = "allow" missing-errors-doc = "allow" missing-safety-doc = "allow" module-name-repetitions = "allow" partialeq-ne-impl = "allow" similar-names = "allow" single-match-else = "allow" use-self = "allow" wildcard-imports = "allow" [lints.rustdoc] # TODO This is only needed because `cargo-rdme` requires a path like `crate::⋯`. Once that limitation is lifted we # can remove this. redundant-explicit-links = "allow" [lib] # Disable libtest to make sure criterion can parse the command line flags. # See https://bheisler.github.io/criterion.rs/book/faq.html and https://github.com/rust-lang/rust/issues/47241. bench = false [[bench]] name = "std_rc" path = "benches/std_rc.rs" harness = false [[bench]] name = "std_arc" path = "benches/std_arc.rs" harness = false [[bench]] name = "archery_shared_pointer_rc" path = "benches/archery_shared_pointer_rc.rs" harness = false [[bench]] name = "archery_shared_pointer_arc" path = "benches/archery_shared_pointer_arc.rs" harness = false [[bench]] name = "archery_shared_pointer_arct" path = "benches/archery_shared_pointer_arct.rs" harness = false required-features = ["triomphe"] [package.metadata.docs.rs] features = ["triomphe", "serde"] archery-1.2.2/LICENSE.md000064400000000000000000000020541046102023000126700ustar 00000000000000MIT License Copyright (c) 2025 Diogo Sousa Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. archery-1.2.2/README.md000064400000000000000000000122251046102023000125440ustar 00000000000000[![Build Status](https://github.com/orium/archery/workflows/CI/badge.svg)](https://github.com/orium/archery/actions?query=workflow%3ACI) [![Code Coverage](https://codecov.io/gh/orium/archery/branch/main/graph/badge.svg)](https://codecov.io/gh/orium/archery) [![Dependency status](https://deps.rs/repo/github/orium/archery/status.svg)](https://deps.rs/repo/github/orium/archery) [![crates.io](https://img.shields.io/crates/v/archery.svg)](https://crates.io/crates/archery) [![Downloads](https://img.shields.io/crates/d/archery.svg)](https://crates.io/crates/archery) [![Github stars](https://img.shields.io/github/stars/orium/archery.svg?logo=github)](https://github.com/orium/archery/stargazers) [![Documentation](https://docs.rs/archery/badge.svg)](https://docs.rs/archery/) [![License](https://img.shields.io/crates/l/archery.svg)](./LICENSE.md) # `archery` `archery` is a rust library that offers a way to abstraction over [`Rc`](https://doc.rust-lang.org/stable/alloc/rc/struct.Rc.html) and [`Arc`](https://doc.rust-lang.org/stable/alloc/sync/struct.Arc.html) smart pointers. This allows you to create data structures where the pointer type is parameterizable, so you can [avoid the overhead of `Arc`](https://doc.rust-lang.org/stable/alloc/sync/struct.Arc.html#thread-safety) when you don’t need to share data across threads. In languages that supports [higher-kinded polymorphism](https://en.wikipedia.org/wiki/Type_class#Higher-kinded_polymorphism) this would be simple to achieve without any library, but [rust does not support that yet](https://github.com/rust-lang/rfcs/issues/324). To mimic higher-kinded polymorphism `archery` implements the approach suggested by Joshua Liebow-Feeser in “[Rust has higher kinded types already… sort of](https://joshlf.com/post/2018/10/18/rust-higher-kinded-types-already/)”. While [other approaches](#alternative-approaches) exist, they seem to always offer poor ergonomics for the user. ## Setup To use `archery` add the following to your `Cargo.toml`: ```toml [dependencies] archery = "" ``` ## Using `archery` `archery` defines a [`SharedPointer`](https://docs.rs/archery/latest/archery/shared_pointer/struct.SharedPointer.html) that receives the [kind of pointer](https://docs.rs/archery/latest/archery/shared_pointer/kind/trait.SharedPointerKind.html) as a type parameter. This gives you a convenient and ergonomic way to abstract the pointer type away. ### Example Declare a data structure with the pointer kind as a type parameter bounded by [`SharedPointerKind`](https://docs.rs/archery/latest/archery/shared_pointer/kind/trait.SharedPointerKind.html): ```rust use archery::*; struct KeyValuePair { pub key: SharedPointer, pub value: SharedPointer, } impl KeyValuePair { fn new(key: K, value: V) -> KeyValuePair { KeyValuePair { key: SharedPointer::new(key), value: SharedPointer::new(value), } } } ``` To use it just plug-in the kind of pointer you want: ```rust let pair: KeyValuePair<_, _, RcK> = KeyValuePair::new("António Variações", 1944); assert_eq!(*pair.value, 1944); ``` ### `triomphe::Arc` You can also use [`triomphe::Arc`](https://docs.rs/triomphe/latest/triomphe/struct.Arc.html) as the backing implementation of a [`SharedPointer`](https://docs.rs/archery/latest/archery/shared_pointer/struct.SharedPointer.html). This is generally faster than [`std::sync::Arc`](https://doc.rust-lang.org/stable/alloc/sync/struct.Arc.html). Read [`triomphe`’s crate documentation](https://docs.rs/triomphe/latest/triomphe/) to learn more about it. To use it you need to enable the `triomphe` feature in `archery`. Use `ArcTK` as the pointer kind in [`SharedPointer`](https://docs.rs/archery/latest/archery/shared_pointer/struct.SharedPointer.html). ### Serialization We support serialization through [serde](https://crates.io/crates/serde). To use it enable the `serde` feature. To do so change the archery dependency in your `Cargo.toml` to ```toml [dependencies] archery = { version = "", features = ["serde"] } ``` ## Limitations Currently it is not possible to have unsized types inside a [`SharedPointer`](https://docs.rs/archery/latest/archery/shared_pointer/struct.SharedPointer.html). As a workaround you can put the unsized type inside a [`Box`](https://doc.rust-lang.org/stable/alloc/boxed/struct.Box.html). ## Alternative approaches An alternative to the approach taken by `archery` is to use traits with associated types to encode type-level functions. This has been suggested [multiple](https://github.com/orium/rpds/issues/7#issuecomment-362635901) [times](https://joshlf.com/post/2018/10/18/rust-higher-kinded-types-already/#comment-4160863400), but offers ugly ergonomics (see [here](https://github.com/Marwes/rpds/blob/e482d5abbaa6c876d7c624e497affe7299bbeece/src/sequence/vector/mod.rs#L153) and [here](https://github.com/Marwes/rpds/blob/e482d5abbaa6c876d7c624e497affe7299bbeece/src/sequence/vector/mod.rs#L249)). archery-1.2.2/release-notes.md000064400000000000000000000025411046102023000143550ustar 00000000000000# Release notes ## 1.2.2 * Relicensed project under the MIT license. ## 1.2.1 * No longer depend on `static_assertions`. ## 1.2.0 * Added support for serde. This is gated behind the `serde` feature. ## 1.1.0 * Added support for `triomphe::Arc` as a pointer kind. This gated behind the `triomphe` feature. ## 1.0.0 * No changes. It’s just time to commit to a stable release :). ## 0.5.0 * Fix `Send`/`Sync` unsoundness in `SharedPointer`. See issue [#18](https://github.com/orium/archery/issues/18). * Added `SharedPointer::pin()`. * Added `SharedPointer::as_ptr()`. * Updated dependencies. * Updated to 2021 edition. ## 0.4.0 * Added support for `no_std`. ## 0.3.0 * Renamed `SharedPointerKindRc` and `SharedPointerKindArc` to `RcK` and `ArcK`, respectively. ## 0.2.1 * Minor fix in README. ## 0.2.0 * Added some functionality to `SharedPointer` that you would expect from `Rc`/`Arc`. * Functions: * `SharedPointer::try_unwrap()`. * `SharedPointer::get_mut()`. * `SharedPointer::strong_count()`. * `SharedPointer::ptr_eq()`. * Traits: * `Default`. * `From`. * `From>`. * `std::fmt::Pointer`. ## 0.1.0 * Initial release with `SharedPointer`, `SharedPointerKind`, `SharedPointerKindRc`, and `SharedPointerKindArc`. * Functionality exposed from the underlying pointers: `deref()`, `make_mut()`, `clone()`. archery-1.2.2/src/lib.rs000064400000000000000000000120311046102023000131630ustar 00000000000000#![no_std] #![cfg_attr(feature = "fatal-warnings", deny(warnings))] // Note: If you change this remember to update `README.md`. To do so run `cargo rdme`. //! `archery` is a rust library that offers a way to abstraction over //! [`Rc`](::alloc::rc::Rc) and //! [`Arc`](::alloc::sync::Arc) smart pointers. //! This allows you to create data structures where the pointer type is parameterizable, so you can //! [avoid the overhead of `Arc`](::alloc::sync::Arc#thread-safety) //! when you don’t need to share data across threads. //! //! In languages that supports //! [higher-kinded polymorphism](https://en.wikipedia.org/wiki/Type_class#Higher-kinded_polymorphism) //! this would be simple to achieve without any library, but //! [rust does not support that yet](https://github.com/rust-lang/rfcs/issues/324). //! To mimic higher-kinded polymorphism `archery` implements the approach suggested by //! Joshua Liebow-Feeser in //! “[Rust has higher kinded types already… sort of](https://joshlf.com/post/2018/10/18/rust-higher-kinded-types-already/)”. //! While [other approaches](#alternative-approaches) exist, they seem to always offer poor //! ergonomics for the user. //! //! # Setup //! //! To use `archery` add the following to your `Cargo.toml`: //! //! ```toml //! [dependencies] //! archery = "" //! ``` //! //! # Using `archery` //! //! `archery` defines a [`SharedPointer`](crate::shared_pointer::SharedPointer) //! that receives the [kind of pointer](crate::shared_pointer::kind::SharedPointerKind) //! as a type parameter. This gives you a convenient and ergonomic way to abstract the pointer //! type away. //! //! ## Example //! //! Declare a data structure with the pointer kind as a type parameter bounded by //! [`SharedPointerKind`](crate::shared_pointer::kind::SharedPointerKind): //! //! ```rust //! use archery::*; //! //! struct KeyValuePair { //! pub key: SharedPointer, //! pub value: SharedPointer, //! } //! //! impl KeyValuePair { //! fn new(key: K, value: V) -> KeyValuePair { //! KeyValuePair { //! key: SharedPointer::new(key), //! value: SharedPointer::new(value), //! } //! } //! } //! ``` //! //! To use it just plug-in the kind of pointer you want: //! //! ```rust //! # use archery::*; //! # //! # struct KeyValuePair { //! # pub key: SharedPointer, //! # pub value: SharedPointer, //! # } //! # //! # impl KeyValuePair { //! # fn new(key: K, value: V) -> KeyValuePair { //! # KeyValuePair { //! # key: SharedPointer::new(key), //! # value: SharedPointer::new(value), //! # } //! # } //! # } //! # //! let pair: KeyValuePair<_, _, RcK> = //! KeyValuePair::new("António Variações", 1944); //! //! assert_eq!(*pair.value, 1944); //! ``` //! //! ## `triomphe::Arc` //! //! You can also use [`triomphe::Arc`](https://docs.rs/triomphe/latest/triomphe/struct.Arc.html) //! as the backing implementation of a [`SharedPointer`](crate::shared_pointer::SharedPointer). //! This is generally faster than [`std::sync::Arc`](::alloc::sync::Arc). //! Read [`triomphe`’s crate documentation](https://docs.rs/triomphe/latest/triomphe/) to learn more //! about it. //! //! To use it you need to enable the `triomphe` feature in `archery`. Use `ArcTK` as the pointer //! kind in [`SharedPointer`](crate::shared_pointer::SharedPointer). //! //! ## Serialization //! //! We support serialization through [serde](https://crates.io/crates/serde). To use it //! enable the `serde` feature. To do so change the archery dependency in your `Cargo.toml` to //! //! ```toml //! [dependencies] //! archery = { version = "", features = ["serde"] } //! ``` //! # Limitations //! //! Currently it is not possible to have unsized types inside a //! [`SharedPointer`](crate::shared_pointer::SharedPointer). As a workaround you can put the //! unsized type inside a [`Box`](::alloc::boxed::Box). //! //! # Alternative approaches //! //! An alternative to the approach taken by `archery` is to use traits with associated types to encode //! type-level functions. This has been suggested //! [multiple](https://github.com/orium/rpds/issues/7#issuecomment-362635901) //! [times](https://joshlf.com/post/2018/10/18/rust-higher-kinded-types-already/#comment-4160863400), //! but offers ugly ergonomics (see //! [here](https://github.com/Marwes/rpds/blob/e482d5abbaa6c876d7c624e497affe7299bbeece/src/sequence/vector/mod.rs#L153) //! and [here](https://github.com/Marwes/rpds/blob/e482d5abbaa6c876d7c624e497affe7299bbeece/src/sequence/vector/mod.rs#L249)). extern crate alloc; #[cfg(test)] #[macro_use] extern crate std; pub mod shared_pointer; pub use shared_pointer::SharedPointer; pub use shared_pointer::kind::SharedPointerKind; #[doc(no_inline)] pub use shared_pointer::kind::ArcK; #[cfg(feature = "triomphe")] #[doc(no_inline)] pub use shared_pointer::kind::ArcTK; #[doc(no_inline)] pub use shared_pointer::kind::RcK; archery-1.2.2/src/shared_pointer/kind/arc/mod.rs000064400000000000000000000070051046102023000177010ustar 00000000000000use crate::shared_pointer::kind::SharedPointerKind; use alloc::boxed::Box; use alloc::sync::Arc; use core::fmt; use core::fmt::Debug; use core::fmt::Formatter; use core::mem; use core::mem::ManuallyDrop; use core::ops::Deref; use core::ops::DerefMut; use core::ptr; type UntypedArc = Arc<()>; /// [Type constructors](https://en.wikipedia.org/wiki/Type_constructor) for /// [`Arc`] pointers. pub struct ArcK { /// We use [`ManuallyDrop`] here, so that we can drop it explicitly as /// [`Arc`](alloc::sync::Arc). Not sure if it can be dropped as [`UntypedArc`], but it /// seems to be playing with fire (even more than we already are). inner: ManuallyDrop, } impl ArcK { #[inline(always)] fn new_from_inner(arc: Arc) -> ArcK { ArcK { inner: ManuallyDrop::new(unsafe { mem::transmute::, UntypedArc>(arc) }) } } #[inline(always)] unsafe fn take_inner(self) -> Arc { unsafe { let arc: UntypedArc = ManuallyDrop::into_inner(self.inner); mem::transmute(arc) } } #[inline(always)] unsafe fn as_inner_ref(&self) -> &Arc { unsafe { let arc_t: *const Arc = ptr::from_ref::(self.inner.deref()).cast::>(); // Static check to make sure we are not messing up the sizes. // This could happen if we allowed for `T` to be unsized, because it would need to be // represented as a wide pointer inside `Arc`. // TODO Use static_assertion when https://github.com/nvzqz/static-assertions-rs/issues/21 // gets fixed let _ = mem::transmute::>; &*arc_t } } #[inline(always)] unsafe fn as_inner_mut(&mut self) -> &mut Arc { unsafe { let arc_t: *mut Arc = ptr::from_mut::(self.inner.deref_mut()).cast::>(); &mut *arc_t } } } unsafe impl SharedPointerKind for ArcK { #[inline(always)] fn new(v: T) -> ArcK { ArcK::new_from_inner(Arc::new(v)) } #[inline(always)] fn from_box(v: Box) -> ArcK { ArcK::new_from_inner::(Arc::from(v)) } #[inline(always)] unsafe fn as_ptr(&self) -> *const T { unsafe { Arc::as_ptr(self.as_inner_ref()) } } #[inline(always)] unsafe fn deref(&self) -> &T { unsafe { self.as_inner_ref::().as_ref() } } #[inline(always)] unsafe fn try_unwrap(self) -> Result { unsafe { Arc::try_unwrap(self.take_inner()).map_err(ArcK::new_from_inner) } } #[inline(always)] unsafe fn get_mut(&mut self) -> Option<&mut T> { unsafe { Arc::get_mut(self.as_inner_mut()) } } #[inline(always)] unsafe fn make_mut(&mut self) -> &mut T { unsafe { Arc::make_mut(self.as_inner_mut()) } } #[inline(always)] unsafe fn strong_count(&self) -> usize { unsafe { Arc::strong_count(self.as_inner_ref::()) } } #[inline(always)] unsafe fn clone(&self) -> ArcK { unsafe { ArcK { inner: ManuallyDrop::new(Arc::clone(self.as_inner_ref())) } } } #[inline(always)] unsafe fn drop(&mut self) { unsafe { ptr::drop_in_place::>(self.as_inner_mut()); } } } impl Debug for ArcK { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { f.write_str("ArcK") } } #[cfg(test)] mod test; archery-1.2.2/src/shared_pointer/kind/arc/test.rs000064400000000000000000000102421046102023000200760ustar 00000000000000use super::*; use pretty_assertions::assert_eq; use static_assertions::assert_impl_all; use std::cell::Cell; use std::string::ToString; type PointerKind = ArcK; assert_impl_all!(ArcK: Send, Sync); #[test] fn test_from_box_t() { let mut ptr = PointerKind::from_box(Box::new(42)); unsafe { assert_eq!(ptr.deref::(), &42); ptr.drop::(); } } #[test] fn test_as_ptr() { let mut x = PointerKind::new::<&'static str>("hello"); unsafe { let mut y = PointerKind::clone::<&'static str>(&x); let x_ptr: *const &'static str = PointerKind::as_ptr(&x); assert_eq!(x_ptr, PointerKind::as_ptr(&y)); assert_eq!(*x_ptr, "hello"); x.drop::<&'static str>(); y.drop::<&'static str>(); } } #[test] fn test_deref() { let mut ptr_42 = PointerKind::new::(42); let mut ptr_box_dyn_hello = PointerKind::new::>(Box::new("hello")); unsafe { assert_eq!(ptr_42.deref::(), &42); assert_eq!(ptr_box_dyn_hello.deref::>().to_string(), "hello"); ptr_42.drop::(); ptr_box_dyn_hello.drop::>(); } } #[test] fn test_try_unwrap() { let ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.try_unwrap::().unwrap(), 42); } let ptr = PointerKind::new::(42); unsafe { let ptr_clone = ptr.clone::(); let mut ptr_clone = ptr_clone.try_unwrap::().unwrap_err(); let mut ptr = ptr.try_unwrap::().unwrap_err(); assert_eq!(ptr.deref::(), &42); assert_eq!(ptr_clone.deref::(), &42); ptr.drop::(); ptr_clone.drop::(); } } #[test] fn test_get_mut() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.deref::(), &42); *ptr.get_mut::().unwrap() += 1; assert_eq!(ptr.deref::(), &43); let mut ptr_clone = ptr.clone::(); assert_eq!(ptr.get_mut::(), None); assert_eq!(ptr_clone.get_mut::(), None); ptr.drop::(); *ptr_clone.get_mut::().unwrap() += 1; assert_eq!(ptr_clone.deref::(), &44); ptr_clone.drop::(); } } #[test] fn test_make_mut() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.deref::(), &42); *ptr.make_mut::() += 1; assert_eq!(ptr.deref::(), &43); // Clone to force make_mut to clone the data. let mut ptr_clone = ptr.clone::(); assert_eq!(ptr_clone.deref::(), &43); *ptr_clone.make_mut::() += 1; assert_eq!(ptr.deref::(), &43); assert_eq!(ptr_clone.deref::(), &44); *ptr.make_mut::() *= 2; assert_eq!(ptr.deref::(), &(2 * 43)); assert_eq!(ptr_clone.deref::(), &44); ptr.drop::(); assert_eq!(ptr_clone.deref::(), &44); ptr_clone.drop::(); } } #[test] fn test_strong_count() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.strong_count::(), 1); let mut ptr_clone = ptr.clone::(); assert_eq!(ptr.strong_count::(), 2); assert_eq!(ptr_clone.strong_count::(), 2); ptr.drop::(); assert_eq!(ptr_clone.strong_count::(), 1); ptr_clone.drop::(); } } #[test] fn test_clone() { let mut ptr = PointerKind::new::>(Cell::new(42)); unsafe { let mut ptr_clone = ptr.clone::>(); assert_eq!(ptr.deref::>().get(), 42); assert_eq!(ptr_clone.deref::>().get(), 42); ptr_clone.deref::>().set(3); assert_eq!(ptr.deref::>().get(), 3); assert_eq!(ptr_clone.deref::>().get(), 3); ptr.drop::>(); assert_eq!(ptr_clone.deref::>().get(), 3); ptr_clone.drop::>(); } } #[test] fn test_debug() { let mut ptr = PointerKind::new::(42); assert_eq!(format!("{:?}", ptr), "ArcK"); unsafe { ptr.drop::(); } } archery-1.2.2/src/shared_pointer/kind/arct/mod.rs000064400000000000000000000066541046102023000200760ustar 00000000000000use crate::shared_pointer::kind::SharedPointerKind; use alloc::boxed::Box; use core::fmt; use core::fmt::Debug; use core::fmt::Formatter; use core::mem; use core::mem::ManuallyDrop; use core::ops::Deref; use core::ops::DerefMut; use core::ptr; use triomphe::Arc; type UntypedArc = Arc<()>; /// [Type constructors](https://en.wikipedia.org/wiki/Type_constructor) for /// [`triomphe::Arc`](triomphe::Arc) pointers. pub struct ArcTK { /// We use [`ManuallyDrop`] here, so that we can drop it explicitly as /// [`Arc`](triomphe::Arc). Not sure if it can be dropped as [`UntypedArc`], but it /// seems to be playing with fire (even more than we already are). inner: ManuallyDrop, } impl ArcTK { #[inline(always)] fn new_from_inner(arc: Arc) -> ArcTK { ArcTK { inner: ManuallyDrop::new(unsafe { mem::transmute::, UntypedArc>(arc) }) } } #[inline(always)] unsafe fn take_inner(self) -> Arc { let arc: UntypedArc = ManuallyDrop::into_inner(self.inner); unsafe { mem::transmute(arc) } } #[inline(always)] unsafe fn as_inner_ref(&self) -> &Arc { let arc_t: *const Arc = ptr::from_ref::(self.inner.deref()).cast::>(); // Static check to make sure we are not messing up the sizes. // This could happen if we allowed for `T` to be unsized, because it would need to be // represented as a wide pointer inside `Arc`. // TODO Use static_assertion when https://github.com/nvzqz/static-assertions-rs/issues/21 // gets fixed let _ = mem::transmute::>; unsafe { &*arc_t } } #[inline(always)] unsafe fn as_inner_mut(&mut self) -> &mut Arc { let arc_t: *mut Arc = ptr::from_mut::(self.inner.deref_mut()).cast::>(); unsafe { &mut *arc_t } } } unsafe impl SharedPointerKind for ArcTK { #[inline(always)] fn new(v: T) -> ArcTK { ArcTK::new_from_inner(Arc::new(v)) } #[inline(always)] fn from_box(v: Box) -> ArcTK { ArcTK::new_from_inner::(Arc::from(v)) } #[inline(always)] unsafe fn as_ptr(&self) -> *const T { unsafe { Arc::as_ptr(self.as_inner_ref()) } } #[inline(always)] unsafe fn deref(&self) -> &T { unsafe { self.as_inner_ref::().as_ref() } } #[inline(always)] unsafe fn try_unwrap(self) -> Result { unsafe { Arc::try_unwrap(self.take_inner()).map_err(ArcTK::new_from_inner) } } #[inline(always)] unsafe fn get_mut(&mut self) -> Option<&mut T> { unsafe { Arc::get_mut(self.as_inner_mut()) } } #[inline(always)] unsafe fn make_mut(&mut self) -> &mut T { unsafe { Arc::make_mut(self.as_inner_mut()) } } #[inline(always)] unsafe fn strong_count(&self) -> usize { unsafe { Arc::count(self.as_inner_ref::()) } } #[inline(always)] unsafe fn clone(&self) -> ArcTK { unsafe { ArcTK { inner: ManuallyDrop::new(Arc::clone(self.as_inner_ref())) } } } #[inline(always)] unsafe fn drop(&mut self) { unsafe { ptr::drop_in_place::>(self.as_inner_mut()); } } } impl Debug for ArcTK { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { f.write_str("ArcTK") } } #[cfg(test)] mod test; archery-1.2.2/src/shared_pointer/kind/arct/test.rs000064400000000000000000000102451046102023000202650ustar 00000000000000use super::*; use pretty_assertions::assert_eq; use static_assertions::assert_impl_all; use std::cell::Cell; use std::string::ToString; type PointerKind = ArcTK; assert_impl_all!(ArcTK: Send, Sync); #[test] fn test_from_box_t() { let mut ptr = PointerKind::from_box(Box::new(42)); unsafe { assert_eq!(ptr.deref::(), &42); ptr.drop::(); } } #[test] fn test_as_ptr() { let mut x = PointerKind::new::<&'static str>("hello"); unsafe { let mut y = PointerKind::clone::<&'static str>(&x); let x_ptr: *const &'static str = PointerKind::as_ptr(&x); assert_eq!(x_ptr, PointerKind::as_ptr(&y)); assert_eq!(*x_ptr, "hello"); x.drop::<&'static str>(); y.drop::<&'static str>(); } } #[test] fn test_deref() { let mut ptr_42 = PointerKind::new::(42); let mut ptr_box_dyn_hello = PointerKind::new::>(Box::new("hello")); unsafe { assert_eq!(ptr_42.deref::(), &42); assert_eq!(ptr_box_dyn_hello.deref::>().to_string(), "hello"); ptr_42.drop::(); ptr_box_dyn_hello.drop::>(); } } #[test] fn test_try_unwrap() { let ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.try_unwrap::().unwrap(), 42); } let ptr = PointerKind::new::(42); unsafe { let ptr_clone = ptr.clone::(); let mut ptr_clone = ptr_clone.try_unwrap::().unwrap_err(); let mut ptr = ptr.try_unwrap::().unwrap_err(); assert_eq!(ptr.deref::(), &42); assert_eq!(ptr_clone.deref::(), &42); ptr.drop::(); ptr_clone.drop::(); } } #[test] fn test_get_mut() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.deref::(), &42); *ptr.get_mut::().unwrap() += 1; assert_eq!(ptr.deref::(), &43); let mut ptr_clone = ptr.clone::(); assert_eq!(ptr.get_mut::(), None); assert_eq!(ptr_clone.get_mut::(), None); ptr.drop::(); *ptr_clone.get_mut::().unwrap() += 1; assert_eq!(ptr_clone.deref::(), &44); ptr_clone.drop::(); } } #[test] fn test_make_mut() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.deref::(), &42); *ptr.make_mut::() += 1; assert_eq!(ptr.deref::(), &43); // Clone to force make_mut to clone the data. let mut ptr_clone = ptr.clone::(); assert_eq!(ptr_clone.deref::(), &43); *ptr_clone.make_mut::() += 1; assert_eq!(ptr.deref::(), &43); assert_eq!(ptr_clone.deref::(), &44); *ptr.make_mut::() *= 2; assert_eq!(ptr.deref::(), &(2 * 43)); assert_eq!(ptr_clone.deref::(), &44); ptr.drop::(); assert_eq!(ptr_clone.deref::(), &44); ptr_clone.drop::(); } } #[test] fn test_strong_count() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.strong_count::(), 1); let mut ptr_clone = ptr.clone::(); assert_eq!(ptr.strong_count::(), 2); assert_eq!(ptr_clone.strong_count::(), 2); ptr.drop::(); assert_eq!(ptr_clone.strong_count::(), 1); ptr_clone.drop::(); } } #[test] fn test_clone() { let mut ptr = PointerKind::new::>(Cell::new(42)); unsafe { let mut ptr_clone = ptr.clone::>(); assert_eq!(ptr.deref::>().get(), 42); assert_eq!(ptr_clone.deref::>().get(), 42); ptr_clone.deref::>().set(3); assert_eq!(ptr.deref::>().get(), 3); assert_eq!(ptr_clone.deref::>().get(), 3); ptr.drop::>(); assert_eq!(ptr_clone.deref::>().get(), 3); ptr_clone.drop::>(); } } #[test] fn test_debug() { let mut ptr = PointerKind::new::(42); assert_eq!(format!("{:?}", ptr), "ArcTK"); unsafe { ptr.drop::(); } } archery-1.2.2/src/shared_pointer/kind/mod.rs000064400000000000000000000043001046102023000171270ustar 00000000000000use core::fmt::Debug; /// Trait for [type constructors](https://en.wikipedia.org/wiki/Type_constructor) of /// reference-counting pointers. /// /// # Safety /// /// `T` may be `!`[`Unpin`], and [`SharedPointer`][1] may be held in a pinned /// form ([`Pin`][2]`>`). /// As such, the implementation of this trait must uphold the pinning invariants /// for `T` while it's held in `Self`. Specifically, this necessitates the /// following: /// /// - `&mut T` is only exposed through the trait methods returning `&mut T`. /// /// - The implementor must not move out the contained `T` unless the semantics /// of trait methods demands that. /// /// - [`Self::drop`] drops `T` in place. /// /// [1]: crate::shared_pointer::SharedPointer /// [2]: core::pin::Pin // // There are two conditions for types implementing this trait to be used in a safe way: // // 1. Always use the correct type `T`. // 2. Make sure that you use it wrapped in something that derives the correct auto-traits taking // into account the type of `T`. // // To elaborate on point 2: a `ArcK` will always implement `Send + Sync`, but that // is only safe if the actually type that `ArcK` holds is in fact `Send + Sync`. // This means that a safe wrapper around this type must make sure it does not implement // `Send + Sync` unless `T: Send + Sync`. This is holds true for `SharedPointer` since it has a // phantom field with `T`, thus the compiler will only make `SharedPointer` implement // `Send + Sync` if `T: Send + Sync`. pub unsafe trait SharedPointerKind: Sized + Debug { fn new(v: T) -> Self; fn from_box(v: Box) -> Self; unsafe fn as_ptr(&self) -> *const T; unsafe fn deref(&self) -> &T; unsafe fn try_unwrap(self) -> Result; unsafe fn get_mut(&mut self) -> Option<&mut T>; unsafe fn make_mut(&mut self) -> &mut T; unsafe fn strong_count(&self) -> usize; #[must_use] unsafe fn clone(&self) -> Self; unsafe fn drop(&mut self); } mod arc; #[cfg(feature = "triomphe")] mod arct; mod rc; use alloc::boxed::Box; #[doc(inline)] pub use arc::ArcK; #[cfg(feature = "triomphe")] #[doc(inline)] pub use arct::ArcTK; #[doc(inline)] pub use rc::RcK; archery-1.2.2/src/shared_pointer/kind/rc/mod.rs000064400000000000000000000066701046102023000175470ustar 00000000000000use crate::shared_pointer::kind::SharedPointerKind; use alloc::boxed::Box; use alloc::rc::Rc; use core::fmt; use core::fmt::Debug; use core::fmt::Formatter; use core::mem; use core::mem::ManuallyDrop; use core::ops::Deref; use core::ops::DerefMut; use core::ptr; type UntypedRc = Rc<()>; /// [Type constructors](https://en.wikipedia.org/wiki/Type_constructor) for /// [`Rc`] pointers. pub struct RcK { /// We use [`ManuallyDrop`] here, so that we can drop it explicitly as [`Rc`](alloc::rc::Rc). /// Not sure if it can be dropped as [`UntypedRc`], but it seems to be playing with fire (even /// more than we already are). inner: ManuallyDrop, } impl RcK { #[inline(always)] fn new_from_inner(rc: Rc) -> RcK { RcK { inner: ManuallyDrop::new(unsafe { mem::transmute::, UntypedRc>(rc) }) } } #[inline(always)] unsafe fn take_inner(self) -> Rc { unsafe { let rc: UntypedRc = ManuallyDrop::into_inner(self.inner); mem::transmute(rc) } } #[inline(always)] unsafe fn as_inner_ref(&self) -> &Rc { unsafe { let rc_t: *const Rc = ptr::from_ref::(self.inner.deref()).cast::>(); // Static check to make sure we are not messing up the sizes. // This could happen if we allowed for `T` to be unsized, because it would need to be // represented as a wide pointer inside `Rc`. // TODO Use static_assertion when https://github.com/nvzqz/static-assertions-rs/issues/21 // gets fixed let _ = mem::transmute::>; &*rc_t } } #[inline(always)] unsafe fn as_inner_mut(&mut self) -> &mut Rc { unsafe { let rc_t: *mut Rc = ptr::from_mut::(self.inner.deref_mut()).cast::>(); &mut *rc_t } } } unsafe impl SharedPointerKind for RcK { #[inline(always)] fn new(v: T) -> RcK { RcK::new_from_inner(Rc::new(v)) } #[inline(always)] fn from_box(v: Box) -> RcK { RcK::new_from_inner::(Rc::from(v)) } #[inline(always)] unsafe fn as_ptr(&self) -> *const T { unsafe { Rc::as_ptr(self.as_inner_ref()) } } #[inline(always)] unsafe fn deref(&self) -> &T { unsafe { self.as_inner_ref::().as_ref() } } #[inline(always)] unsafe fn try_unwrap(self) -> Result { unsafe { Rc::try_unwrap(self.take_inner()).map_err(RcK::new_from_inner) } } #[inline(always)] unsafe fn get_mut(&mut self) -> Option<&mut T> { unsafe { Rc::get_mut(self.as_inner_mut()) } } #[inline(always)] unsafe fn make_mut(&mut self) -> &mut T { unsafe { Rc::make_mut(self.as_inner_mut()) } } #[inline(always)] unsafe fn strong_count(&self) -> usize { unsafe { Rc::strong_count(self.as_inner_ref::()) } } #[inline(always)] unsafe fn clone(&self) -> RcK { unsafe { RcK { inner: ManuallyDrop::new(Rc::clone(self.as_inner_ref())) } } } #[inline(always)] unsafe fn drop(&mut self) { unsafe { ptr::drop_in_place::>(self.as_inner_mut()); } } } impl Debug for RcK { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { f.write_str("RcK") } } #[cfg(test)] mod test; archery-1.2.2/src/shared_pointer/kind/rc/test.rs000064400000000000000000000102331046102023000177350ustar 00000000000000use super::*; use std::cell::Cell; use std::string::ToString; type PointerKind = RcK; #[test] fn test_from_box_t() { let mut ptr = PointerKind::from_box(Box::new(42)); unsafe { assert_eq!(ptr.deref::(), &42); ptr.drop::(); } } #[test] fn test_as_ptr() { let mut x = PointerKind::new::<&'static str>("hello from test_as_ptr"); unsafe { let mut y = PointerKind::clone::<&'static str>(&x); let x_ptr: *const &'static str = PointerKind::as_ptr(&x); assert_eq!(x_ptr, PointerKind::as_ptr(&y)); assert_eq!(*x_ptr, "hello from test_as_ptr"); x.drop::<&'static str>(); y.drop::<&'static str>(); } } #[test] fn test_deref() { let mut ptr_42 = PointerKind::new::(42); let mut ptr_box_dyn_hello = PointerKind::new::>(Box::new("hello from test_deref")); unsafe { assert_eq!(ptr_42.deref::(), &42); assert_eq!( ptr_box_dyn_hello.deref::>().to_string(), "hello from test_deref" ); ptr_42.drop::(); ptr_box_dyn_hello.drop::>(); } } #[test] fn test_try_unwrap() { let ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.try_unwrap::().unwrap(), 42); } let ptr = PointerKind::new::(42); unsafe { let ptr_clone = ptr.clone::(); let mut ptr_clone = ptr_clone.try_unwrap::().unwrap_err(); let mut ptr = ptr.try_unwrap::().unwrap_err(); assert_eq!(ptr.deref::(), &42); assert_eq!(ptr_clone.deref::(), &42); ptr.drop::(); ptr_clone.drop::(); } } #[test] fn test_get_mut() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.deref::(), &42); *ptr.get_mut::().unwrap() += 1; assert_eq!(ptr.deref::(), &43); let mut ptr_clone = ptr.clone::(); assert_eq!(ptr.get_mut::(), None); assert_eq!(ptr_clone.get_mut::(), None); ptr.drop::(); *ptr_clone.get_mut::().unwrap() += 1; assert_eq!(ptr_clone.deref::(), &44); ptr_clone.drop::(); } } #[test] fn test_make_mut() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.deref::(), &42); *ptr.make_mut::() += 1; assert_eq!(ptr.deref::(), &43); // Clone to force make_mut to clone the data. let mut ptr_clone = ptr.clone::(); assert_eq!(ptr_clone.deref::(), &43); *ptr_clone.make_mut::() += 1; assert_eq!(ptr.deref::(), &43); assert_eq!(ptr_clone.deref::(), &44); *ptr.make_mut::() *= 2; assert_eq!(ptr.deref::(), &(2 * 43)); assert_eq!(ptr_clone.deref::(), &44); ptr.drop::(); assert_eq!(ptr_clone.deref::(), &44); ptr_clone.drop::(); } } #[test] fn test_strong_count() { let mut ptr = PointerKind::new::(42); unsafe { assert_eq!(ptr.strong_count::(), 1); let mut ptr_clone = ptr.clone::(); assert_eq!(ptr.strong_count::(), 2); assert_eq!(ptr_clone.strong_count::(), 2); ptr.drop::(); assert_eq!(ptr_clone.strong_count::(), 1); ptr_clone.drop::(); } } #[test] fn test_clone() { let mut ptr = PointerKind::new::>(Cell::new(42)); unsafe { let mut ptr_clone = ptr.clone::>(); assert_eq!(ptr.deref::>().get(), 42); assert_eq!(ptr_clone.deref::>().get(), 42); ptr_clone.deref::>().set(3); assert_eq!(ptr.deref::>().get(), 3); assert_eq!(ptr_clone.deref::>().get(), 3); ptr.drop::>(); assert_eq!(ptr_clone.deref::>().get(), 3); ptr_clone.drop::>(); } } #[test] fn test_debug() { let mut ptr = PointerKind::new::(42); assert_eq!(format!("{ptr:?}"), "RcK"); unsafe { ptr.drop::(); } } archery-1.2.2/src/shared_pointer/mod.rs000064400000000000000000000215671046102023000162200ustar 00000000000000use crate::shared_pointer::kind::SharedPointerKind; use alloc::boxed::Box; use core::borrow::Borrow; use core::cmp::Ordering; use core::fmt; use core::fmt::Debug; use core::fmt::Display; use core::fmt::Formatter; use core::hash::Hash; use core::hash::Hasher; use core::marker::PhantomData; use core::mem; use core::mem::ManuallyDrop; use core::ops::Deref; use core::pin::Pin; use core::ptr; /// Pointer to shared data with reference-counting. /// /// The type parameter `P` is a [type constructor](https://en.wikipedia.org/wiki/Type_constructor) /// of the underlying pointer type, offering a way to abstraction over [`Rc`](alloc::rc::Rc) and /// [`Arc`](alloc::sync::Arc) smart pointers. /// This allows you to create data structures where the pointer type is parameterizable, so you can /// [avoid the overhead of `Arc`](alloc::sync::Arc#thread-safety) /// when you don’t need to share data across threads. /// /// # Example /// /// Declare a data structure with the pointer kind as a type parameter bounded by /// [`SharedPointerKind`]: /// /// ```rust /// use archery::*; /// /// struct KeyValuePair { /// pub key: SharedPointer, /// pub value: SharedPointer, /// } /// /// impl KeyValuePair { /// fn new(key: K, value: V) -> KeyValuePair { /// KeyValuePair { /// key: SharedPointer::new(key), /// value: SharedPointer::new(value), /// } /// } /// } /// ``` /// /// To use it just plug-in the kind of pointer you want: /// /// ```rust /// # use archery::*; /// # /// # struct KeyValuePair { /// # pub key: SharedPointer, /// # pub value: SharedPointer, /// # } /// # /// # impl KeyValuePair { /// # fn new(key: K, value: V) -> KeyValuePair { /// # KeyValuePair { /// # key: SharedPointer::new(key), /// # value: SharedPointer::new(value), /// # } /// # } /// # } /// # /// let pair: KeyValuePair<_, _, RcK> = /// KeyValuePair::new("António Variações", 1944); /// /// assert_eq!(*pair.value, 1944); /// ``` pub struct SharedPointer where P: SharedPointerKind, { ptr: ManuallyDrop

, _phantom_t: PhantomData, _phantom_no_send_sync: PhantomData<*mut ()>, } unsafe impl Send for SharedPointer where P: SharedPointerKind {} unsafe impl Sync for SharedPointer where P: SharedPointerKind {} impl Unpin for SharedPointer where P: SharedPointerKind {} impl SharedPointer where P: SharedPointerKind, { #[inline(always)] fn new_from_inner(ptr: P) -> SharedPointer { SharedPointer { ptr: ManuallyDrop::new(ptr), _phantom_t: PhantomData, _phantom_no_send_sync: PhantomData, } } #[inline(always)] pub fn new(v: T) -> SharedPointer { SharedPointer::new_from_inner(P::new::(v)) } #[inline(always)] pub fn pin(v: T) -> Pin> { unsafe { Pin::new_unchecked(Self::new(v)) } } #[inline(always)] pub fn as_ptr(this: &Self) -> *const T { unsafe { this.ptr.as_ptr::() } } #[inline(always)] pub fn try_unwrap(mut this: SharedPointer) -> Result> { let ptr: P = unsafe { ManuallyDrop::take(&mut this.ptr) }; mem::forget(this); unsafe { ptr.try_unwrap::() }.map_err(SharedPointer::new_from_inner) } #[inline(always)] pub fn get_mut(this: &mut SharedPointer) -> Option<&mut T> { unsafe { this.ptr.get_mut::() } } #[inline(always)] pub fn strong_count(this: &Self) -> usize { unsafe { this.ptr.strong_count::() } } #[inline(always)] pub fn ptr_eq( this: &SharedPointer, other: &SharedPointer, ) -> bool { ptr::eq(this.deref(), other.deref()) } } impl SharedPointer where T: Clone, P: SharedPointerKind, { #[inline(always)] pub fn make_mut(this: &mut SharedPointer) -> &mut T { unsafe { this.ptr.make_mut::() } } } impl Default for SharedPointer where T: Default, P: SharedPointerKind, { #[inline(always)] fn default() -> SharedPointer { SharedPointer::new(Default::default()) } } impl Deref for SharedPointer where P: SharedPointerKind, { type Target = T; #[inline(always)] fn deref(&self) -> &T { unsafe { self.ptr.deref().deref() } } } impl Borrow for SharedPointer where P: SharedPointerKind, { #[inline(always)] fn borrow(&self) -> &T { self.deref() } } impl AsRef for SharedPointer where P: SharedPointerKind, { #[inline(always)] fn as_ref(&self) -> &T { self.deref() } } impl Clone for SharedPointer where P: SharedPointerKind, { #[inline(always)] fn clone(&self) -> SharedPointer { SharedPointer::new_from_inner(unsafe { self.ptr.deref().clone::() }) } } impl Hash for SharedPointer where T: Hash, P: SharedPointerKind, { #[inline(always)] fn hash(&self, state: &mut H) { self.deref().hash(state); } } impl PartialEq> for SharedPointer where T: PartialEq, P: SharedPointerKind, PO: SharedPointerKind, { #[inline(always)] fn eq(&self, other: &SharedPointer) -> bool { self.deref().eq(other.deref()) } #[inline(always)] fn ne(&self, other: &SharedPointer) -> bool { self.deref().ne(other.deref()) } } impl Eq for SharedPointer where T: Eq, P: SharedPointerKind, { } impl PartialOrd> for SharedPointer where T: PartialOrd, P: SharedPointerKind, PO: SharedPointerKind, { #[inline(always)] fn partial_cmp(&self, other: &SharedPointer) -> Option { self.deref().partial_cmp(other.deref()) } #[inline(always)] fn lt(&self, other: &SharedPointer) -> bool { self.deref().lt(other.deref()) } #[inline(always)] fn le(&self, other: &SharedPointer) -> bool { self.deref().le(other.deref()) } #[inline(always)] fn gt(&self, other: &SharedPointer) -> bool { self.deref().gt(other.deref()) } #[inline(always)] fn ge(&self, other: &SharedPointer) -> bool { self.deref().ge(other.deref()) } } impl Ord for SharedPointer where T: Ord, P: SharedPointerKind, { #[inline(always)] fn cmp(&self, other: &SharedPointer) -> Ordering { self.deref().cmp(other.deref()) } } impl From for SharedPointer where P: SharedPointerKind, { #[inline(always)] fn from(other: T) -> SharedPointer { SharedPointer::new(other) } } impl From> for SharedPointer where P: SharedPointerKind, { #[inline(always)] fn from(v: Box) -> SharedPointer { SharedPointer::new_from_inner(P::from_box(v)) } } impl Debug for SharedPointer where T: Debug, P: SharedPointerKind, { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { Debug::fmt(self.deref(), f) } } impl fmt::Pointer for SharedPointer where P: SharedPointerKind, { #[inline(always)] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Pointer::fmt(&core::ptr::addr_of!(**self), f) } } impl Display for SharedPointer where T: Display, P: SharedPointerKind, { #[inline(always)] fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> { Display::fmt(self.deref(), f) } } impl Drop for SharedPointer where P: SharedPointerKind, { #[inline(always)] fn drop(&mut self) { unsafe { self.ptr.drop::(); } } } pub mod kind; #[cfg(feature = "serde")] pub mod serde { use super::*; use ::serde::de::{Deserialize, Deserializer}; use ::serde::ser::{Serialize, Serializer}; impl Serialize for SharedPointer where T: Serialize, P: SharedPointerKind, { fn serialize(&self, serializer: S) -> Result { self.as_ref().serialize(serializer) } } impl<'de, T, P> Deserialize<'de> for SharedPointer where T: Deserialize<'de>, P: SharedPointerKind, { fn deserialize>( deserializer: D, ) -> Result, D::Error> { T::deserialize(deserializer).map(SharedPointer::new) } } } #[cfg(test)] mod test; archery-1.2.2/src/shared_pointer/test.rs000064400000000000000000000235301046102023000164100ustar 00000000000000#![allow(clippy::eq_op)] use super::*; use kind::ArcK; use kind::RcK; use static_assertions::assert_impl_all; use std::cell::Cell; use std::mem; use std::string::ToString; assert_impl_all!(SharedPointer: Send, Sync); mod static_check_rc_is_not_send_nor_sync { use crate::{RcK, SharedPointer}; use static_assertions::*; assert_impl_all!(i32: Send, Sync); assert_not_impl_any!(SharedPointer: Send); assert_not_impl_any!(SharedPointer: Sync); } mod static_check_arc_of_non_send_is_not_send_nor_sync { use crate::{ArcK, SharedPointer}; use static_assertions::*; use std::sync::MutexGuard; assert_not_impl_any!(MutexGuard<'static, ()>: Send); assert_impl_all!(MutexGuard<'static, ()>: Sync); assert_not_impl_any!(SharedPointer, ArcK>: Send); assert_not_impl_any!(SharedPointer, ArcK>: Sync); } mod static_check_arc_of_non_sync_is_not_send_nor_sync { use crate::{ArcK, SharedPointer}; use static_assertions::*; use std::cell::Cell; assert_impl_all!(Cell<()>: Send); assert_not_impl_any!(Cell<()>: Sync); assert_not_impl_any!(SharedPointer, ArcK>: Send); assert_not_impl_any!(SharedPointer, ArcK>: Sync); } mod static_check_arc_of_non_send_nor_sync_is_not_send_nor_sync { use crate::{ArcK, SharedPointer}; use alloc::rc::Rc; use static_assertions::*; assert_not_impl_any!(Rc: Send); assert_not_impl_any!(Rc: Sync); assert_not_impl_any!(SharedPointer, ArcK>: Send); assert_not_impl_any!(SharedPointer, ArcK>: Sync); } #[test] fn test_as_ptr() { let x = SharedPointer::<&'static str, RcK>::new("hello"); let y = SharedPointer::clone(&x); let x_ptr: *const &'static str = SharedPointer::as_ptr(&x); assert_eq!(x_ptr, SharedPointer::as_ptr(&y)); assert_eq!(unsafe { *x_ptr }, "hello"); } #[test] fn test_deref() { let ptr_42: SharedPointer = SharedPointer::new(42); let ptr_box_dyn_hello: SharedPointer, RcK> = SharedPointer::new(Box::new("hello")); assert_eq!(*ptr_42, 42); assert_eq!(ptr_box_dyn_hello.to_string(), "hello"); assert_eq!(*Borrow::::borrow(&ptr_42), 42); assert_eq!(Borrow::>::borrow(&ptr_box_dyn_hello).to_string(), "hello"); assert_eq!(*ptr_42.as_ref(), 42); assert_eq!(ptr_box_dyn_hello.as_ref().to_string(), "hello"); } #[test] fn test_try_unwrap() { let ptr: SharedPointer<_, RcK> = SharedPointer::new(42); assert_eq!(SharedPointer::try_unwrap(ptr).unwrap(), 42); let ptr: SharedPointer<_, RcK> = SharedPointer::new(42); let ptr_clone = SharedPointer::clone(&ptr); let ptr_clone = SharedPointer::try_unwrap(ptr_clone).unwrap_err(); let ptr = SharedPointer::try_unwrap(ptr).unwrap_err(); assert_eq!(*ptr, 42); assert_eq!(*ptr_clone, 42); } #[test] fn test_get_mut() { let mut ptr: SharedPointer<_, RcK> = SharedPointer::new(42); assert_eq!(*ptr, 42); *SharedPointer::get_mut(&mut ptr).unwrap() += 1; assert_eq!(*ptr, 43); let mut ptr_clone = SharedPointer::clone(&ptr); assert_eq!(SharedPointer::get_mut(&mut ptr), None); assert_eq!(SharedPointer::get_mut(&mut ptr_clone), None); mem::drop(ptr); *SharedPointer::get_mut(&mut ptr_clone).unwrap() += 1; assert_eq!(*ptr_clone, 44); } #[test] fn test_strong_count() { let ptr: SharedPointer<_, RcK> = SharedPointer::new(42); assert_eq!(SharedPointer::strong_count(&ptr), 1); let ptr_clone = SharedPointer::clone(&ptr); assert_eq!(SharedPointer::strong_count(&ptr), 2); assert_eq!(SharedPointer::strong_count(&ptr_clone), 2); mem::drop(ptr); assert_eq!(SharedPointer::strong_count(&ptr_clone), 1); } #[test] fn test_ptr_eq() { let ptr: SharedPointer<_, RcK> = SharedPointer::new(42); let ptr_same_content: SharedPointer<_, RcK> = SharedPointer::new(42); let ptr_clone: SharedPointer<_, _> = SharedPointer::clone(&ptr); assert!(SharedPointer::ptr_eq(&ptr, &ptr)); assert!(!SharedPointer::ptr_eq(&ptr, &ptr_same_content)); assert!(SharedPointer::ptr_eq(&ptr, &ptr_clone)); } #[test] fn test_make_mut() { let mut ptr: SharedPointer<_, RcK> = SharedPointer::new(42); assert_eq!(*ptr, 42); *SharedPointer::make_mut(&mut ptr) += 1; assert_eq!(*ptr, 43); // Clone to force make_mut to clone the data. let mut ptr_clone = SharedPointer::clone(&ptr); assert_eq!(*ptr_clone, 43); *SharedPointer::make_mut(&mut ptr_clone) += 1; assert_eq!(*ptr, 43); assert_eq!(*ptr_clone, 44); *SharedPointer::make_mut(&mut ptr) *= 2; assert_eq!(*ptr, 2 * 43); assert_eq!(*ptr_clone, 44); mem::drop(ptr); assert_eq!(*ptr_clone, 44); } #[test] fn test_clone() { let ptr: SharedPointer<_, RcK> = SharedPointer::new(Cell::new(42)); let ptr_clone = SharedPointer::clone(&ptr); assert_eq!(ptr.get(), 42); assert_eq!(ptr_clone.get(), 42); ptr_clone.set(3); assert_eq!(ptr.get(), 3); assert_eq!(ptr_clone.get(), 3); mem::drop(ptr); assert_eq!(ptr_clone.get(), 3); } fn hash(pointer: &SharedPointer) -> u64 { let mut hasher = std::collections::hash_map::DefaultHasher::new(); pointer.hash(&mut hasher); hasher.finish() } #[test] fn test_hash() { let ptr_42: SharedPointer<_, RcK> = SharedPointer::new(42); let ptr_hello: SharedPointer<_, RcK> = SharedPointer::new("hello"); assert_eq!(hash(&ptr_42), hash(&SharedPointer::<_, RcK>::new(42))); assert_eq!(hash(&ptr_hello), hash(&SharedPointer::<_, RcK>::new("hello"))); } #[test] fn test_hash_pointer_kind_consistent() { let ptr_hello_rc: SharedPointer<_, RcK> = SharedPointer::new("hello"); let ptr_hello_arc: SharedPointer<_, ArcK> = SharedPointer::new("hello"); assert_eq!(hash(&ptr_hello_rc), hash(&ptr_hello_arc)); } #[allow(clippy::nonminimal_bool)] #[test] fn test_eq() { let ptr_22: SharedPointer<_, RcK> = SharedPointer::new(22); let ptr_42: SharedPointer<_, RcK> = SharedPointer::new(42); assert!(ptr_22 == SharedPointer::<_, RcK>::new(22)); assert!(ptr_22 == SharedPointer::<_, ArcK>::new(22)); assert!(ptr_22 == ptr_22); assert!(!(ptr_22 == SharedPointer::<_, RcK>::new(42))); assert!(!(ptr_22 == SharedPointer::<_, ArcK>::new(42))); assert!(!(ptr_22 == ptr_42)); assert!(ptr_22 != SharedPointer::<_, RcK>::new(42)); assert!(ptr_22 != SharedPointer::<_, ArcK>::new(42)); assert!(ptr_22 != ptr_42); assert!(!(ptr_22 != SharedPointer::<_, RcK>::new(22))); assert!(!(ptr_22 != SharedPointer::<_, ArcK>::new(22))); assert!(!(ptr_22 != ptr_22)); } #[allow(clippy::cognitive_complexity)] #[allow(clippy::nonminimal_bool)] #[test] fn test_ord() { let ptr_22: SharedPointer<_, RcK> = SharedPointer::new(22); let ptr_42: SharedPointer<_, RcK> = SharedPointer::new(42); assert_eq!(ptr_22.partial_cmp(&SharedPointer::<_, RcK>::new(22)), Some(Ordering::Equal)); assert_eq!(ptr_22.partial_cmp(&SharedPointer::<_, RcK>::new(42)), Some(Ordering::Less)); assert_eq!(ptr_42.partial_cmp(&SharedPointer::<_, RcK>::new(22)), Some(Ordering::Greater)); assert_eq!(ptr_22.cmp(&SharedPointer::<_, RcK>::new(22)), Ordering::Equal); assert_eq!(ptr_22.cmp(&SharedPointer::<_, RcK>::new(42)), Ordering::Less); assert_eq!(ptr_42.cmp(&SharedPointer::<_, RcK>::new(22)), Ordering::Greater); assert!(ptr_22 < SharedPointer::<_, RcK>::new(42)); assert!(ptr_22 < SharedPointer::<_, ArcK>::new(42)); assert!(ptr_22 < ptr_42); assert!(!(ptr_42 < SharedPointer::<_, RcK>::new(22))); assert!(!(ptr_42 < SharedPointer::<_, ArcK>::new(22))); assert!(!(ptr_42 < ptr_22)); assert!(!(ptr_22 < ptr_22)); assert!(ptr_22 <= SharedPointer::<_, RcK>::new(42)); assert!(ptr_22 <= SharedPointer::<_, ArcK>::new(42)); assert!(ptr_22 <= ptr_42); assert!(ptr_22 <= ptr_22); assert!(!(ptr_42 <= SharedPointer::<_, RcK>::new(22))); assert!(!(ptr_42 <= SharedPointer::<_, ArcK>::new(22))); assert!(!(ptr_42 <= ptr_22)); assert!(ptr_42 > SharedPointer::<_, RcK>::new(22)); assert!(ptr_42 > SharedPointer::<_, ArcK>::new(22)); assert!(ptr_42 > ptr_22); assert!(!(ptr_22 > SharedPointer::<_, RcK>::new(42))); assert!(!(ptr_22 > SharedPointer::<_, ArcK>::new(42))); assert!(!(ptr_22 > ptr_42)); assert!(!(ptr_42 > ptr_42)); assert!(ptr_42 >= SharedPointer::<_, RcK>::new(22)); assert!(ptr_42 >= SharedPointer::<_, ArcK>::new(22)); assert!(ptr_42 >= ptr_22); assert!(ptr_42 >= ptr_42); assert!(!(ptr_22 >= SharedPointer::<_, RcK>::new(42))); assert!(!(ptr_22 >= SharedPointer::<_, ArcK>::new(42))); assert!(!(ptr_22 >= ptr_42)); } #[test] fn test_default() { let ptr: SharedPointer = SharedPointer::default(); assert_eq!(*ptr, 0); } #[test] fn test_from_box_t() { let ptr: SharedPointer = SharedPointer::from(Box::new(42)); assert_eq!(*ptr, 42); } #[test] fn test_from_t() { let ptr: SharedPointer = SharedPointer::from(42); assert_eq!(*ptr, 42); } #[test] fn test_debug() { let ptr: SharedPointer<_, RcK> = SharedPointer::new([1, 2, 3]); assert_eq!(format!("{ptr:?}"), "[1, 2, 3]"); } #[cfg(not(miri))] // Miri doesn't like this one. #[test] fn test_fmt_pointer() { let ptr: SharedPointer<_, RcK> = SharedPointer::new(314); assert_eq!(format!("{ptr:p}"), format!("{:p}", ptr::from_ref::(ptr.deref()))); } #[test] fn test_display() { let ptr: SharedPointer<_, RcK> = SharedPointer::new("hello"); assert_eq!(format!("{ptr}"), "hello"); } #[cfg(feature = "serde")] #[test] fn test_serde() { let ptr: SharedPointer<_, RcK> = SharedPointer::new("hello"); let encoded = serde_json::to_string(&ptr).unwrap(); let decoded: SharedPointer<_, RcK> = serde_json::from_str(&encoded).unwrap(); pretty_assertions::assert_eq!(ptr, decoded); }