diff options
author | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-04-30 23:11:10 +0000 |
---|---|---|
committer | Android Build Coastguard Worker <android-build-coastguard-worker@google.com> | 2024-04-30 23:11:10 +0000 |
commit | 652864812d53671340139dbf3c57a74efdae2dc8 (patch) | |
tree | b7635fe1d323ed86d29ed0d2dec44e212f1113df | |
parent | 98800fa16bf56e070e832d971225aa37966fcd55 (diff) | |
parent | 7685292d3ba4a578a795bb03dd6e00a8a4d1b6e5 (diff) | |
download | mockall-sdk-release.tar.gz |
Snap for 11785460 from 7685292d3ba4a578a795bb03dd6e00a8a4d1b6e5 to sdk-releasesdk-releasebusytown-mac-infra-release
Change-Id: If5b8573998dba7218d286a0c668c3bb0b9f747be
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 8 | ||||
-rw-r--r-- | Cargo.lock | 198 | ||||
-rw-r--r-- | Cargo.toml | 17 | ||||
-rw-r--r-- | METADATA | 22 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | src/examples.rs | 14 | ||||
-rw-r--r-- | src/lib.rs | 167 |
8 files changed, 240 insertions, 192 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 555d0e6..998a338 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,6 +1,6 @@ { "git": { - "sha1": "d5351f7215c6c5bca11f704ed41d9ae768b43007" + "sha1": "51c4820bc6552aea22a1844c3bf9f1b44175d132" }, "path_in_vcs": "mockall" }
\ No newline at end of file @@ -1,14 +1,16 @@ // This file is generated by cargo_embargo. -// Do not modify this file as changes will be overridden on upgrade. +// Do not modify this file after the first "rust_*" or "genrule" module +// because the changes will be overridden on upgrade. +// Content before the first "rust_*" or "genrule" module is preserved. rust_library { name: "libmockall", host_supported: true, crate_name: "mockall", cargo_env_compat: true, - cargo_pkg_version: "0.11.4", + cargo_pkg_version: "0.12.1", srcs: ["src/lib.rs"], - edition: "2018", + edition: "2021", rustlibs: [ "libcfg_if", "libdowncast", @@ -3,19 +3,16 @@ version = 3 [[package]] -name = "aho-corasick" -version = "0.7.20" +name = "anstyle" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" -dependencies = [ - "memchr", -] +checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" [[package]] name = "async-trait" -version = "0.1.66" +version = "0.1.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b84f9ebcc6c1f5b8cb160f6990096a5c127f423fcb6e1ccc46c370cbdfb75dfc" +checksum = "bc00ceb34980c03614e35a3a4e218276a0a824e911d07651cd0d858a51e8c0f0" dependencies = [ "proc-macro2", "quote", @@ -35,12 +32,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "difflib" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" - -[[package]] name = "downcast" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -48,18 +39,9 @@ checksum = "1435fa1053d8b2fbbe9be7e97eca7f33d37b28409959813daefc1446a14247f1" [[package]] name = "either" -version = "1.8.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" - -[[package]] -name = "float-cmp" -version = "0.9.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4" -dependencies = [ - "num-traits", -] +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "fragile" @@ -69,9 +51,9 @@ checksum = "6c2141d6d6c8512188a7891b4b01590a45f6dac67afb4f255c4124dbb86d4eaa" [[package]] name = "futures" -version = "0.3.21" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e" +checksum = "23342abe12aba583913b2e62f22225ff9c950774065e4bfb61a19cd9770fec40" dependencies = [ "futures-channel", "futures-core", @@ -84,9 +66,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "164713a5a0dcc3e7b4b1ed7d3b433cabc18025386f9339346e8daf15963cf7ac" +checksum = "955518d47e09b25bbebc7a18df10b81f0c766eaf4c4f1cccef2fca5f2a4fb5f2" dependencies = [ "futures-core", "futures-sink", @@ -94,15 +76,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86d7a0c1aa76363dac491de0ee99faf6941128376f1cf96f07db7603b7de69dd" +checksum = "4bca583b7e26f571124fe5b7561d49cb2868d79116cfa0eefce955557c6fee8c" [[package]] name = "futures-executor" -version = "0.3.21" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6" +checksum = "ccecee823288125bd88b4d7f565c9e58e41858e47ab72e8ea2d64e93624386e0" dependencies = [ "futures-core", "futures-task", @@ -111,15 +93,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d422fa3cbe3b40dca574ab087abb5bc98258ea57eea3fd6f1fa7162c778b91" +checksum = "4fff74096e71ed47f8e023204cfd0aa1289cd54ae5430a9523be060cdb849964" [[package]] name = "futures-macro" -version = "0.3.21" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" +checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", @@ -128,21 +110,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.27" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec93083a4aecafb2a80a885c9de1f0ccae9dbd32c2bb54b0c3a65690e0b8d2f2" +checksum = "f43be4fe21a13b9781a69afa4985b0f6ee0e1afab2c6f454a8cf30e2b2237b6e" [[package]] name = "futures-task" -version = "0.3.21" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57c66a976bf5909d801bbef33416c41372779507e7a6b3a5e25e4749c58f776a" +checksum = "76d3d132be6c0e6aa1534069c705a74a5997a356c0dc2f86a47765e5617c5b65" [[package]] name = "futures-util" -version = "0.3.21" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" +checksum = "26b01e40b772d54cf6c6d721c1d1abd0647a0106a12ecaa1c186273392a69533" dependencies = [ "futures-channel", "futures-core", @@ -158,18 +140,18 @@ dependencies = [ [[package]] name = "itertools" -version = "0.10.5" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" dependencies = [ "either", ] [[package]] name = "itoa" -version = "1.0.6" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "lazy_static" @@ -179,13 +161,13 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "memchr" -version = "2.5.0" +version = "2.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" +checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" [[package]] name = "mockall" -version = "0.11.4" +version = "0.12.1" dependencies = [ "async-trait", "cfg-if", @@ -205,9 +187,9 @@ dependencies = [ [[package]] name = "mockall_derive" -version = "0.11.4" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ce75669015c4f47b289fd4d4f56e894e4c96003ffdf3ac51313126f94c6cbb" +checksum = "af7cbce79ec385a1d4f54baa90a76401eb15d9cab93685f62e7e9f942aa00ae2" dependencies = [ "cfg-if", "proc-macro2", @@ -217,9 +199,9 @@ dependencies = [ [[package]] name = "mockall_double" -version = "0.3.0" +version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae71c7bb287375187c775cf82e2dcf1bef3388aaf58f0789a77f9c7ab28466f6" +checksum = "f1ca96e5ac35256ae3e13536edd39b172b88f41615e1d7b653c8ad24524113e8" dependencies = [ "cfg-if", "proc-macro2", @@ -228,31 +210,16 @@ dependencies = [ ] [[package]] -name = "normalize-line-endings" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" - -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", -] - -[[package]] name = "once_cell" -version = "1.14.0" +version = "1.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f7254b99e31cad77da24b08ebf628882739a608578bb1bcdfc1f9c21260d7c0" +checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] name = "pin-project-lite" -version = "0.2.9" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" [[package]] name = "pin-utils" @@ -262,29 +229,26 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "predicates" -version = "2.1.0" +version = "3.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95e5a7689e456ab905c22c2b48225bb921aba7c8dfa58440d68ba13f6222a715" +checksum = "6dfc28575c2e3f19cb3c73b93af36460ae898d426eba6fc15b9bd2a5220758a0" dependencies = [ - "difflib", - "float-cmp", + "anstyle", "itertools", - "normalize-line-endings", "predicates-core", - "regex", ] [[package]] name = "predicates-core" -version = "1.0.3" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da1c2388b1513e1b605fcec39a95e0a9e8ef088f71443ef37099fa9ae6673fcb" +checksum = "b794032607612e7abeb4db69adb4e33590fa6cf1149e95fd7cb00e634b92f174" [[package]] name = "predicates-tree" -version = "1.0.5" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d86de6de25020a36c6d3643a86d9a6a9f552107c0559c60ea03551b5e16c032" +checksum = "368ba315fb8c5052ab692e68a0eefec6ec57b23a36959c14496f0b0df2c0cecf" dependencies = [ "predicates-core", "termtree", @@ -292,56 +256,42 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.54" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" +checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.26" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] [[package]] -name = "regex" -version = "1.7.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b1f693b24f6ac912f4893ef08244d70b6067480d2f1a46e950c9691e6749d1d" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax", -] - -[[package]] -name = "regex-syntax" -version = "0.6.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" - -[[package]] name = "ryu" -version = "1.0.13" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] name = "serde" -version = "1.0.158" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "771d4d9c4163ee138805e12c710dd365e4f44be8be0503cb1bb9eb989425d9c9" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +dependencies = [ + "serde_derive", +] [[package]] name = "serde_derive" -version = "1.0.156" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7e29c4601e36bcec74a223228dce795f4cd3616341a4af93520ca1a837c087d" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", @@ -350,9 +300,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.94" +version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c533a59c9d8a93a09c6ab31f0fd5e5f4dd1b8fc9434804029839884765d04ea" +checksum = "2cc66a619ed80bf7a0f6b17dd063a84b88f6dea1813737cf469aef1d081142c2" dependencies = [ "itoa", "ryu", @@ -361,18 +311,18 @@ dependencies = [ [[package]] name = "slab" -version = "0.4.8" +version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6528351c9bc8ab22353f9d776db39a20288e8d6c37ef8cfe3317cf875eecfc2d" +checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67" dependencies = [ "autocfg", ] [[package]] name = "syn" -version = "1.0.109" +version = "2.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" dependencies = [ "proc-macro2", "quote", @@ -381,9 +331,9 @@ dependencies = [ [[package]] name = "termtree" -version = "0.2.4" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "507e9898683b6c43a9aa55b64259b721b52ba226e0f3779137e50ad114a4c90b" +checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" [[package]] name = "tracing" @@ -399,9 +349,9 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.23" +version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4017f8f45139870ca7e672686113917c71c7a6e02d4924eda67186083c03081a" +checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", @@ -410,15 +360,15 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.30" +version = "0.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24eb03ba0eab1fd845050058ce5e616558e8f8d8fca633e6b163fe25c797213a" +checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" dependencies = [ "once_cell", ] [[package]] name = "unicode-ident" -version = "1.0.8" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" +checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" @@ -10,10 +10,10 @@ # See Cargo.toml.orig for the original contents. [package] -edition = "2018" -rust-version = "1.45" +edition = "2021" +rust-version = "1.60" name = "mockall" -version = "0.11.4" +version = "0.12.1" authors = ["Alan Somers <asomers@gmail.com>"] exclude = [ "examples/*", @@ -31,7 +31,7 @@ keywords = [ "testing", ] categories = ["development-tools::testing"] -license = "MIT/Apache-2.0" +license = "MIT OR Apache-2.0" repository = "https://github.com/asomers/mockall" [package.metadata.docs.rs] @@ -73,10 +73,11 @@ version = "2.0" version = "1.1" [dependencies.mockall_derive] -version = "=0.11.4" +version = "=0.12.1" [dependencies.predicates] -version = "2.0.1" +version = "3.0.0" +default-features = false [dependencies.predicates-tree] version = "1.0" @@ -88,7 +89,7 @@ version = "0.1.38" version = "0.3.7" [dev-dependencies.mockall_double] -version = "^0.3.0" +version = "^0.3.1" [dev-dependencies.serde] version = "1.0" @@ -100,7 +101,7 @@ version = "1.0" version = "1.0" [dev-dependencies.tracing] -version = "0.1.23" +version = "0.1.32" [features] nightly = [ @@ -1,20 +1,24 @@ +# This project was upgraded with external_updater. +# Usage: tools/external_updater/updater.sh update external/rust/crates/mockall +# For more info, check https://cs.android.com/android/platform/superproject/+/main:tools/external_updater/README.md + name: "mockall" description: "()" third_party { + license_type: NOTICE + last_upgrade_date { + year: 2024 + month: 4 + day: 25 + } identifier { type: "crates.io" - value: "https://crates.io/crates/mockall" + value: "https://static.crates.io/crates/mockall/mockall-0.12.1.crate" + version: "0.11.4" } identifier { type: "Archive" value: "https://static.crates.io/crates/mockall/mockall-0.11.4.crate" - } - version: "0.11.4" - # Dual-licensed, using the least restrictive per go/thirdpartylicenses#same. - license_type: NOTICE - last_upgrade_date { - year: 2023 - month: 11 - day: 6 + version: "0.12.1" } } @@ -29,7 +29,7 @@ your `Cargo.toml`: ```toml [dev-dependencies] -mockall = "0.11.4" +mockall = "0.12.1" ``` Then use it like this: @@ -62,7 +62,7 @@ See the [API docs](https://docs.rs/mockall) for more information. # Minimum Supported Rust Version (MSRV) -Mockall is supported on Rust 1.42.0 and higher. Mockall's MSRV will not be +Mockall is supported on Rust 1.64.0 and higher. Mockall's MSRV will not be changed in the future without bumping the major or minor version. # License diff --git a/src/examples.rs b/src/examples.rs index a630752..c15a765 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -40,18 +40,20 @@ mock! { fn boo(&self); } /// An implementation of a trait on a mocked struct - trait Bah { + impl Bah for Boo { fn bah(&self); } } -#[automock(mod mock_ffi;)] -extern "C" { - /// A foreign "C" function. - pub fn ffi_func(); +/// A module full of foreign C functions. +#[automock] +pub mod ffi { + extern "C" { + /// A foreign "C" function. + pub fn ffi_func(); + } } -#[cfg(feature = "nightly")] /// Mock this entire module #[automock] pub mod my_module { @@ -37,7 +37,6 @@ //! * [`impl Trait`](#impl-trait) //! * [`Mocking structs`](#mocking-structs) //! * [`Generic methods`](#generic-methods) -//! * [`Methods with generic lifetimes`](#methods-with-generic-lifetimes) //! * [`Generic traits and structs`](#generic-traits-and-structs) //! * [`Associated types`](#associated-types) //! * [`Multiple and inherited traits`](#multiple-and-inherited-traits) @@ -59,7 +58,7 @@ //! fn foo(&self, x: u32) -> u32; //! } //! -//! fn call_with_four(x: &MyTrait) -> u32 { +//! fn call_with_four(x: &dyn MyTrait) -> u32 { //! x.foo(4) //! } //! @@ -673,12 +672,15 @@ //! //! ## Generic methods //! -//! Generic methods can be mocked, too. Effectively each generic method is an -//! infinite set of regular methods, and each of those works just like any other -//! regular method. The expect_* method is generic, too, and usually must be -//! called with a turbofish. The only restrictions on mocking generic methods -//! are that all generic parameters must be `'static`, and generic lifetime -//! parameters are not allowed. +//! Mocking generic methods is possible, but the exact process depends on +//! whether the parameters are `'static`, non-`'static`, or lifetimes. +//! +//! ### With static parameters +//! +//! With fully `'static` parameters, the mock method is generic and so is its +//! expect_* method. The expect_* method usually must be called with a +//! turbofish. Expectations set with different generic parameters operate +//! completely independently of one another. //! //! ``` //! # use mockall::*; @@ -697,7 +699,15 @@ //! assert_eq!(-5, mock.foo(5i8)); //! ``` //! -//! ## Methods with generic lifetimes +//! ### With non-`static` type parameters +//! +//! Mocking methods with non-`'static` type parameters is harder. The way +//! Mockall does it is by turning the generic parameters into trait objects +//! before evaluating expectations. This makes the expect_* method concrete, +//! rather than generic. It also comes with many restrictions. See +//! [`#[concretize]`](attr.concretize.html) for more details. +//! +//! ### With generic lifetimes //! //! A method with a lifetime parameter is technically a generic method, but //! Mockall treats it like a non-generic method that must work for all possible @@ -1136,7 +1146,7 @@ use downcast::*; use std::{ any, - fmt::{self, Debug, Formatter}, + fmt::Debug, marker::PhantomData, ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive}, @@ -1235,14 +1245,29 @@ pub mod examples; /// } /// ``` /// -/// Finally, `#[automock]` can also mock foreign functions. This requires -/// another metaitem to specify the mock module name. +/// It can mock a module full of functions. In this case, the mock functions +/// will be found in a module whose name is prepended with `mock_`. /// /// ``` /// # use mockall_derive::*; -/// #[automock(mod mock_ffi;)] -/// extern "C" { -/// pub fn foo() -> u32; +/// #[automock] +/// mod mymod { +/// pub fn foo() -> u32 { +/// // ... +/// # unimplemented!() +/// } +/// } +/// ``` +/// Finally, `#[automock]` can also mock foreign functions. This works just +/// like mocking a module. +/// +/// ``` +/// # use mockall_derive::*; +/// #[automock] +/// mod ffi { +/// extern "C" { +/// pub fn foo() -> u32; +/// } /// } /// ``` /// @@ -1260,6 +1285,60 @@ pub mod examples; /// to choose your own name for the mock structure. pub use mockall_derive::automock; +/// Decorates a method or function to tell Mockall to treat its generic arguments +/// as trait objects when creating expectations. +/// +/// This allows users to use non-`'static` generic parameters, which otherwise +/// can't be mocked. The downsides of using this attribute are: +/// +/// * Mockall can't tell if a parameter isn't `'static`, so you must annotate +/// such methods with the `#[mockall::concretize]` attribute. +/// * Generic methods will share expectations for all argument types. That is, +/// you won't be able to do `my_mock.expect_foo::<i32>(...)`. +/// * It can't be used on methods with a closure argument (though this may be +/// fixable). +/// * Concretized methods' expectations may only be matched with `.withf` or +/// `.withf_st`, not `.with`. +/// * It only works for parameters that can be turned into a trait object. +/// (may be fixable). +/// * Mockall needs to know how to turn the function argument into a trait +/// object. Given a generic parameter `T`, currently supported patterns are: +/// - `T` +/// - `&T` +/// - `&mut T` +/// - `&[T]` +/// +/// # Examples +/// ``` +/// # use std::path::Path; +/// # use mockall::{automock, concretize}; +/// #[automock] +/// trait Foo { +/// #[mockall::concretize] +/// fn foo<P: AsRef<Path>>(&self, p: P); +/// } +/// +/// # fn main() { +/// let mut mock = MockFoo::new(); +/// mock.expect_foo() +/// .withf(|p| p.as_ref() == Path::new("/tmp")) +/// .return_const(()); +/// mock.foo(Path::new("/tmp")); +/// # } +/// ``` +/// +/// NB: This attribute must be imported with its canonical name. It won't work +/// otherwise! +/// ```compile_fail +/// use mockall::concretize as something_else; +/// #[mockall::automock] +/// trait Foo { +/// #[something_else] +/// fn foo<T>(&self, t: T); +/// } +/// ``` +pub use mockall_derive::concretize; + /// Manually mock a structure. /// /// Sometimes `automock` can't be used. In those cases you can use `mock!`, @@ -1311,8 +1390,8 @@ pub use mockall_derive::automock; /// ``` /// /// When mocking a generic struct's implementation of a generic trait, use the -/// same namespace for their generic parameters. For example, if you wanted to -/// mock `Rc`, do +/// same name for their generic parameters. For example, if you wanted to mock +/// `Rc`, do /// ``` /// # use mockall_derive::mock; /// mock!{ @@ -1398,28 +1477,38 @@ pub struct DefaultReturner<O>(PhantomData<O>); } } +// Wrapper type to allow for better expectation messages for any type. +// Will first try Debug, otherwise will print '?' #[doc(hidden)] -pub struct MaybeDebugger<'a, T>(pub &'a T); -::cfg_if::cfg_if! { - if #[cfg(feature = "nightly")] { - impl<'a, T> Debug for MaybeDebugger<'a, T> { - default fn fmt(&self, f: &mut Formatter<'_>) - -> Result<(), fmt::Error> - { - write!(f, "?") - } - } - impl<'a, T: Debug> Debug for MaybeDebugger<'a, T> { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { - self.0.fmt(f) - } - } - } else { - impl<'a, T> Debug for MaybeDebugger<'a, T> { - fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { - write!(f, "?") - } - } +pub struct ArgPrinter<'a, T>(pub &'a T); + +#[doc(hidden)] +pub struct DebugPrint<'a, T: Debug>(pub &'a T); +impl<'a, T> Debug for DebugPrint<'a, T> where T: Debug { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Debug::fmt(self.0, f) + } +} +#[doc(hidden)] +pub trait ViaDebug<T> where T: Debug { fn debug_string(&self) -> DebugPrint<'_, T>; } +impl<'a, T: Debug> ViaDebug<T> for &ArgPrinter<'a, T> { + fn debug_string(&self) -> DebugPrint<'a, T> { + DebugPrint(self.0) + } +} + +#[doc(hidden)] +pub struct NothingPrint; +impl Debug for NothingPrint { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "?") + } +} +#[doc(hidden)] +pub trait ViaNothing { fn debug_string(&self) -> NothingPrint; } +impl<'a, T> ViaNothing for ArgPrinter<'a, T> { + fn debug_string(&self) -> NothingPrint { + NothingPrint } } @@ -1625,7 +1714,7 @@ impl SeqInner { /// Verify that the call identified by `seq` was called in the correct order fn verify(&self, seq: usize, desc: &str) { assert_eq!(seq, self.satisfaction_level.load(Ordering::Relaxed), - "{}: Method sequence violation", desc) + "{desc}: Method sequence violation") } } |