archery-1.2.1/.cargo_vcs_info.json0000644000000001360000000000100124720ustar { "git": { "sha1": "d9d72f8ec7c9b6b2dd5b20b111be5806a0510ad3" }, "path_in_vcs": "" }archery-1.2.1/Cargo.lock0000644000000412200000000000100104440ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[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.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" [[package]] name = "archery" version = "1.2.1" dependencies = [ "bincode", "criterion", "pretty_assertions", "serde", "static_assertions", "triomphe", ] [[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" [[package]] name = "bincode" version = "1.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" dependencies = [ "serde", ] [[package]] name = "bumpalo" version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" [[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.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[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.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64acc1846d54c1fe936a78dc189c34e28d3f5afc348403f28ecf53660b9b8462" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" version = "4.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8393d67ba2e7bfaf28a23458e4e2b543cc73a99595511eb207fdb8aede942" dependencies = [ "anstyle", "clap_lex", ] [[package]] name = "clap_lex" version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" [[package]] name = "criterion" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2b12d017a929603d80db1831cd3a24082f8137ce19c69e6447f54f5fc8d692f" dependencies = [ "anes", "cast", "ciborium", "clap", "criterion-plot", "is-terminal", "itertools", "num-traits", "once_cell", "oorandom", "plotters", "rayon", "regex", "serde", "serde_derive", "serde_json", "tinytemplate", "walkdir", ] [[package]] name = "criterion-plot" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" dependencies = [ "cast", "itertools", ] [[package]] name = "crossbeam-deque" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" 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.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "crunchy" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" [[package]] name = "diff" version = "0.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "half" version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" dependencies = [ "cfg-if", "crunchy", ] [[package]] name = "hermit-abi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" [[package]] name = "is-terminal" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" dependencies = [ "hermit-abi", "libc", "windows-sys", ] [[package]] name = "itertools" version = "0.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" dependencies = [ "either", ] [[package]] name = "itoa" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" dependencies = [ "wasm-bindgen", ] [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] name = "log" version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[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.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "oorandom" version = "11.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" [[package]] name = "plotters" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a15b6eccb8484002195a3e44fe65a4ce8e93a625797a063735536fd59cb01cf3" dependencies = [ "num-traits", "plotters-backend", "plotters-svg", "wasm-bindgen", "web-sys", ] [[package]] name = "plotters-backend" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "414cec62c6634ae900ea1c56128dfe87cf63e7caece0852ec76aba307cebadb7" [[package]] name = "plotters-svg" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81b30686a7d9c3e010b84284bdd26a29f2138574f52f5eb6f794fc0ad924e705" dependencies = [ "plotters-backend", ] [[package]] name = "pretty_assertions" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" dependencies = [ "diff", "yansi", ] [[package]] name = "proc-macro2" version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] [[package]] name = "rayon" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" dependencies = [ "either", "rayon-core", ] [[package]] name = "rayon-core" version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" dependencies = [ "crossbeam-deque", "crossbeam-utils", ] [[package]] name = "regex" version = "1.10.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "ryu" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[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.204" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.204" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.120" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e0d21c9a8cae1235ad58a00c11cb40d4b1e5c784f1ef2c537876ed6ffd8b7c5" dependencies = [ "itoa", "ryu", "serde", ] [[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.70" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f0209b68b3613b093e0ec905354eccaedcfe83b8cb37cbdeae64026c3064c16" 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.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6631e42e10b40c0690bf92f404ebcfe6e1fdb480391d15f17cc8e96eeed5369" [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" [[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.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" dependencies = [ "cfg-if", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" dependencies = [ "bumpalo", "log", "once_cell", "proc-macro2", "quote", "syn", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-macro" version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" dependencies = [ "quote", "wasm-bindgen-macro-support", ] [[package]] name = "wasm-bindgen-macro-support" version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" dependencies = [ "proc-macro2", "quote", "syn", "wasm-bindgen-backend", "wasm-bindgen-shared", ] [[package]] name = "wasm-bindgen-shared" version = "0.2.92" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" [[package]] name = "web-sys" version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" dependencies = [ "js-sys", "wasm-bindgen", ] [[package]] name = "winapi-util" version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ "windows-sys", ] [[package]] name = "windows-sys" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" dependencies = [ "windows-targets", ] [[package]] name = "windows-targets" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" [[package]] name = "windows_i686_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "yansi" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" archery-1.2.1/Cargo.toml0000644000000046340000000000100104770ustar # 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 = "2021" rust-version = "1.66.0" name = "archery" version = "1.2.1" authors = ["Diogo Sousa "] build = false include = [ "/src/**/*.rs", "/Cargo.toml", "/LICENSE.md", "/README.md", "/release-notes.md", ] 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 = "MPL-2.0" repository = "https://github.com/orium/archery" [package.metadata.docs.rs] features = [ "triomphe", "serde", ] [lib] name = "archery" path = "src/lib.rs" bench = false [dependencies.serde] version = "1.0.204" optional = true default-features = false [dependencies.triomphe] version = "0.1.13" optional = true default-features = false [dev-dependencies.bincode] version = "1.3.3" [dev-dependencies.criterion] version = "0.5.1" features = ["html_reports"] [dev-dependencies.pretty_assertions] version = "1.4.0" [dev-dependencies.static_assertions] version = "1.1.0" [features] fatal-warnings = [] serde = ["dep:serde"] triomphe = ["dep:triomphe"] [badges.codecov] branch = "main" repository = "orium/archery" service = "github" [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.1/Cargo.toml.orig000064400000000000000000000051101046102023000141460ustar 00000000000000[package] name = "archery" description = "Abstract over the atomicity of reference-counting pointers" version = "1.2.1" authors = ["Diogo Sousa "] edition = "2021" rust-version = "1.66.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 = "MPL-2.0" # 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.204", optional = true, default-features = false } triomphe = { version = "0.1.13", optional = true, default-features = false } [dev-dependencies] bincode = "1.3.3" criterion = { version = "0.5.1", features = ["html_reports"] } pretty_assertions = "1.4.0" static_assertions = "1.1.0" [features] fatal-warnings = [] triomphe = ["dep:triomphe"] serde = ["dep:serde"] [lints.clippy] all = { level = "warn", priority = -2 } pedantic = { level = "warn", priority = -2 } correctness = { level = "deny", priority = -1 } 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.1/LICENSE.md000064400000000000000000000362761046102023000127040ustar 00000000000000Mozilla Public License Version 2.0 ================================== ### 1. Definitions **1.1. “Contributor”** means each individual or legal entity that creates, contributes to the creation of, or owns Covered Software. **1.2. “Contributor Version”** means the combination of the Contributions of others (if any) used by a Contributor and that particular Contributor's Contribution. **1.3. “Contribution”** means Covered Software of a particular Contributor. **1.4. “Covered Software”** means Source Code Form to which the initial Contributor has attached the notice in Exhibit A, the Executable Form of such Source Code Form, and Modifications of such Source Code Form, in each case including portions thereof. **1.5. “Incompatible With Secondary Licenses”** means * **(a)** that the initial Contributor has attached the notice described in Exhibit B to the Covered Software; or * **(b)** that the Covered Software was made available under the terms of version 1.1 or earlier of the License, but not also under the terms of a Secondary License. **1.6. “Executable Form”** means any form of the work other than Source Code Form. **1.7. “Larger Work”** means a work that combines Covered Software with other material, in a separate file or files, that is not Covered Software. **1.8. “License”** means this document. **1.9. “Licensable”** means having the right to grant, to the maximum extent possible, whether at the time of the initial grant or subsequently, any and all of the rights conveyed by this License. **1.10. “Modifications”** means any of the following: * **(a)** any file in Source Code Form that results from an addition to, deletion from, or modification of the contents of Covered Software; or * **(b)** any new file in Source Code Form that contains any Covered Software. **1.11. “Patent Claims” of a Contributor** means any patent claim(s), including without limitation, method, process, and apparatus claims, in any patent Licensable by such Contributor that would be infringed, but for the grant of the License, by the making, using, selling, offering for sale, having made, import, or transfer of either its Contributions or its Contributor Version. **1.12. “Secondary License”** means either the GNU General Public License, Version 2.0, the GNU Lesser General Public License, Version 2.1, the GNU Affero General Public License, Version 3.0, or any later versions of those licenses. **1.13. “Source Code Form”** means the form of the work preferred for making modifications. **1.14. “You” (or “Your”)** means an individual or a legal entity exercising rights under this License. For legal entities, “You” includes any entity that controls, is controlled by, or is under common control with You. For purposes of this definition, “control” means **(a)** the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or **(b)** ownership of more than fifty percent (50%) of the outstanding shares or beneficial ownership of such entity. ### 2. License Grants and Conditions #### 2.1. Grants Each Contributor hereby grants You a world-wide, royalty-free, non-exclusive license: * **(a)** under intellectual property rights (other than patent or trademark) Licensable by such Contributor to use, reproduce, make available, modify, display, perform, distribute, and otherwise exploit its Contributions, either on an unmodified basis, with Modifications, or as part of a Larger Work; and * **(b)** under Patent Claims of such Contributor to make, use, sell, offer for sale, have made, import, and otherwise transfer either its Contributions or its Contributor Version. #### 2.2. Effective Date The licenses granted in Section 2.1 with respect to any Contribution become effective for each Contribution on the date the Contributor first distributes such Contribution. #### 2.3. Limitations on Grant Scope The licenses granted in this Section 2 are the only rights granted under this License. No additional rights or licenses will be implied from the distribution or licensing of Covered Software under this License. Notwithstanding Section 2.1(b) above, no patent license is granted by a Contributor: * **(a)** for any code that a Contributor has removed from Covered Software; or * **(b)** for infringements caused by: **(i)** Your and any other third party's modifications of Covered Software, or **(ii)** the combination of its Contributions with other software (except as part of its Contributor Version); or * **(c)** under Patent Claims infringed by Covered Software in the absence of its Contributions. This License does not grant any rights in the trademarks, service marks, or logos of any Contributor (except as may be necessary to comply with the notice requirements in Section 3.4). #### 2.4. Subsequent Licenses No Contributor makes additional grants as a result of Your choice to distribute the Covered Software under a subsequent version of this License (see Section 10.2) or under the terms of a Secondary License (if permitted under the terms of Section 3.3). #### 2.5. Representation Each Contributor represents that the Contributor believes its Contributions are its original creation(s) or it has sufficient rights to grant the rights to its Contributions conveyed by this License. #### 2.6. Fair Use This License is not intended to limit any rights You have under applicable copyright doctrines of fair use, fair dealing, or other equivalents. #### 2.7. Conditions Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in Section 2.1. ### 3. Responsibilities #### 3.1. Distribution of Source Form All distribution of Covered Software in Source Code Form, including any Modifications that You create or to which You contribute, must be under the terms of this License. You must inform recipients that the Source Code Form of the Covered Software is governed by the terms of this License, and how they can obtain a copy of this License. You may not attempt to alter or restrict the recipients' rights in the Source Code Form. #### 3.2. Distribution of Executable Form If You distribute Covered Software in Executable Form then: * **(a)** such Covered Software must also be made available in Source Code Form, as described in Section 3.1, and You must inform recipients of the Executable Form how they can obtain a copy of such Source Code Form by reasonable means in a timely manner, at a charge no more than the cost of distribution to the recipient; and * **(b)** You may distribute such Executable Form under the terms of this License, or sublicense it under different terms, provided that the license for the Executable Form does not attempt to limit or alter the recipients' rights in the Source Code Form under this License. #### 3.3. Distribution of a Larger Work You may create and distribute a Larger Work under terms of Your choice, provided that You also comply with the requirements of this License for the Covered Software. If the Larger Work is a combination of Covered Software with a work governed by one or more Secondary Licenses, and the Covered Software is not Incompatible With Secondary Licenses, this License permits You to additionally distribute such Covered Software under the terms of such Secondary License(s), so that the recipient of the Larger Work may, at their option, further distribute the Covered Software under the terms of either this License or such Secondary License(s). #### 3.4. Notices You may not remove or alter the substance of any license notices (including copyright notices, patent notices, disclaimers of warranty, or limitations of liability) contained within the Source Code Form of the Covered Software, except that You may alter any license notices to the extent required to remedy known factual inaccuracies. #### 3.5. Application of Additional Terms You may choose to offer, and to charge a fee for, warranty, support, indemnity or liability obligations to one or more recipients of Covered Software. However, You may do so only on Your own behalf, and not on behalf of any Contributor. You must make it absolutely clear that any such warranty, support, indemnity, or liability obligation is offered by You alone, and You hereby agree to indemnify every Contributor for any liability incurred by such Contributor as a result of warranty, support, indemnity or liability terms You offer. You may include additional disclaimers of warranty and limitations of liability specific to any jurisdiction. ### 4. Inability to Comply Due to Statute or Regulation If it is impossible for You to comply with any of the terms of this License with respect to some or all of the Covered Software due to statute, judicial order, or regulation then You must: **(a)** comply with the terms of this License to the maximum extent possible; and **(b)** describe the limitations and the code they affect. Such description must be placed in a text file included with all distributions of the Covered Software under this License. Except to the extent prohibited by statute or regulation, such description must be sufficiently detailed for a recipient of ordinary skill to be able to understand it. ### 5. Termination **5.1.** The rights granted under this License will terminate automatically if You fail to comply with any of its terms. However, if You become compliant, then the rights granted under this License from a particular Contributor are reinstated **(a)** provisionally, unless and until such Contributor explicitly and finally terminates Your grants, and **(b)** on an ongoing basis, if such Contributor fails to notify You of the non-compliance by some reasonable means prior to 60 days after You have come back into compliance. Moreover, Your grants from a particular Contributor are reinstated on an ongoing basis if such Contributor notifies You of the non-compliance by some reasonable means, this is the first time You have received notice of non-compliance with this License from such Contributor, and You become compliant prior to 30 days after Your receipt of the notice. **5.2.** If You initiate litigation against any entity by asserting a patent infringement claim (excluding declaratory judgment actions, counter-claims, and cross-claims) alleging that a Contributor Version directly or indirectly infringes any patent, then the rights granted to You by any and all Contributors for the Covered Software under Section 2.1 of this License shall terminate. **5.3.** In the event of termination under Sections 5.1 or 5.2 above, all end user license agreements (excluding distributors and resellers) which have been validly granted by You or Your distributors under this License prior to termination shall survive termination. ### 6. Disclaimer of Warranty > Covered Software is provided under this License on an “as is” > basis, without warranty of any kind, either expressed, implied, or > statutory, including, without limitation, warranties that the > Covered Software is free of defects, merchantable, fit for a > particular purpose or non-infringing. The entire risk as to the > quality and performance of the Covered Software is with You. > Should any Covered Software prove defective in any respect, You > (not any Contributor) assume the cost of any necessary servicing, > repair, or correction. This disclaimer of warranty constitutes an > essential part of this License. No use of any Covered Software is > authorized under this License except under this disclaimer. ### 7. Limitation of Liability > Under no circumstances and under no legal theory, whether tort > (including negligence), contract, or otherwise, shall any > Contributor, or anyone who distributes Covered Software as > permitted above, be liable to You for any direct, indirect, > special, incidental, or consequential damages of any character > including, without limitation, damages for lost profits, loss of > goodwill, work stoppage, computer failure or malfunction, or any > and all other commercial damages or losses, even if such party > shall have been informed of the possibility of such damages. This > limitation of liability shall not apply to liability for death or > personal injury resulting from such party's negligence to the > extent applicable law prohibits such limitation. Some > jurisdictions do not allow the exclusion or limitation of > incidental or consequential damages, so this exclusion and > limitation may not apply to You. ### 8. Litigation Any litigation relating to this License may be brought only in the courts of a jurisdiction where the defendant maintains its principal place of business and such litigation shall be governed by laws of that jurisdiction, without reference to its conflict-of-law provisions. Nothing in this Section shall prevent a party's ability to bring cross-claims or counter-claims. ### 9. Miscellaneous This License represents the complete agreement concerning the subject matter hereof. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. Any law or regulation which provides that the language of a contract shall be construed against the drafter shall not be used to construe this License against a Contributor. ### 10. Versions of the License #### 10.1. New Versions Mozilla Foundation is the license steward. Except as provided in Section 10.3, no one other than the license steward has the right to modify or publish new versions of this License. Each version will be given a distinguishing version number. #### 10.2. Effect of New Versions You may distribute the Covered Software under the terms of the version of the License under which You originally received the Covered Software, or under the terms of any subsequent version published by the license steward. #### 10.3. Modified Versions If you create software not governed by this License, and you want to create a new license for such software, you may create and use a modified version of this License if you rename the license and remove any references to the name of the license steward (except to note that such modified license differs from this License). #### 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses If You choose to distribute Source Code Form that is Incompatible With Secondary Licenses under the terms of this version of the License, the notice described in Exhibit B of this License must be attached. ## Exhibit A - Source Code Form License Notice This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. If it is not possible or desirable to put the notice in a particular file, then You may include the notice in a location (such as a LICENSE file in a relevant directory) where a recipient would be likely to look for such a notice. You may add additional accurate notices of copyright ownership. ## Exhibit B - “Incompatible With Secondary Licenses” Notice This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. archery-1.2.1/README.md000064400000000000000000000122251046102023000125430ustar 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.1/release-notes.md000064400000000000000000000024521046102023000143550ustar 00000000000000# Release notes ## 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.1/src/lib.rs000064400000000000000000000123501046102023000131660ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #![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.1/src/shared_pointer/kind/arc/mod.rs000064400000000000000000000067531046102023000177110ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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 { let arc: UntypedArc = ManuallyDrop::into_inner(self.inner); mem::transmute(arc) } #[inline(always)] unsafe fn as_inner_ref(&self) -> &Arc { let arc_t: *const Arc = (self.inner.deref() as *const UntypedArc).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 { let arc_t: *mut Arc = (self.inner.deref_mut() as *mut UntypedArc).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 { Arc::as_ptr(self.as_inner_ref()) } #[inline(always)] unsafe fn deref(&self) -> &T { self.as_inner_ref::().as_ref() } #[inline(always)] unsafe fn try_unwrap(self) -> Result { Arc::try_unwrap(self.take_inner()).map_err(ArcK::new_from_inner) } #[inline(always)] unsafe fn get_mut(&mut self) -> Option<&mut T> { Arc::get_mut(self.as_inner_mut()) } #[inline(always)] unsafe fn make_mut(&mut self) -> &mut T { Arc::make_mut(self.as_inner_mut()) } #[inline(always)] unsafe fn strong_count(&self) -> usize { Arc::strong_count(self.as_inner_ref::()) } #[inline(always)] unsafe fn clone(&self) -> ArcK { ArcK { inner: ManuallyDrop::new(Arc::clone(self.as_inner_ref())) } } #[inline(always)] unsafe fn drop(&mut self) { 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.1/src/shared_pointer/kind/arc/test.rs000064400000000000000000000105611046102023000201010ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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.1/src/shared_pointer/kind/arct/mod.rs000064400000000000000000000067241046102023000200730ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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); mem::transmute(arc) } #[inline(always)] unsafe fn as_inner_ref(&self) -> &Arc { let arc_t: *const Arc = (self.inner.deref() as *const UntypedArc).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 { let arc_t: *mut Arc = (self.inner.deref_mut() as *mut UntypedArc).cast::>(); &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 { Arc::as_ptr(self.as_inner_ref()) } #[inline(always)] unsafe fn deref(&self) -> &T { self.as_inner_ref::().as_ref() } #[inline(always)] unsafe fn try_unwrap(self) -> Result { Arc::try_unwrap(self.take_inner()).map_err(ArcTK::new_from_inner) } #[inline(always)] unsafe fn get_mut(&mut self) -> Option<&mut T> { Arc::get_mut(self.as_inner_mut()) } #[inline(always)] unsafe fn make_mut(&mut self) -> &mut T { Arc::make_mut(self.as_inner_mut()) } #[inline(always)] unsafe fn strong_count(&self) -> usize { Arc::count(self.as_inner_ref::()) } #[inline(always)] unsafe fn clone(&self) -> ArcTK { ArcTK { inner: ManuallyDrop::new(Arc::clone(self.as_inner_ref())) } } #[inline(always)] unsafe fn drop(&mut self) { 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.1/src/shared_pointer/kind/arct/test.rs000064400000000000000000000105641046102023000202700ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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.1/src/shared_pointer/kind/mod.rs000064400000000000000000000046171046102023000171410ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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.1/src/shared_pointer/kind/rc/mod.rs000064400000000000000000000066521046102023000175460ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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 { let rc: UntypedRc = ManuallyDrop::into_inner(self.inner); mem::transmute(rc) } #[inline(always)] unsafe fn as_inner_ref(&self) -> &Rc { let rc_t: *const Rc = (self.inner.deref() as *const UntypedRc).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 { let rc_t: *mut Rc = (self.inner.deref_mut() as *mut UntypedRc).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 { Rc::as_ptr(self.as_inner_ref()) } #[inline(always)] unsafe fn deref(&self) -> &T { self.as_inner_ref::().as_ref() } #[inline(always)] unsafe fn try_unwrap(self) -> Result { Rc::try_unwrap(self.take_inner()).map_err(RcK::new_from_inner) } #[inline(always)] unsafe fn get_mut(&mut self) -> Option<&mut T> { Rc::get_mut(self.as_inner_mut()) } #[inline(always)] unsafe fn make_mut(&mut self) -> &mut T { Rc::make_mut(self.as_inner_mut()) } #[inline(always)] unsafe fn strong_count(&self) -> usize { Rc::strong_count(self.as_inner_ref::()) } #[inline(always)] unsafe fn clone(&self) -> RcK { RcK { inner: ManuallyDrop::new(Rc::clone(self.as_inner_ref())) } } #[inline(always)] unsafe fn drop(&mut self) { 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.1/src/shared_pointer/kind/rc/test.rs000064400000000000000000000105521046102023000177400ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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.1/src/shared_pointer/mod.rs000064400000000000000000000221061046102023000162050ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use 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.1/src/shared_pointer/test.rs000064400000000000000000000240641046102023000164120ustar 00000000000000/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #![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.deref() as *const i32)); } #[test] fn test_display() { let ptr: SharedPointer<_, RcK> = SharedPointer::new("hello"); assert_eq!(format!("{ptr}"), "hello"); } #[cfg(feature = "serde")] #[test] fn test_serde() { use bincode::{deserialize, serialize}; let ptr: SharedPointer<_, RcK> = SharedPointer::new("hello"); let encoded = serialize(&ptr).unwrap(); let decoded: SharedPointer<_, RcK> = deserialize(&encoded).unwrap(); pretty_assertions::assert_eq!(ptr, decoded); }