Vendor dependencies

Let's see how I like this workflow.
This commit is contained in:
John Doty 2022-12-19 08:27:18 -08:00
parent 34d1830413
commit 9c435dc440
7500 changed files with 1665121 additions and 99 deletions

View file

@ -0,0 +1 @@
{"files":{"CODE_OF_CONDUCT.md":"e0bd80144c93b032dadb144d7cf8663c55d697ba2dd60382715c981dfc60e421","COPYRIGHT":"495c30b45120f8af07cfa26eb9cb1ebfe8324560dca2b3b1e76cad1b9b6b489a","Cargo.toml":"250de4b507a7eb89bf97fdcea7b2511020d28c5919de5ab12fb8d91d5ba98247","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-Apache-2.0_WITH_LLVM-exception":"268872b9816f90fd8e85db5a28d33f8150ebb8dd016653fb39ef1f94f2686bc5","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","ORG_CODE_OF_CONDUCT.md":"a62b69bf86e605ee1bcbb2f0a12ba79e4cebb6983a7b6491949750aecc4f2178","README.md":"408d5965c87cc338493f67f638adea97dbd415f4e12638be1365096afa2b8ba3","build.rs":"5809e0c02e95c4e7ddc57a07090b7ae2254cbe1aef493c9a2d720c58e4424556","src/example_ffi.rs":"c369438f2c44b21a5b977d303d6d0f3069a44d56d8ef770da06ddc3dcc967bdf","src/impls_async_std.rs":"62f73e42a2e76a9287f72f1c3db322114d4a08a2d8e2715c04156df60df1f876","src/impls_fs_err.rs":"12333298a291377a8c24835c60dfa3338e413e6435576cede04ce780da84ebe6","src/impls_mio.rs":"32ba71c1912d7dedcb110cb91dd953bab05a039d576c55a7508fcdad1bebbc2b","src/impls_os_pipe.rs":"5075498711f2dfdd6795d3afdb81bd3aca26c033d2f8f7238de59016d3ead0a9","src/impls_socket2.rs":"deacb4dd2c1e19ef483c937b6a18034397d729c3df361de5e748d70f5457f48b","src/impls_std.rs":"4e73f3993f539b7fa22db0b878eb8845d9ecf79f1580382619c83e18cf384fa3","src/impls_tokio.rs":"4f5f3dbdfa9395cd7e5149658cbd3872fa6c3973e1c844b7f6b48ee2e39e9197","src/lib.rs":"2dc8f7bcad5d12ce5d60c9db9a7780bead0240baeaca8d8cfd1d531c14e8eec4","src/portability.rs":"3e9929c7c04524d99a481c646f708f29cd5e251bf44dad7115ae381c2bcaab78","src/raw.rs":"2fa1ea37da1cc993e36ce7950b3e2bc635b3ac268dacd5b5520e5bfc84741b4a","src/traits.rs":"7419b21302b5a633dddf6765af706c5c09840a55e004d839f418fdb5f8826b2b","src/types.rs":"ca3729538bcfd3f63379fd1db4cb635b84cc1380d96ec5072403570de062df9c","src/views.rs":"ecc38c14b08566cc1667d530804a86124d90480e7fdecc585b0e4cfff7312807"},"package":"46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c"}

49
vendor/io-lifetimes/CODE_OF_CONDUCT.md vendored Normal file
View file

@ -0,0 +1,49 @@
# Contributor Covenant Code of Conduct
*Note*: this Code of Conduct pertains to individuals' behavior. Please also see the [Organizational Code of Conduct][OCoC].
## Our Pledge
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the Bytecode Alliance CoC team at [report@bytecodealliance.org](mailto:report@bytecodealliance.org). The CoC team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The CoC team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the Bytecode Alliance's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
[OCoC]: https://github.com/sunfishcode/io-lifetimes/blob/main/ORG_CODE_OF_CONDUCT.md
[homepage]: https://www.contributor-covenant.org
[version]: https://www.contributor-covenant.org/version/1/4/

29
vendor/io-lifetimes/COPYRIGHT vendored Normal file
View file

@ -0,0 +1,29 @@
Short version for non-lawyers:
`io-lifetimes` is triple-licensed under Apache 2.0 with the LLVM Exception,
Apache 2.0, and MIT terms.
Longer version:
Copyrights in the `io-lifetimes` project are retained by their contributors.
No copyright assignment is required to contribute to the `io-lifetimes`
project.
Some files include code derived from Rust's `libstd`; see the comments in
the code for details.
Except as otherwise noted (below and/or in individual files), `io-lifetimes`
is licensed under:
- the Apache License, Version 2.0, with the LLVM Exception
<LICENSE-Apache-2.0_WITH_LLVM-exception> or
<http://llvm.org/foundation/relicensing/LICENSE.txt>
- the Apache License, Version 2.0
<LICENSE-APACHE> or
<http://www.apache.org/licenses/LICENSE-2.0>,
- or the MIT license
<LICENSE-MIT> or
<http://opensource.org/licenses/MIT>,
at your option.

94
vendor/io-lifetimes/Cargo.toml vendored Normal file
View file

@ -0,0 +1,94 @@
# 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 = "2018"
name = "io-lifetimes"
version = "1.0.3"
authors = ["Dan Gohman <dev@sunfishcode.online>"]
include = [
"src",
"build.rs",
"Cargo.toml",
"COPYRIGHT",
"LICENSE*",
"/*.md",
]
description = "A low-level I/O ownership and borrowing library"
readme = "README.md"
keywords = [
"api",
"io",
]
categories = [
"os",
"rust-patterns",
]
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
repository = "https://github.com/sunfishcode/io-lifetimes"
[dependencies.fs-err]
version = "2.6.0"
optional = true
[features]
close = [
"libc",
"windows-sys",
]
default = ["close"]
[target."cfg(not(target_os = \"wasi\"))".dependencies.async-std]
version = "1.12.0"
optional = true
[target."cfg(not(target_os = \"wasi\"))".dependencies.mio]
version = "0.8.0"
features = [
"net",
"os-ext",
]
optional = true
[target."cfg(not(target_os = \"wasi\"))".dependencies.os_pipe]
version = "1.0.0"
features = ["io_safety"]
optional = true
[target."cfg(not(target_os = \"wasi\"))".dependencies.socket2]
version = "0.4.0"
optional = true
[target."cfg(not(target_os = \"wasi\"))".dependencies.tokio]
version = "1.6.0"
features = [
"io-std",
"fs",
"net",
"process",
]
optional = true
[target."cfg(not(windows))".dependencies.libc]
version = "0.2.96"
optional = true
[target."cfg(windows)".dependencies.windows-sys]
version = "0.42.0"
features = [
"Win32_Foundation",
"Win32_Storage_FileSystem",
"Win32_Networking_WinSock",
"Win32_Security",
"Win32_System_IO",
"Win32_System_Threading",
]
optional = true

201
vendor/io-lifetimes/LICENSE-APACHE vendored Normal file
View file

@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -0,0 +1,220 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--- LLVM Exceptions to the Apache 2.0 License ----
As an exception, if, as a result of your compiling your source code, portions
of this Software are embedded into an Object form of such source code, you
may redistribute such embedded portions in such Object form without complying
with the conditions of Sections 4(a), 4(b) and 4(d) of the License.
In addition, if you combine or link compiled forms of this Software with
software that is licensed under the GPLv2 ("Combined Software") and if a
court of competent jurisdiction determines that the patent provision (Section
3), the indemnity provision (Section 9) or other Section of the License
conflicts with the conditions of the GPLv2, you may retroactively and
prospectively choose to deem waived or otherwise exclude such Section(s) of
the License, but only in their entirety and only with respect to the Combined
Software.

23
vendor/io-lifetimes/LICENSE-MIT vendored Normal file
View file

@ -0,0 +1,23 @@
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

View file

@ -0,0 +1,143 @@
# Bytecode Alliance Organizational Code of Conduct (OCoC)
*Note*: this Code of Conduct pertains to organizations' behavior. Please also see the [Individual Code of Conduct](CODE_OF_CONDUCT.md).
## Preamble
The Bytecode Alliance (BA) welcomes involvement from organizations,
including commercial organizations. This document is an
*organizational* code of conduct, intended particularly to provide
guidance to commercial organizations. It is distinct from the
[Individual Code of Conduct (ICoC)](CODE_OF_CONDUCT.md), and does not
replace the ICoC. This OCoC applies to any group of people acting in
concert as a BA member or as a participant in BA activities, whether
or not that group is formally incorporated in some jurisdiction.
The code of conduct described below is not a set of rigid rules, and
we did not write it to encompass every conceivable scenario that might
arise. For example, it is theoretically possible there would be times
when asserting patents is in the best interest of the BA community as
a whole. In such instances, consult with the BA, strive for
consensus, and interpret these rules with an intent that is generous
to the community the BA serves.
While we may revise these guidelines from time to time based on
real-world experience, overall they are based on a simple principle:
*Bytecode Alliance members should observe the distinction between
public community functions and private functions — especially
commercial ones — and should ensure that the latter support, or at
least do not harm, the former.*
## Guidelines
* **Do not cause confusion about Wasm standards or interoperability.**
Having an interoperable WebAssembly core is a high priority for
the BA, and members should strive to preserve that core. It is fine
to develop additional non-standard features or APIs, but they
should always be clearly distinguished from the core interoperable
Wasm.
Treat the WebAssembly name and any BA-associated names with
respect, and follow BA trademark and branding guidelines. If you
distribute a customized version of software originally produced by
the BA, or if you build a product or service using BA-derived
software, use names that clearly distinguish your work from the
original. (You should still provide proper attribution to the
original, of course, wherever such attribution would normally be
given.)
Further, do not use the WebAssembly name or BA-associated names in
other public namespaces in ways that could cause confusion, e.g.,
in company names, names of commercial service offerings, domain
names, publicly-visible social media accounts or online service
accounts, etc. It may sometimes be reasonable, however, to
register such a name in a new namespace and then immediately donate
control of that account to the BA, because that would help the project
maintain its identity.
For further guidance, see the BA Trademark and Branding Policy
[TODO: create policy, then insert link].
* **Do not restrict contributors.** If your company requires
employees or contractors to sign non-compete agreements, those
agreements must not prevent people from participating in the BA or
contributing to related projects.
This does not mean that all non-compete agreements are incompatible
with this code of conduct. For example, a company may restrict an
employee's ability to solicit the company's customers. However, an
agreement must not block any form of technical or social
participation in BA activities, including but not limited to the
implementation of particular features.
The accumulation of experience and expertise in individual persons,
who are ultimately free to direct their energy and attention as
they decide, is one of the most important drivers of progress in
open source projects. A company that limits this freedom may hinder
the success of the BA's efforts.
* **Do not use patents as offensive weapons.** If any BA participant
prevents the adoption or development of BA technologies by
asserting its patents, that undermines the purpose of the
coalition. The collaboration fostered by the BA cannot include
members who act to undermine its work.
* **Practice responsible disclosure** for security vulnerabilities.
Use designated, non-public reporting channels to disclose technical
vulnerabilities, and give the project a reasonable period to
respond, remediate, and patch. [TODO: optionally include the
security vulnerability reporting URL here.]
Vulnerability reporters may patch their company's own offerings, as
long as that patching does not significantly delay the reporting of
the vulnerability. Vulnerability information should never be used
for unilateral commercial advantage. Vendors may legitimately
compete on the speed and reliability with which they deploy
security fixes, but withholding vulnerability information damages
everyone in the long run by risking harm to the BA project's
reputation and to the security of all users.
* **Respect the letter and spirit of open source practice.** While
there is not space to list here all possible aspects of standard
open source practice, some examples will help show what we mean:
* Abide by all applicable open source license terms. Do not engage
in copyright violation or misattribution of any kind.
* Do not claim others' ideas or designs as your own.
* When others engage in publicly visible work (e.g., an upcoming
demo that is coordinated in a public issue tracker), do not
unilaterally announce early releases or early demonstrations of
that work ahead of their schedule in order to secure private
advantage (such as marketplace advantage) for yourself.
The BA reserves the right to determine what constitutes good open
source practices and to take action as it deems appropriate to
encourage, and if necessary enforce, such practices.
## Enforcement
Instances of organizational behavior in violation of the OCoC may
be reported by contacting the Bytecode Alliance CoC team at
[report@bytecodealliance.org](mailto:report@bytecodealliance.org). The
CoC team will review and investigate all complaints, and will respond
in a way that it deems appropriate to the circumstances. The CoC team
is obligated to maintain confidentiality with regard to the reporter of
an incident. Further details of specific enforcement policies may be
posted separately.
When the BA deems an organization in violation of this OCoC, the BA
will, at its sole discretion, determine what action to take. The BA
will decide what type, degree, and duration of corrective action is
needed, if any, before a violating organization can be considered for
membership (if it was not already a member) or can have its membership
reinstated (if it was a member and the BA canceled its membership due
to the violation).
In practice, the BA's first approach will be to start a conversation,
with punitive enforcement used only as a last resort. Violations
often turn out to be unintentional and swiftly correctable with all
parties acting in good faith.

179
vendor/io-lifetimes/README.md vendored Normal file
View file

@ -0,0 +1,179 @@
<div align="center">
<h1><code>io-lifetimes</code></h1>
<p>
<strong>A low-level I/O ownership and borrowing library</strong>
</p>
<p>
<a href="https://github.com/sunfishcode/io-lifetimes/actions?query=workflow%3ACI"><img src="https://github.com/sunfishcode/io-lifetimes/workflows/CI/badge.svg" alt="Github Actions CI Status" /></a>
<a href="https://crates.io/crates/io-lifetimes"><img src="https://img.shields.io/crates/v/io-lifetimes.svg" alt="crates.io page" /></a>
<a href="https://docs.rs/io-lifetimes"><img src="https://docs.rs/io-lifetimes/badge.svg" alt="docs.rs docs" /></a>
</p>
</div>
This library introduces `OwnedFd`, `BorrowedFd`, and supporting types and
traits, and corresponding features for Windows, which implement safe owning
and borrowing I/O lifetime patterns.
This is associated with [RFC 3128], the I/O Safety RFC, which is now merged.
Work is now underway to move the `OwnedFd` and `BorrowedFd` types and `AsFd`
trait developed here into `std`.
For a quick taste, check out the code examples:
- [hello], a basic demo of this API, doing low-level I/O manually, using the
[provided example FFI bindings]
- [easy-conversions], demonstrating the `from_into` convenience feature for
converting from an `impl Into*` into an `impl From*`.
- [portable-views], demonstrating the convenience feature which allows one
to temporarily "view" a file descriptor as any owning type such as `File`
- [flexible-apis], demonstrating how to write library APIs that accept
untyped I/O resources.
- [owning-wrapper], demonstrating how to implement a type which wraps an
`Owned*` type.
[hello]: https://github.com/sunfishcode/io-lifetimes/blob/main/examples/hello.rs
[easy-conversions]: https://github.com/sunfishcode/io-lifetimes/blob/main/examples/easy-conversions.rs
[portable-views]: https://github.com/sunfishcode/io-lifetimes/blob/main/examples/portable-views.rs
[flexible-apis]: https://github.com/sunfishcode/io-lifetimes/blob/main/examples/flexible-apis.rs
[owning-wrapper]: https://github.com/sunfishcode/io-lifetimes/blob/main/examples/owning-wrapper.rs
[provided example FFI bindings]: https://github.com/sunfishcode/io-lifetimes/blob/main/src/example_ffi.rs
The core of the API is very simple, and consists of two main types and three
main traits:
```rust
pub struct BorrowedFd<'fd> { ... }
pub struct OwnedFd { ... }
pub trait AsFd { ... }
pub trait IntoFd { ... }
pub trait FromFd { ... }
impl AsRawFd for BorrowedFd<'_> { ... }
impl AsRawFd for OwnedFd { ... }
impl IntoRawFd for OwnedFd { ... }
impl FromRawFd for OwnedFd { ... }
impl Drop for OwnedFd { ... }
impl AsFd for BorrowedFd<'_> { ... }
impl AsFd for OwnedFd { ... }
impl IntoFd for OwnedFd { ... }
impl FromFd for OwnedFd { ... }
```
On Windows, there are `Handle` and `Socket` versions of every `Fd` thing, and
a special `HandleOrInvalid` type to cope with inconsistent error reporting
in the Windows API.
## The magic of transparency
Here's the fun part. `BorrowedFd` and `OwnedFd` are `repr(transparent)` and
hold `RawFd` values, and `Option<BorrowedFd>` and `Option<OwnedFd>` are
FFI-safe (on Rust >= 1.63), so they can all be used in FFI [directly]:
[directly]: https://github.com/sunfishcode/io-lifetimes/blob/main/src/example_ffi.rs
```rust
extern "C" {
pub fn open(pathname: *const c_char, flags: c_int, ...) -> Option<OwnedFd>;
pub fn read(fd: BorrowedFd<'_>, ptr: *mut c_void, size: size_t) -> ssize_t;
pub fn write(fd: BorrowedFd<'_>, ptr: *const c_void, size: size_t) -> ssize_t;
pub fn close(fd: OwnedFd) -> c_int;
}
```
With bindings like this, users never have to touch `RawFd` values. Of course,
not all code will do this, but it is a fun feature for code that can. This
is what motivates having `BorrowedFd` instead of just using `&OwnedFd`.
Note the use of `Option<OwnedFd>` as the return value of `open`, representing
the fact that it can either succeed or fail.
## I/O Safety in Rust
I/O Safety feature is stablized in Rust 1.63. With this version or later,
io-lifetimes will use and re-export the standard-library types and traits. With
older versions, io-lifetimes defines its own copy of these types and traits.
io-lifetimes also includes several features which are not (yet?) in std,
including the portability traits `AsFilelike`/`AsSocketlike`/etc., the
`from_into_*` functions in the `From*` traits, and [views].
[views]: https://docs.rs/io-lifetimes/*/io_lifetimes/views/index.html
## Prior Art
There are several similar crates: [fd](https://crates.io/crates/fd),
[filedesc](https://crates.io/crates/filedesc),
[filedescriptor](https://crates.io/crates/filedescriptor),
[owned-fd](https://crates.io/crates/owned-fd), and
[unsafe-io](https://crates.io/crates/unsafe-io).
Some of these provide additional features such as the ability to create pipes
or sockets, to get and set flags, and to do read and write operations.
io-lifetimes omits these features, leaving them to to be provided as separate
layers on top.
Most of these crates provide ways to duplicate a file descriptor. io-lifetimes
currently treats this as another feature that can be provided by a layer on
top, though if there are use cases where this is a common operation, it could
be added.
io-lifetimes's distinguishing features are its use of `repr(transparent)`
to support direct FFI usage, niche optimizations so `Option` can support direct
FFI usafe as well (on Rust >= 1.63), lifetime-aware `As*`/`Into*`/`From*`
traits which leverage Rust's lifetime system and allow safe and checked
`from_*` and `as_*`/`into_*` functions, and powerful convenience features
enabled by its underlying safety.
io-lifetimes also has full Windows support, as well as Unix/Windows
portability abstractions, covering both file-like and socket-like types.
io-lifetimes's [`OwnedFd`] type is similar to
[fd](https://crates.io/crates/fd)'s
[`FileDesc`](https://docs.rs/fd/0.2.3/fd/struct.FileDesc.html). io-lifetimes
doesn't have a `close_on_drop` parameter, and instead uses [`OwnedFd`] and
[`BorrowedFd`] to represent dropping and non-dropping handles, respectively, in
a way that is checked at compile time rather than runtime.
io-lifetimes's [`OwnedFd`] type is also similar to
[filedesc](https://crates.io/crates/filedesc)'s
[`FileDesc`](https://docs.rs/filedesc/0.3.0/filedesc/struct.FileDesc.html)
io-lifetimes's `OwnedFd` reserves the value -1, so it doesn't need to test for
`-1` in its `Drop`, and `Option<OwnedFd>` (on Rust >= 1.63) is the same size
as `FileDesc`.
io-lifetimes's [`OwnedFd`] type is also similar to
[owned-fd](https://crates.io/crates/owned-fd)'s
[`OwnedFd`](https://docs.rs/owned-fd/0.1.0/owned_fd/struct.OwnedFd.html).
io-lifetimes doesn't implement `Clone`, because duplicating a file descriptor
can fail due to OS process limits, while `Clone` is an infallible interface.
io-lifetimes's [`BorrowedFd`] is similar to
[owned-fd](https://crates.io/crates/owned-fd)'s
[`FdRef`](https://docs.rs/owned-fd/0.1.0/owned_fd/struct.FdRef.html), except it
uses a lifetime parameter and `PhantomData` rather than transmuting a raw file
descriptor value into a reference value.
io-lifetimes's convenience features are similar to those of
[unsafe-io](https://crates.io/crates/unsafe-io), but io-lifetimes is built on
its own `As*`/`Into*`/`From*` traits, rather than extending
`AsRaw*`/`IntoRaw*`/`FromRaw*` with
[`OwnsRaw`](https://docs.rs/unsafe-io/0.6.9/unsafe_io/trait.OwnsRaw.html), so
they're simpler and safer to use. io-lifetimes doesn't include unsafe-io's
`*ReadWrite*` or `*HandleOrSocket*` abstractions, and leaves these as features
to be provided by separate layers on top.
## Minimum Supported Rust Version (MSRV)
This crate currently works on the version of [Rust on Debian stable], which is
currently Rust 1.48. This policy may change in the future, in minor version
releases, so users using a fixed version of Rust should pin to a specific
version of this crate.
[`OwnedFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/struct.OwnedFd.html
[`BorrowedFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/struct.BorrowedFd.html
[RFC 3128]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md

114
vendor/io-lifetimes/build.rs vendored Normal file
View file

@ -0,0 +1,114 @@
use std::env::var;
use std::io::Write;
fn main() {
// I/O safety is stabilized in Rust 1.63.
if has_io_safety() {
use_feature("io_safety_is_in_std")
}
// Work around
// https://github.com/rust-lang/rust/issues/103306.
use_feature_or_nothing("wasi_ext");
// Rust 1.56 and earlier don't support panic in const fn.
if has_panic_in_const_fn() {
use_feature("panic_in_const_fn")
}
// Don't rerun this on changes other than build.rs, as we only depend on
// the rustc version.
println!("cargo:rerun-if-changed=build.rs");
}
fn use_feature_or_nothing(feature: &str) {
if has_feature(feature) {
use_feature(feature);
}
}
fn use_feature(feature: &str) {
println!("cargo:rustc-cfg={}", feature);
}
/// Test whether the rustc at `var("RUSTC")` supports the given feature.
fn has_feature(feature: &str) -> bool {
let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();
let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.spawn()
.unwrap();
writeln!(child.stdin.take().unwrap(), "#![feature({})]", feature).unwrap();
child.wait().unwrap().success()
}
/// Test whether the rustc at `var("RUSTC")` supports panic in `const fn`.
fn has_panic_in_const_fn() -> bool {
let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();
let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.spawn()
.unwrap();
writeln!(child.stdin.take().unwrap(), "const fn foo() {{ panic!() }}").unwrap();
child.wait().unwrap().success()
}
/// Test whether the rustc at `var("RUSTC")` supports the I/O safety feature.
fn has_io_safety() -> bool {
let out_dir = var("OUT_DIR").unwrap();
let rustc = var("RUSTC").unwrap();
let target = var("TARGET").unwrap();
let mut child = std::process::Command::new(rustc)
.arg("--crate-type=rlib") // Don't require `main`.
.arg("--emit=metadata") // Do as little as possible but still parse.
.arg("--target")
.arg(target)
.arg("--out-dir")
.arg(out_dir) // Put the output somewhere inconsequential.
.arg("-") // Read from stdin.
.stdin(std::process::Stdio::piped()) // Stdin is a pipe.
.spawn()
.unwrap();
writeln!(
child.stdin.take().unwrap(),
"\
#[cfg(unix)]\n\
use std::os::unix::io::OwnedFd as Owned;\n\
#[cfg(target_os = \"wasi\")]\n\
use std::os::wasi::io::OwnedFd as Owned;\n\
#[cfg(windows)]\n\
use std::os::windows::io::OwnedHandle as Owned;\n\
\n\
pub type Success = Owned;\n\
"
)
.unwrap();
child.wait().unwrap().success()
}

75
vendor/io-lifetimes/src/example_ffi.rs vendored Normal file
View file

@ -0,0 +1,75 @@
//! This is just a sample of what FFI using this crate can look like.
#![cfg_attr(not(io_safety_is_in_std), allow(unused_imports))]
#![allow(missing_docs)]
#[cfg(any(unix, target_os = "wasi"))]
use crate::{BorrowedFd, OwnedFd};
#[cfg(windows)]
use crate::{BorrowedHandle, HandleOrInvalid};
#[cfg(any(unix, target_os = "wasi"))]
use libc::{c_char, c_int, c_void, size_t, ssize_t};
#[cfg(windows)]
use {
core::ffi::c_void,
windows_sys::core::PCWSTR,
windows_sys::Win32::Foundation::BOOL,
windows_sys::Win32::Security::SECURITY_ATTRIBUTES,
windows_sys::Win32::Storage::FileSystem::{
FILE_ACCESS_FLAGS, FILE_CREATION_DISPOSITION, FILE_FLAGS_AND_ATTRIBUTES, FILE_SHARE_MODE,
},
windows_sys::Win32::System::IO::OVERLAPPED,
};
// Declare a few FFI functions ourselves, to show off the FFI ergonomics.
#[cfg(all(io_safety_is_in_std, any(unix, target_os = "wasi")))]
extern "C" {
pub fn open(pathname: *const c_char, flags: c_int, ...) -> Option<OwnedFd>;
}
#[cfg(any(unix, target_os = "wasi"))]
extern "C" {
pub fn read(fd: BorrowedFd<'_>, ptr: *mut c_void, size: size_t) -> ssize_t;
pub fn write(fd: BorrowedFd<'_>, ptr: *const c_void, size: size_t) -> ssize_t;
}
#[cfg(any(unix, target_os = "wasi"))]
pub use libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY};
// The Windows analogs of the above. Note the use of [`HandleOrInvalid`] as
// the return type for `CreateFileW`, since that function is defined to return
// [`INVALID_HANDLE_VALUE`] on error instead of null.
#[cfg(windows)]
extern "system" {
pub fn CreateFileW(
lpfilename: PCWSTR,
dwdesiredaccess: FILE_ACCESS_FLAGS,
dwsharemode: FILE_SHARE_MODE,
lpsecurityattributes: *const SECURITY_ATTRIBUTES,
dwcreationdisposition: FILE_CREATION_DISPOSITION,
dwflagsandattributes: FILE_FLAGS_AND_ATTRIBUTES,
htemplatefile: HANDLE,
) -> HandleOrInvalid;
pub fn ReadFile(
hfile: BorrowedHandle<'_>,
lpbuffer: *mut c_void,
nnumberofbytestoread: u32,
lpnumberofbytesread: *mut u32,
lpoverlapped: *mut OVERLAPPED,
) -> BOOL;
pub fn WriteFile(
hfile: BorrowedHandle<'_>,
lpbuffer: *const c_void,
nnumberofbytestowrite: u32,
lpnumberofbyteswritten: *mut u32,
lpoverlapped: *mut OVERLAPPED,
) -> BOOL;
}
#[cfg(windows)]
pub use {
windows_sys::Win32::Foundation::HANDLE,
windows_sys::Win32::Storage::FileSystem::{
CREATE_ALWAYS, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, FILE_GENERIC_READ, FILE_GENERIC_WRITE,
OPEN_EXISTING,
},
};

View file

@ -0,0 +1,509 @@
//! Implementations of io-lifetimes' traits for async-std's types. In the
//! future, we'll prefer to have crates provide their own impls; this is
//! just a temporary measure.
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
#[cfg(windows)]
use crate::{
AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, FromHandle, FromSocket, IntoHandle,
IntoSocket, OwnedHandle, OwnedSocket,
};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{
AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket,
};
unsafe impl FilelikeViewType for async_std::fs::File {}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for async_std::fs::File {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for async_std::fs::File {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for async_std::fs::File {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<async_std::fs::File> for OwnedFd {
#[inline]
fn from(owned: async_std::fs::File) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for async_std::fs::File {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<async_std::fs::File> for OwnedHandle {
#[inline]
fn from(owned: async_std::fs::File) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for async_std::fs::File {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for async_std::fs::File {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromHandle for async_std::fs::File {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<OwnedHandle> for async_std::fs::File {
#[inline]
fn from(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for async_std::net::TcpStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for async_std::net::TcpStream {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for async_std::net::TcpStream {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<async_std::net::TcpStream> for OwnedFd {
#[inline]
fn from(owned: async_std::net::TcpStream) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for async_std::net::TcpStream {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<async_std::net::TcpStream> for OwnedSocket {
#[inline]
fn from(owned: async_std::net::TcpStream) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for async_std::net::TcpStream {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for async_std::net::TcpStream {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for async_std::net::TcpStream {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for async_std::net::TcpStream {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for async_std::net::TcpListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for async_std::net::TcpListener {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for async_std::net::TcpListener {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<async_std::net::TcpListener> for OwnedFd {
#[inline]
fn from(owned: async_std::net::TcpListener) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for async_std::net::TcpListener {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<async_std::net::TcpListener> for OwnedSocket {
#[inline]
fn from(owned: async_std::net::TcpListener) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for async_std::net::TcpListener {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for async_std::net::TcpListener {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for async_std::net::TcpListener {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for async_std::net::TcpListener {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for async_std::net::UdpSocket {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for async_std::net::UdpSocket {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for async_std::net::UdpSocket {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<async_std::net::UdpSocket> for OwnedFd {
#[inline]
fn from(owned: async_std::net::UdpSocket) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for async_std::net::UdpSocket {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<async_std::net::UdpSocket> for OwnedSocket {
#[inline]
fn from(owned: async_std::net::UdpSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for async_std::net::UdpSocket {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for async_std::net::UdpSocket {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for async_std::net::UdpSocket {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for async_std::net::UdpSocket {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for async_std::io::Stdin {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for async_std::io::Stdin {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for async_std::io::Stdout {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for async_std::io::Stdout {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for async_std::io::Stderr {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for async_std::io::Stderr {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(unix)]
impl AsFd for async_std::os::unix::net::UnixStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for async_std::os::unix::net::UnixStream {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<async_std::os::unix::net::UnixStream> for OwnedFd {
#[inline]
fn from(owned: async_std::os::unix::net::UnixStream) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for async_std::os::unix::net::UnixStream {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for async_std::os::unix::net::UnixStream {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for async_std::os::unix::net::UnixListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for async_std::os::unix::net::UnixListener {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<async_std::os::unix::net::UnixListener> for OwnedFd {
#[inline]
fn from(owned: async_std::os::unix::net::UnixListener) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for async_std::os::unix::net::UnixListener {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for async_std::os::unix::net::UnixListener {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for async_std::os::unix::net::UnixDatagram {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for async_std::os::unix::net::UnixDatagram {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<async_std::os::unix::net::UnixDatagram> for OwnedFd {
#[inline]
fn from(owned: async_std::os::unix::net::UnixDatagram) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for async_std::os::unix::net::UnixDatagram {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for async_std::os::unix::net::UnixDatagram {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}

62
vendor/io-lifetimes/src/impls_fs_err.rs vendored Normal file
View file

@ -0,0 +1,62 @@
//! Implementations of io-lifetimes' traits for fs_err's types. In the
//! future, we'll prefer to have crates provide their own impls; this is
//! just a temporary measure.
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, BorrowedFd, IntoFd, OwnedFd};
#[cfg(windows)]
use crate::{AsHandle, BorrowedHandle, IntoHandle, OwnedHandle};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle};
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for fs_err::File {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for fs_err::File {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for fs_err::File {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<fs_err::File> for OwnedFd {
#[inline]
fn from(owned: fs_err::File) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for fs_err::File {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<fs_err::File> for OwnedHandle {
#[inline]
fn from(owned: fs_err::File) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}

454
vendor/io-lifetimes/src/impls_mio.rs vendored Normal file
View file

@ -0,0 +1,454 @@
//! Implementations of io-lifetimes' traits for mio's types. In the
//! future, we'll prefer to have crates provide their own impls; this is
//! just a temporary measure.
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
#[cfg(windows)]
use crate::{AsSocket, BorrowedSocket, FromSocket, IntoSocket, OwnedSocket};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket};
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for mio::net::TcpStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for mio::net::TcpStream {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for mio::net::TcpStream {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<mio::net::TcpStream> for OwnedFd {
#[inline]
fn from(owned: mio::net::TcpStream) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for mio::net::TcpStream {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<mio::net::TcpStream> for OwnedSocket {
#[inline]
fn from(owned: mio::net::TcpStream) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for mio::net::TcpStream {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for mio::net::TcpStream {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for mio::net::TcpStream {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for mio::net::TcpStream {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for mio::net::TcpListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for mio::net::TcpListener {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for mio::net::TcpListener {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<mio::net::TcpListener> for OwnedFd {
#[inline]
fn from(owned: mio::net::TcpListener) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for mio::net::TcpListener {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<mio::net::TcpListener> for OwnedSocket {
#[inline]
fn from(owned: mio::net::TcpListener) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for mio::net::TcpListener {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for mio::net::TcpListener {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for mio::net::TcpListener {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for mio::net::TcpListener {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for mio::net::UdpSocket {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for mio::net::UdpSocket {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for mio::net::UdpSocket {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<mio::net::UdpSocket> for OwnedFd {
#[inline]
fn from(owned: mio::net::UdpSocket) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for mio::net::UdpSocket {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<mio::net::UdpSocket> for OwnedSocket {
#[inline]
fn from(owned: mio::net::UdpSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for mio::net::UdpSocket {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for mio::net::UdpSocket {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for mio::net::UdpSocket {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for mio::net::UdpSocket {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(unix)]
impl AsFd for mio::net::UnixDatagram {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for mio::net::UnixDatagram {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<mio::net::UnixDatagram> for OwnedFd {
#[inline]
fn from(owned: mio::net::UnixDatagram) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for mio::net::UnixDatagram {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for mio::net::UnixDatagram {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for mio::net::UnixListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for mio::net::UnixListener {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<mio::net::UnixListener> for OwnedFd {
#[inline]
fn from(owned: mio::net::UnixListener) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for mio::net::UnixListener {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for mio::net::UnixListener {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for mio::net::UnixStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for mio::net::UnixStream {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<mio::net::UnixStream> for OwnedFd {
#[inline]
fn from(owned: mio::net::UnixStream) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for mio::net::UnixStream {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for mio::net::UnixStream {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for mio::unix::pipe::Receiver {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for mio::unix::pipe::Receiver {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<mio::unix::pipe::Receiver> for OwnedFd {
#[inline]
fn from(owned: mio::unix::pipe::Receiver) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for mio::unix::pipe::Receiver {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for mio::unix::pipe::Receiver {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for mio::unix::pipe::Sender {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for mio::unix::pipe::Sender {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<mio::unix::pipe::Sender> for OwnedFd {
#[inline]
fn from(owned: mio::unix::pipe::Sender) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for mio::unix::pipe::Sender {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for mio::unix::pipe::Sender {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}

174
vendor/io-lifetimes/src/impls_os_pipe.rs vendored Normal file
View file

@ -0,0 +1,174 @@
//! Implementations of io-lifetimes' traits for os_pipe's types. In the
//! future, we'll prefer to have crates provide their own impls; this is
//! just a temporary measure.
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
#[cfg(windows)]
use crate::{AsHandle, BorrowedHandle, FromHandle, IntoHandle, OwnedHandle};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle};
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for os_pipe::PipeReader {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for os_pipe::PipeReader {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for os_pipe::PipeReader {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<os_pipe::PipeReader> for OwnedFd {
#[inline]
fn from(owned: os_pipe::PipeReader) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for os_pipe::PipeReader {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<os_pipe::PipeReader> for OwnedHandle {
#[inline]
fn from(owned: os_pipe::PipeReader) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for os_pipe::PipeReader {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for os_pipe::PipeReader {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromHandle for os_pipe::PipeReader {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<OwnedHandle> for os_pipe::PipeReader {
#[inline]
fn from(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for os_pipe::PipeWriter {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for os_pipe::PipeWriter {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for os_pipe::PipeWriter {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<os_pipe::PipeWriter> for OwnedFd {
#[inline]
fn from(owned: os_pipe::PipeWriter) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for os_pipe::PipeWriter {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<os_pipe::PipeWriter> for OwnedHandle {
#[inline]
fn from(owned: os_pipe::PipeWriter) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for os_pipe::PipeWriter {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for os_pipe::PipeWriter {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromHandle for os_pipe::PipeWriter {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<OwnedHandle> for os_pipe::PipeWriter {
#[inline]
fn from(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}

View file

@ -0,0 +1,94 @@
//! Implementations of io-lifetimes' traits for socket2's types. In the
//! future, we'll prefer to have crates provide their own impls; this is
//! just a temporary measure.
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, BorrowedFd, FromFd, IntoFd, OwnedFd};
#[cfg(windows)]
use crate::{AsSocket, BorrowedSocket, FromSocket, IntoSocket, OwnedSocket};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, FromRawSocket, IntoRawSocket};
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for socket2::Socket {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for socket2::Socket {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for socket2::Socket {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<socket2::Socket> for OwnedFd {
#[inline]
fn from(owned: socket2::Socket) -> Self {
unsafe { OwnedFd::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for socket2::Socket {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<socket2::Socket> for OwnedSocket {
#[inline]
fn from(owned: socket2::Socket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for socket2::Socket {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for socket2::Socket {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for socket2::Socket {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for socket2::Socket {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}

890
vendor/io-lifetimes/src/impls_std.rs vendored Normal file
View file

@ -0,0 +1,890 @@
#![allow(deprecated)] // Don't warn on `IntoFd` and `FromFd` impls.
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, FromFd, IntoFd};
#[cfg(windows)]
use crate::{AsHandle, AsSocket, FromHandle, FromSocket, IntoHandle, IntoSocket};
#[cfg(any(unix, target_os = "wasi"))]
use crate::{BorrowedFd, OwnedFd};
#[cfg(windows)]
use crate::{BorrowedHandle, BorrowedSocket, HandleOrInvalid, OwnedHandle, OwnedSocket};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{
AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket,
};
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for BorrowedFd<'_> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for BorrowedHandle<'_> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(windows)]
impl AsSocket for BorrowedSocket<'_> {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for OwnedFd {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for OwnedHandle {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(windows)]
impl AsSocket for OwnedSocket {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for OwnedFd {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { Self::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for OwnedHandle {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { Self::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl IntoSocket for OwnedSocket {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { Self::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for OwnedFd {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromHandle for OwnedHandle {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl FromSocket for OwnedSocket {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl FromHandle for HandleOrInvalid {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<OwnedHandle> for HandleOrInvalid {
#[inline]
fn from(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for std::fs::File {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for std::fs::File {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for std::fs::File {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<std::fs::File> for OwnedFd {
#[inline]
fn from(owned: std::fs::File) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for std::fs::File {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<std::fs::File> for OwnedHandle {
#[inline]
fn from(owned: std::fs::File) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for std::fs::File {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for std::fs::File {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromHandle for std::fs::File {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<OwnedHandle> for std::fs::File {
#[inline]
fn from(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for std::net::TcpStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for std::net::TcpStream {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for std::net::TcpStream {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<std::net::TcpStream> for OwnedFd {
#[inline]
fn from(owned: std::net::TcpStream) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for std::net::TcpStream {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<std::net::TcpStream> for OwnedSocket {
#[inline]
fn from(owned: std::net::TcpStream) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for std::net::TcpStream {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for std::net::TcpStream {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for std::net::TcpStream {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for std::net::TcpStream {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for std::net::TcpListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for std::net::TcpListener {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for std::net::TcpListener {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<std::net::TcpListener> for OwnedFd {
#[inline]
fn from(owned: std::net::TcpListener) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for std::net::TcpListener {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<std::net::TcpListener> for OwnedSocket {
#[inline]
fn from(owned: std::net::TcpListener) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for std::net::TcpListener {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for std::net::TcpListener {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for std::net::TcpListener {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for std::net::TcpListener {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for std::net::UdpSocket {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for std::net::UdpSocket {
#[inline]
fn as_socket(&self) -> BorrowedSocket {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoFd for std::net::UdpSocket {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<std::net::UdpSocket> for OwnedFd {
#[inline]
fn from(owned: std::net::UdpSocket) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoSocket for std::net::UdpSocket {
#[inline]
fn into_socket(self) -> OwnedSocket {
unsafe { OwnedSocket::from_raw_socket(self.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<std::net::UdpSocket> for OwnedSocket {
#[inline]
fn from(owned: std::net::UdpSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for std::net::UdpSocket {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for std::net::UdpSocket {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromSocket for std::net::UdpSocket {
#[inline]
fn from_socket(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(windows)]
impl From<OwnedSocket> for std::net::UdpSocket {
#[inline]
fn from(owned: OwnedSocket) -> Self {
unsafe { Self::from_raw_socket(owned.into_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for std::io::Stdin {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for std::io::Stdin {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl<'a> AsFd for std::io::StdinLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl<'a> AsHandle for std::io::StdinLock<'a> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for std::io::Stdout {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for std::io::Stdout {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl<'a> AsFd for std::io::StdoutLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl<'a> AsHandle for std::io::StdoutLock<'a> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for std::io::Stderr {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for std::io::Stderr {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl<'a> AsFd for std::io::StderrLock<'a> {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl<'a> AsHandle for std::io::StderrLock<'a> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(unix)]
impl AsFd for std::process::ChildStdin {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for std::process::ChildStdin {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(unix)]
impl IntoFd for std::process::ChildStdin {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<std::process::ChildStdin> for OwnedFd {
#[inline]
fn from(owned: std::process::ChildStdin) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for std::process::ChildStdin {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<std::process::ChildStdin> for OwnedHandle {
#[inline]
fn from(owned: std::process::ChildStdin) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(unix)]
impl AsFd for std::process::ChildStdout {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for std::process::ChildStdout {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(unix)]
impl IntoFd for std::process::ChildStdout {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<std::process::ChildStdout> for OwnedFd {
#[inline]
fn from(owned: std::process::ChildStdout) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for std::process::ChildStdout {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<std::process::ChildStdout> for OwnedHandle {
#[inline]
fn from(owned: std::process::ChildStdout) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(unix)]
impl AsFd for std::process::ChildStderr {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for std::process::ChildStderr {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(unix)]
impl IntoFd for std::process::ChildStderr {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<std::process::ChildStderr> for OwnedFd {
#[inline]
fn from(owned: std::process::ChildStderr) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl IntoHandle for std::process::ChildStderr {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<std::process::ChildStderr> for OwnedHandle {
#[inline]
fn from(owned: std::process::ChildStderr) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(unix)]
impl FromFd for std::process::Stdio {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for std::process::Stdio {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromHandle for std::process::Stdio {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<OwnedHandle> for std::process::Stdio {
#[inline]
fn from(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl AsHandle for std::process::Child {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(windows)]
impl IntoHandle for std::process::Child {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<std::process::Child> for OwnedHandle {
#[inline]
fn from(owned: std::process::Child) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(unix)]
impl AsFd for std::os::unix::net::UnixStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for std::os::unix::net::UnixStream {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<std::os::unix::net::UnixStream> for OwnedFd {
#[inline]
fn from(owned: std::os::unix::net::UnixStream) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for std::os::unix::net::UnixStream {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for std::os::unix::net::UnixStream {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for std::os::unix::net::UnixListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for std::os::unix::net::UnixListener {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<std::os::unix::net::UnixListener> for OwnedFd {
#[inline]
fn from(owned: std::os::unix::net::UnixListener) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for std::os::unix::net::UnixListener {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for std::os::unix::net::UnixListener {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for std::os::unix::net::UnixDatagram {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl IntoFd for std::os::unix::net::UnixDatagram {
#[inline]
fn into_fd(self) -> OwnedFd {
unsafe { OwnedFd::from_raw_fd(self.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<std::os::unix::net::UnixDatagram> for OwnedFd {
#[inline]
fn from(owned: std::os::unix::net::UnixDatagram) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl FromFd for std::os::unix::net::UnixDatagram {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(unix)]
impl From<OwnedFd> for std::os::unix::net::UnixDatagram {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl<T> AsHandle for std::thread::JoinHandle<T> {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(windows)]
impl<T> IntoHandle for std::thread::JoinHandle<T> {
#[inline]
fn into_handle(self) -> OwnedHandle {
unsafe { OwnedHandle::from_raw_handle(self.into_raw_handle()) }
}
}
#[cfg(windows)]
impl<T> From<std::thread::JoinHandle<T>> for OwnedHandle {
#[inline]
fn from(owned: std::thread::JoinHandle<T>) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}

214
vendor/io-lifetimes/src/impls_tokio.rs vendored Normal file
View file

@ -0,0 +1,214 @@
//! Implementations of io-lifetimes' traits for tokio's types. In the
//! future, we'll prefer to have crates provide their own impls; this is
//! just a temporary measure.
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, BorrowedFd, FromFd, OwnedFd};
#[cfg(windows)]
use crate::{AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, FromHandle, OwnedHandle};
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd};
#[cfg(windows)]
use std::os::windows::io::{AsRawHandle, AsRawSocket, FromRawHandle, IntoRawHandle};
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::fs::File {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for tokio::fs::File {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromFd for tokio::fs::File {
#[inline]
fn from_fd(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl From<OwnedFd> for tokio::fs::File {
#[inline]
fn from(owned: OwnedFd) -> Self {
unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}
#[cfg(windows)]
impl FromHandle for tokio::fs::File {
#[inline]
fn from_handle(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(windows)]
impl From<OwnedHandle> for tokio::fs::File {
#[inline]
fn from(owned: OwnedHandle) -> Self {
unsafe { Self::from_raw_handle(owned.into_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::net::TcpStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for tokio::net::TcpStream {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::net::TcpListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for tokio::net::TcpListener {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::net::UdpSocket {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsSocket for tokio::net::UdpSocket {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
unsafe { BorrowedSocket::borrow_raw(self.as_raw_socket()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::io::Stdin {
#[inline]
fn as_fd(&self) -> BorrowedFd {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for tokio::io::Stdin {
#[inline]
fn as_handle(&self) -> BorrowedHandle {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::io::Stdout {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for tokio::io::Stdout {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::io::Stderr {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for tokio::io::Stderr {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(unix)]
impl AsFd for tokio::net::UnixStream {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for tokio::net::UnixListener {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(unix)]
impl AsFd for tokio::net::UnixDatagram {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsFd for tokio::process::ChildStdout {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
}
}
#[cfg(windows)]
impl AsHandle for tokio::process::ChildStdin {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(windows)]
impl AsHandle for tokio::process::ChildStdout {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}
#[cfg(windows)]
impl AsHandle for tokio::process::ChildStderr {
#[inline]
fn as_handle(&self) -> BorrowedHandle {
unsafe { BorrowedHandle::borrow_raw(self.as_raw_handle()) }
}
}

187
vendor/io-lifetimes/src/lib.rs vendored Normal file
View file

@ -0,0 +1,187 @@
//! Experimental new types and traits to replace the `Raw` family of types and
//! traits.
//!
//! This API has much conceptual similarity with the `Raw` API, but introduces
//! explicit concepts of ownership and borrowing:
//!
//! | `Raw` API | This experimental API |
//! | ---------- | ------------------------ |
//! | `Raw*` | `Borrowed*` and `Owned*` |
//! | `AsRaw*` | `As*` |
//! | `IntoRaw*` | `Into*` |
//! | `FromRaw*` | `From*` |
//!
//! This gives it several advantages:
//!
//! - Less `unsafe` in user code!
//!
//! - Easier to understand ownership.
//!
//! - It avoids the inconsistency where `AsRawFd` and `IntoRawFd` return
//! `RawFd` values that users ought to be able to trust, but aren't unsafe,
//! so it's possible to fail to uphold this trust in purely safe Rust.
//!
//! - It enables a number of safe and portable convenience features, such as
//! [safe typed views] and [from+into conversions].
//!
//! [safe typed views]: AsFilelike::as_filelike_view
//! [from+into conversions]: FromFilelike::from_into_filelike
#![deny(missing_docs)]
// Work around https://github.com/rust-lang/rust/issues/103306.
#![cfg_attr(all(wasi_ext, target_os = "wasi"), feature(wasi_ext))]
// Currently supported platforms.
#![cfg(any(unix, windows, target_os = "wasi"))]
mod portability;
mod traits;
#[cfg(not(io_safety_is_in_std))]
mod types;
#[cfg(not(io_safety_is_in_std))]
mod impls_std;
#[cfg(not(io_safety_is_in_std))]
#[cfg(any(unix, target_os = "wasi"))]
pub use traits::AsFd;
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
pub use traits::{AsHandle, AsSocket};
#[cfg(any(unix, target_os = "wasi"))]
#[allow(deprecated)]
pub use traits::{FromFd, IntoFd};
#[cfg(windows)]
#[allow(deprecated)]
pub use traits::{FromHandle, FromSocket, IntoHandle, IntoSocket};
#[cfg(not(io_safety_is_in_std))]
#[cfg(any(unix, target_os = "wasi"))]
pub use types::{BorrowedFd, OwnedFd};
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
pub use types::{
BorrowedHandle, BorrowedSocket, HandleOrInvalid, InvalidHandleError, NullHandleError,
OwnedHandle, OwnedSocket,
};
#[cfg(io_safety_is_in_std)]
#[cfg(unix)]
pub use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
#[cfg(io_safety_is_in_std)]
#[cfg(target_os = "wasi")]
pub use std::os::wasi::io::{AsFd, BorrowedFd, OwnedFd};
#[cfg(io_safety_is_in_std)]
#[cfg(windows)]
pub use std::os::windows::io::{
AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, HandleOrInvalid, InvalidHandleError,
NullHandleError, OwnedHandle, OwnedSocket,
};
// io-lifetimes defined `FromFd`/`IntoFd` traits instead of just using
// `From`/`Into` because that allowed it to implement them for foreign types,
// including std types like File and TcpStream, and popular third-party types.
//
// std just uses `From`/`Into`, because it defines those traits itself so it
// can implement them for std types itself, and std won't be implementing them
// for third-party types. However, this means that until `OwnedFd` et al are
// stabilized, there will be no impls for third-party traits.
//
// So we define `FromFd`/`IntoFd` traits, and implement them in terms of
// `From`/`Into`,
#[cfg(io_safety_is_in_std)]
#[cfg(any(unix, target_os = "wasi"))]
#[allow(deprecated)]
impl<T: From<OwnedFd>> FromFd for T {
#[inline]
fn from_fd(owned_fd: OwnedFd) -> Self {
owned_fd.into()
}
}
#[cfg(io_safety_is_in_std)]
#[cfg(any(unix, target_os = "wasi"))]
#[allow(deprecated)]
impl<T> IntoFd for T
where
OwnedFd: From<T>,
{
#[inline]
fn into_fd(self) -> OwnedFd {
self.into()
}
}
#[cfg(io_safety_is_in_std)]
#[cfg(windows)]
#[allow(deprecated)]
impl<T: From<OwnedHandle>> FromHandle for T {
#[inline]
fn from_handle(owned_handle: OwnedHandle) -> Self {
owned_handle.into()
}
}
#[cfg(io_safety_is_in_std)]
#[cfg(windows)]
#[allow(deprecated)]
impl<T> IntoHandle for T
where
OwnedHandle: From<T>,
{
#[inline]
fn into_handle(self) -> OwnedHandle {
self.into()
}
}
#[cfg(io_safety_is_in_std)]
#[cfg(windows)]
#[allow(deprecated)]
impl<T: From<OwnedSocket>> FromSocket for T {
#[inline]
fn from_socket(owned_socket: OwnedSocket) -> Self {
owned_socket.into()
}
}
#[cfg(io_safety_is_in_std)]
#[cfg(windows)]
#[allow(deprecated)]
impl<T> IntoSocket for T
where
OwnedSocket: From<T>,
{
#[inline]
fn into_socket(self) -> OwnedSocket {
self.into()
}
}
pub use portability::{
AsFilelike, AsSocketlike, BorrowedFilelike, BorrowedSocketlike, FromFilelike, FromSocketlike,
IntoFilelike, IntoSocketlike, OwnedFilelike, OwnedSocketlike,
};
#[cfg(feature = "close")]
pub mod example_ffi;
pub mod raw;
pub mod views;
// Ideally, we'd want crates to implement our traits themselves. But for now,
// while we're prototyping, we provide a few impls on foreign types.
#[cfg(not(io_safety_is_in_std))]
#[cfg(feature = "async-std")]
mod impls_async_std;
#[cfg(not(io_safety_is_in_std))]
#[cfg(feature = "fs-err")]
mod impls_fs_err;
#[cfg(not(io_safety_is_in_std))]
#[cfg(feature = "mio")]
mod impls_mio;
#[cfg(not(target_os = "wasi"))]
#[cfg(not(io_safety_is_in_std))]
#[cfg(feature = "os_pipe")]
mod impls_os_pipe;
#[cfg(not(io_safety_is_in_std))]
#[cfg(feature = "socket2")]
mod impls_socket2;
#[cfg(not(io_safety_is_in_std))]
#[cfg(feature = "tokio")]
mod impls_tokio;

521
vendor/io-lifetimes/src/portability.rs vendored Normal file
View file

@ -0,0 +1,521 @@
//! Portability abstractions over `Owned*` and `Borrowed*`.
//!
//! On Unix, "everything is a file descriptor". On Windows, file/pipe/process
//! handles are distinct from socket descriptors. This file provides a minimal
//! layer of portability over this difference.
use crate::views::{FilelikeView, FilelikeViewType, SocketlikeView, SocketlikeViewType};
#[cfg(any(unix, target_os = "wasi"))]
use crate::{AsFd, BorrowedFd, OwnedFd};
#[cfg(windows)]
use crate::{AsHandle, AsSocket, BorrowedHandle, BorrowedSocket, OwnedHandle, OwnedSocket};
/// A reference to a filelike object.
///
/// This is a portability abstraction over Unix-like [`BorrowedFd`] and
/// Windows' `BorrowedHandle`.
#[cfg(any(unix, target_os = "wasi"))]
pub type BorrowedFilelike<'filelike> = BorrowedFd<'filelike>;
/// A reference to a filelike object.
///
/// This is a portability abstraction over Unix-like `BorrowedFd` and
/// Windows' [`BorrowedHandle`].
#[cfg(windows)]
pub type BorrowedFilelike<'filelike> = BorrowedHandle<'filelike>;
/// A reference to a socketlike object.
///
/// This is a portability abstraction over Unix-like [`BorrowedFd`] and
/// Windows' `BorrowedSocket`.
#[cfg(any(unix, target_os = "wasi"))]
pub type BorrowedSocketlike<'socketlike> = BorrowedFd<'socketlike>;
/// A reference to a socketlike object.
///
/// This is a portability abstraction over Unix-like `BorrowedFd` and
/// Windows' [`BorrowedSocket`].
#[cfg(windows)]
pub type BorrowedSocketlike<'socketlike> = BorrowedSocket<'socketlike>;
/// An owned filelike object.
///
/// This is a portability abstraction over Unix-like [`OwnedFd`] and
/// Windows' `OwnedHandle`.
#[cfg(any(unix, target_os = "wasi"))]
pub type OwnedFilelike = OwnedFd;
/// An owned filelike object.
///
/// This is a portability abstraction over Unix-like `OwnedFd` and
/// Windows' [`OwnedHandle`].
#[cfg(windows)]
pub type OwnedFilelike = OwnedHandle;
/// An owned socketlike object.
///
/// This is a portability abstraction over Unix-like [`OwnedFd`] and
/// Windows' `OwnedSocket`.
#[cfg(any(unix, target_os = "wasi"))]
pub type OwnedSocketlike = OwnedFd;
/// An owned socketlike object.
///
/// This is a portability abstraction over Unix-like `OwnedFd` and
/// Windows' [`OwnedSocket`].
#[cfg(windows)]
pub type OwnedSocketlike = OwnedSocket;
/// A portable trait to borrow a reference from an underlying filelike object.
///
/// This is a portability abstraction over Unix-like [`AsFd`] and Windows'
/// `AsHandle`. It also provides the `as_filelike_view` convenience function
/// providing typed views.
#[cfg(any(unix, target_os = "wasi"))]
pub trait AsFilelike: AsFd {
/// Borrows the reference.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{AsFilelike, BorrowedFilelike};
///
/// let mut f = File::open("foo.txt")?;
/// let borrowed_filelike: BorrowedFilelike<'_> = f.as_filelike();
/// # Ok::<(), io::Error>(())
/// ```
fn as_filelike(&self) -> BorrowedFilelike<'_>;
/// Return a borrowing view of a resource which dereferences to a `&Target`.
///
/// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases,
/// such as [`File`], `Read` and `Write` are implemented for `&Target` in
/// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on
/// the resuting view, like this:
///
/// ```rust,ignore
/// let v = f.as_filelike_view::<std::fs::File>();
/// (&*v).read(&mut buf).unwrap();
/// ```
///
/// [`File`]: std::fs::File
/// [`Read`]: std::io::Read
/// [`Write`]: std::io::Write
fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target>;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsFd> AsFilelike for T {
#[inline]
fn as_filelike(&self) -> BorrowedFilelike<'_> {
self.as_fd()
}
#[inline]
fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target> {
FilelikeView::new(self)
}
}
/// A portable trait to borrow a reference from an underlying filelike object.
///
/// This is a portability abstraction over Unix-like `AsFd` and Windows'
/// [`AsHandle`]. It also provides the `as_filelike_view` convenience function
/// providing typed views.
#[cfg(windows)]
pub trait AsFilelike: AsHandle {
/// Borrows the reference.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{AsFilelike, BorrowedFilelike};
///
/// let mut f = File::open("foo.txt")?;
/// let borrowed_filelike: BorrowedFilelike<'_> = f.as_filelike();
/// # Ok::<(), io::Error>(())
/// ```
fn as_filelike(&self) -> BorrowedFilelike<'_>;
/// Return a borrowing view of a resource which dereferences to a `&Target`.
///
/// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases,
/// such as [`File`], `Read` and `Write` are implemented for `&Target` in
/// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on
/// the resuting view, like this:
///
/// ```rust,ignore
/// let v = f.as_filelike_view::<std::fs::File>();
/// (&*v).read(&mut buf).unwrap();
/// ```
///
/// [`File`]: std::fs::File
/// [`Read`]: std::io::Read
/// [`Write`]: std::io::Write
fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target>;
}
#[cfg(windows)]
impl<T: AsHandle> AsFilelike for T {
#[inline]
fn as_filelike(&self) -> BorrowedFilelike<'_> {
self.as_handle()
}
#[inline]
fn as_filelike_view<Target: FilelikeViewType>(&self) -> FilelikeView<'_, Target> {
FilelikeView::new(self)
}
}
/// A portable trait to borrow a reference from an underlying socketlike
/// object.
///
/// This is a portability abstraction over Unix-like [`AsFd`] and Windows'
/// `AsSocket`. It also provides the `as_socketlike_view` convenience
/// function providing typed views.
#[cfg(any(unix, target_os = "wasi"))]
pub trait AsSocketlike: AsFd {
/// Borrows the reference.
fn as_socketlike(&self) -> BorrowedSocketlike<'_>;
/// Return a borrowing view of a resource which dereferences to a `&Target`.
///
/// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases,
/// such as [`TcpStream`], `Read` and `Write` are implemented for `&Target` in
/// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on
/// the resuting view, like this:
///
/// ```rust,ignore
/// let v = s.as_socketlike_view::<std::net::TcpStream>();
/// (&*v).read(&mut buf).unwrap();
/// ```
///
/// [`TcpStream`]: std::net::TcpStream
/// [`Read`]: std::io::Read
/// [`Write`]: std::io::Write
fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target>;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsFd> AsSocketlike for T {
#[inline]
fn as_socketlike(&self) -> BorrowedSocketlike<'_> {
self.as_fd()
}
#[inline]
fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target> {
SocketlikeView::new(self)
}
}
/// A portable trait to borrow a reference from an underlying socketlike
/// object.
///
/// This is a portability abstraction over Unix-like `AsFd` and Windows'
/// [`AsSocket`]. It also provides the `as_socketlike_view` convenience
/// function providing typed views.
#[cfg(windows)]
pub trait AsSocketlike: AsSocket {
/// Borrows the reference.
fn as_socketlike(&self) -> BorrowedSocketlike;
/// Return a borrowing view of a resource which dereferences to a `&Target`.
///
/// Note that [`Read`] or [`Write`] require `&mut Target`, but in some cases,
/// such as [`TcpStream`], `Read` and `Write` are implemented for `&Target` in
/// addition to `Target`, and you can get a `&mut &Target` by doing `&*` on
/// the resuting view, like this:
///
/// ```rust,ignore
/// let v = s.as_socketlike_view::<std::net::TcpStream>();
/// (&*v).read(&mut buf).unwrap();
/// ```
///
/// [`TcpStream`]: std::net::TcpStream
fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target>;
}
#[cfg(windows)]
impl<T: AsSocket> AsSocketlike for T {
#[inline]
fn as_socketlike(&self) -> BorrowedSocketlike<'_> {
self.as_socket()
}
#[inline]
fn as_socketlike_view<Target: SocketlikeViewType>(&self) -> SocketlikeView<'_, Target> {
SocketlikeView::new(self)
}
}
/// A portable trait to express the ability to consume an object and acquire
/// ownership of its filelike object.
///
/// This is a portability abstraction over Unix-like [`Into<OwnedFd>`] and Windows'
/// `Into<OwnedHandle>`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait IntoFilelike: Into<OwnedFd> {
/// Consumes this object, returning the underlying filelike object.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{IntoFilelike, OwnedFilelike};
///
/// let f = File::open("foo.txt")?;
/// let owned_filelike: OwnedFilelike = f.into_filelike();
/// # Ok::<(), io::Error>(())
/// ```
fn into_filelike(self) -> OwnedFilelike;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: Into<OwnedFd>> IntoFilelike for T {
#[inline]
fn into_filelike(self) -> OwnedFilelike {
self.into()
}
}
/// A portable trait to express the ability to consume an object and acquire
/// ownership of its filelike object.
///
/// This is a portability abstraction over Unix-like `Into<OwnedFd>` and Windows'
/// [`Into<OwnedHandle>`].
#[cfg(windows)]
pub trait IntoFilelike: Into<OwnedHandle> {
/// Consumes this object, returning the underlying filelike object.
fn into_filelike(self) -> OwnedFilelike;
}
#[cfg(windows)]
impl<T: Into<OwnedHandle>> IntoFilelike for T {
#[inline]
fn into_filelike(self) -> OwnedFilelike {
self.into()
}
}
/// A portable trait to express the ability to consume an object and acquire
/// ownership of its socketlike object.
///
/// This is a portability abstraction over Unix-like [`Into<OwnedFd>`] and Windows'
/// `Into<OwnedSocket>`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait IntoSocketlike: Into<OwnedFd> {
/// Consumes this object, returning the underlying socketlike object.
fn into_socketlike(self) -> OwnedSocketlike;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: Into<OwnedFd>> IntoSocketlike for T {
#[inline]
fn into_socketlike(self) -> OwnedSocketlike {
self.into()
}
}
/// A portable trait to express the ability to consume an object and acquire
/// ownership of its socketlike object.
///
/// This is a portability abstraction over Unix-like `Into<OwnedFd>` and Windows'
/// [`Into<OwnedSocket>`].
#[cfg(windows)]
pub trait IntoSocketlike: Into<OwnedSocket> {
/// Consumes this object, returning the underlying socketlike object.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{IntoFilelike, OwnedFilelike};
///
/// let f = File::open("foo.txt")?;
/// let owned_filelike: OwnedFilelike = f.into_filelike();
/// # Ok::<(), io::Error>(())
/// ```
fn into_socketlike(self) -> OwnedSocketlike;
}
#[cfg(windows)]
impl<T: Into<OwnedSocket>> IntoSocketlike for T {
#[inline]
fn into_socketlike(self) -> OwnedSocketlike {
self.into()
}
}
/// A portable trait to express the ability to construct an object from a
/// filelike object.
///
/// This is a portability abstraction over Unix-like [`From<OwnedFd>`] and Windows'
/// `From<OwnedHandle>`. It also provides the `from_into_filelike` convenience
/// function providing simplified from+into conversions.
#[cfg(any(unix, target_os = "wasi"))]
pub trait FromFilelike: From<OwnedFd> {
/// Constructs a new instance of `Self` from the given filelike object.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromFilelike, IntoFilelike, OwnedFilelike};
///
/// let f = File::open("foo.txt")?;
/// let owned_filelike: OwnedFilelike = f.into_filelike();
/// let f = File::from_filelike(owned_filelike);
/// # Ok::<(), io::Error>(())
/// ```
fn from_filelike(owned: OwnedFilelike) -> Self;
/// Constructs a new instance of `Self` from the given filelike object
/// converted from `into_owned`.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromFilelike, IntoFilelike};
///
/// let f = File::open("foo.txt")?;
/// let f = File::from_into_filelike(f);
/// # Ok::<(), io::Error>(())
/// ```
fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: From<OwnedFd>> FromFilelike for T {
#[inline]
fn from_filelike(owned: OwnedFilelike) -> Self {
Self::from(owned)
}
#[inline]
fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self {
Self::from_filelike(owned.into_filelike())
}
}
/// A portable trait to express the ability to construct an object from a
/// filelike object.
///
/// This is a portability abstraction over Unix-like `From<OwnedFd>` and Windows'
/// [`From<OwnedHandle>`]. It also provides the `from_into_filelike` convenience
/// function providing simplified from+into conversions.
#[cfg(windows)]
pub trait FromFilelike: From<OwnedHandle> {
/// Constructs a new instance of `Self` from the given filelike object.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromFilelike, IntoFilelike, OwnedFilelike};
///
/// let f = File::open("foo.txt")?;
/// let owned_filelike: OwnedFilelike = f.into_filelike();
/// let f = File::from_filelike(owned_filelike);
/// # Ok::<(), io::Error>(())
/// ```
fn from_filelike(owned: OwnedFilelike) -> Self;
/// Constructs a new instance of `Self` from the given filelike object
/// converted from `into_owned`.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromFilelike, IntoFilelike};
///
/// let f = File::open("foo.txt")?;
/// let f = File::from_into_filelike(f);
/// # Ok::<(), io::Error>(())
/// ```
fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self;
}
#[cfg(windows)]
impl<T: From<OwnedHandle>> FromFilelike for T {
#[inline]
fn from_filelike(owned: OwnedFilelike) -> Self {
Self::from(owned)
}
#[inline]
fn from_into_filelike<Owned: IntoFilelike>(owned: Owned) -> Self {
Self::from_filelike(owned.into_filelike())
}
}
/// A portable trait to express the ability to construct an object from a
/// socketlike object.
///
/// This is a portability abstraction over Unix-like [`From<OwnedFd>`] and Windows'
/// `From<OwnedSocketFrom<OwnedSocket> It also provides the `from_into_socketlike` convenience
/// function providing simplified from+into conversions.
#[cfg(any(unix, target_os = "wasi"))]
pub trait FromSocketlike: From<OwnedFd> {
/// Constructs a new instance of `Self` from the given socketlike object.
fn from_socketlike(owned: OwnedSocketlike) -> Self;
/// Constructs a new instance of `Self` from the given socketlike object
/// converted from `into_owned`.
fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: From<OwnedFd>> FromSocketlike for T {
#[inline]
fn from_socketlike(owned: OwnedSocketlike) -> Self {
Self::from(owned)
}
#[inline]
fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self {
Self::from_socketlike(owned.into_socketlike())
}
}
/// A portable trait to express the ability to construct an object from a
/// socketlike object.
///
/// This is a portability abstraction over Unix-like `From<OwnedFd>` and Windows'
/// [`From<OwnedSocket>`]. It also provides the `from_into_socketlike` convenience
/// function providing simplified from+into conversions.
#[cfg(windows)]
pub trait FromSocketlike: From<OwnedSocket> {
/// Constructs a new instance of `Self` from the given socketlike object.
fn from_socketlike(owned: OwnedSocketlike) -> Self;
/// Constructs a new instance of `Self` from the given socketlike object
/// converted from `into_owned`.
fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self;
}
#[cfg(windows)]
impl<T: From<OwnedSocket>> FromSocketlike for T {
#[inline]
fn from_socketlike(owned: OwnedSocketlike) -> Self {
Self::from(owned)
}
#[inline]
fn from_into_socketlike<Owned: IntoSocketlike>(owned: Owned) -> Self {
Self::from_socketlike(owned.into_socketlike())
}
}

253
vendor/io-lifetimes/src/raw.rs vendored Normal file
View file

@ -0,0 +1,253 @@
//! Portability abstractions over `Raw*`.
//!
//! On Unix, "everything is a file descriptor". On Windows, file/pipe/process
//! handles are distinct from socket descriptors. This file provides a minimal
//! layer of portability over this difference.
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::os::windows::io::{
AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket,
RawHandle, RawSocket,
};
/// A raw filelike object.
///
/// This is a portability abstraction over Unix-like [`RawFd`] and
/// Windows' `RawHandle`.
#[cfg(any(unix, target_os = "wasi"))]
pub type RawFilelike = RawFd;
/// A raw filelike object.
///
/// This is a portability abstraction over Unix-like `RawFd` and
/// Windows' [`RawHandle`].
#[cfg(windows)]
pub type RawFilelike = RawHandle;
/// A raw socketlike object.
///
/// This is a portability abstraction over Unix-like [`RawFd`] and
/// Windows' `RawSocket`.
#[cfg(any(unix, target_os = "wasi"))]
pub type RawSocketlike = RawFd;
/// A raw socketlike object.
///
/// This is a portability abstraction over Unix-like `RawFd` and
/// Windows' [`RawSocket`].
#[cfg(windows)]
pub type RawSocketlike = RawSocket;
/// A portable trait to obtain the raw value of an underlying filelike object.
///
/// This is a portability abstraction over Unix-like [`AsRawFd`] and Windows'
/// `AsRawHandle`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait AsRawFilelike: AsRawFd {
/// Returns the raw value.
fn as_raw_filelike(&self) -> RawFilelike;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsRawFd> AsRawFilelike for T {
#[inline]
fn as_raw_filelike(&self) -> RawFilelike {
self.as_raw_fd()
}
}
/// This is a portability abstraction over Unix-like `AsRawFd` and Windows'
/// [`AsRawHandle`].
#[cfg(windows)]
pub trait AsRawFilelike: AsRawHandle {
/// Returns the raw value.
fn as_raw_filelike(&self) -> RawFilelike;
}
#[cfg(windows)]
impl<T: AsRawHandle> AsRawFilelike for T {
#[inline]
fn as_raw_filelike(&self) -> RawFilelike {
self.as_raw_handle()
}
}
/// This is a portability abstraction over Unix-like [`AsRawFd`] and Windows'
/// `AsRawSocket`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait AsRawSocketlike: AsRawFd {
/// Returns the raw value.
fn as_raw_socketlike(&self) -> RawSocketlike;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsRawFd> AsRawSocketlike for T {
#[inline]
fn as_raw_socketlike(&self) -> RawSocketlike {
self.as_raw_fd()
}
}
/// This is a portability abstraction over Unix-like `AsRawFd` and Windows'
/// [`AsRawSocket`].
#[cfg(windows)]
pub trait AsRawSocketlike: AsRawSocket {
/// Returns the raw value.
fn as_raw_socketlike(&self) -> RawSocketlike;
}
#[cfg(windows)]
impl<T: AsRawSocket> AsRawSocketlike for T {
#[inline]
fn as_raw_socketlike(&self) -> RawSocketlike {
self.as_raw_socket()
}
}
/// This is a portability abstraction over Unix-like [`IntoRawFd`] and Windows'
/// `IntoRawHandle`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait IntoRawFilelike: IntoRawFd {
/// Returns the raw value.
fn into_raw_filelike(self) -> RawFilelike;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: IntoRawFd> IntoRawFilelike for T {
#[inline]
fn into_raw_filelike(self) -> RawFilelike {
self.into_raw_fd()
}
}
/// This is a portability abstraction over Unix-like `IntoRawFd` and Windows'
/// [`IntoRawHandle`].
#[cfg(windows)]
pub trait IntoRawFilelike: IntoRawHandle {
/// Returns the raw value.
fn into_raw_filelike(self) -> RawFilelike;
}
#[cfg(windows)]
impl<T: IntoRawHandle> IntoRawFilelike for T {
#[inline]
fn into_raw_filelike(self) -> RawFilelike {
self.into_raw_handle()
}
}
/// This is a portability abstraction over Unix-like [`IntoRawFd`] and Windows'
/// `IntoRawSocket`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait IntoRawSocketlike: IntoRawFd {
/// Returns the raw value.
fn into_raw_socketlike(self) -> RawSocketlike;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: IntoRawFd> IntoRawSocketlike for T {
#[inline]
fn into_raw_socketlike(self) -> RawSocketlike {
self.into_raw_fd()
}
}
/// This is a portability abstraction over Unix-like `IntoRawFd` and Windows'
/// [`IntoRawSocket`].
#[cfg(windows)]
pub trait IntoRawSocketlike: IntoRawSocket {
/// Returns the raw value.
fn into_raw_socketlike(self) -> RawSocketlike;
}
#[cfg(windows)]
impl<T: IntoRawSocket> IntoRawSocketlike for T {
#[inline]
fn into_raw_socketlike(self) -> RawSocketlike {
self.into_raw_socket()
}
}
/// This is a portability abstraction over Unix-like [`FromRawFd`] and Windows'
/// `FromRawHandle`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait FromRawFilelike: FromRawFd {
/// Constructs `Self` from the raw value.
///
/// # Safety
///
/// This is `unsafe` for the same reason as [`from_raw_fd`] and
/// [`from_raw_handle`].
///
/// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.from_raw_fd
/// [`from_raw_handle`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.FromRawHandle.html#tymethod.from_raw_handle
unsafe fn from_raw_filelike(raw: RawFilelike) -> Self;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: FromRawFd> FromRawFilelike for T {
#[inline]
unsafe fn from_raw_filelike(raw: RawFilelike) -> Self {
Self::from_raw_fd(raw)
}
}
/// This is a portability abstraction over Unix-like `FromRawFd` and Windows'
/// [`FromRawHandle`].
#[cfg(windows)]
pub trait FromRawFilelike: FromRawHandle {
/// Constructs `Self` from the raw value.
unsafe fn from_raw_filelike(raw: RawFilelike) -> Self;
}
#[cfg(windows)]
impl<T: FromRawHandle> FromRawFilelike for T {
#[inline]
unsafe fn from_raw_filelike(raw: RawFilelike) -> Self {
Self::from_raw_handle(raw)
}
}
/// This is a portability abstraction over Unix-like [`FromRawFd`] and Windows'
/// `FromRawSocket`.
#[cfg(any(unix, target_os = "wasi"))]
pub trait FromRawSocketlike: FromRawFd {
/// Constructs `Self` from the raw value.
///
/// # Safety
///
/// This is `unsafe` for the same reason as [`from_raw_fd`] and
/// [`from_raw_socket`].
///
/// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.from_raw_fd
/// [`from_raw_socket`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.FromRawSocket.html#tymethod.from_raw_socket
unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self;
}
#[cfg(any(unix, target_os = "wasi"))]
impl<T: FromRawFd> FromRawSocketlike for T {
#[inline]
unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self {
Self::from_raw_fd(raw)
}
}
/// This is a portability abstraction over Unix-like `FromRawFd` and Windows'
/// [`FromRawSocket`].
#[cfg(windows)]
pub trait FromRawSocketlike: FromRawSocket {
/// Constructs `Self` from the raw value.
unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self;
}
#[cfg(windows)]
impl<T: FromRawSocket> FromRawSocketlike for T {
#[inline]
unsafe fn from_raw_socketlike(raw: RawSocketlike) -> Self {
Self::from_raw_socket(raw)
}
}

290
vendor/io-lifetimes/src/traits.rs vendored Normal file
View file

@ -0,0 +1,290 @@
#[cfg(not(io_safety_is_in_std))]
#[cfg(any(unix, target_os = "wasi"))]
use crate::BorrowedFd;
#[cfg(any(unix, target_os = "wasi"))]
use crate::OwnedFd;
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
use crate::{BorrowedHandle, BorrowedSocket};
#[cfg(windows)]
use crate::{OwnedHandle, OwnedSocket};
/// A trait to borrow the file descriptor from an underlying object.
///
/// This is only available on unix platforms and must be imported in order to
/// call the method. Windows platforms have a corresponding `AsHandle` and
/// `AsSocket` set of traits.
#[cfg(not(io_safety_is_in_std))]
#[cfg(any(unix, target_os = "wasi"))]
pub trait AsFd {
/// Borrows the file descriptor.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{AsFd, BorrowedFd};
///
/// let mut f = File::open("foo.txt")?;
/// let borrowed_fd: BorrowedFd<'_> = f.as_fd();
/// # Ok::<(), io::Error>(())
/// ```
fn as_fd(&self) -> BorrowedFd<'_>;
}
/// A trait to borrow the handle from an underlying object.
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
pub trait AsHandle {
/// Borrows the handle.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{AsHandle, BorrowedHandle};
///
/// let mut f = File::open("foo.txt")?;
/// let borrowed_handle: BorrowedHandle<'_> = f.as_handle();
/// # Ok::<(), io::Error>(())
/// ```
fn as_handle(&self) -> BorrowedHandle<'_>;
}
/// A trait to borrow the socket from an underlying object.
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
pub trait AsSocket {
/// Borrows the socket.
fn as_socket(&self) -> BorrowedSocket<'_>;
}
/// A trait to express the ability to consume an object and acquire ownership
/// of its file descriptor.
#[cfg(any(unix, target_os = "wasi"))]
#[deprecated(
since = "1.0.0",
note = "`IntoFd` is replaced by `From<...> for OwnedFd` or `Into<OwnedFd>`"
)]
pub trait IntoFd {
/// Consumes this object, returning the underlying file descriptor.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{IntoFd, OwnedFd};
///
/// let f = File::open("foo.txt")?;
/// let owned_fd: OwnedFd = f.into_fd();
/// # Ok::<(), io::Error>(())
/// ```
fn into_fd(self) -> OwnedFd;
}
/// A trait to express the ability to consume an object and acquire ownership
/// of its handle.
#[cfg(windows)]
#[deprecated(
since = "1.0.0",
note = "`IntoHandle` is replaced by `From<...> for OwnedHandle` or `Into<OwnedHandle>`"
)]
pub trait IntoHandle {
/// Consumes this object, returning the underlying handle.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{IntoHandle, OwnedHandle};
///
/// let f = File::open("foo.txt")?;
/// let owned_handle: OwnedHandle = f.into_handle();
/// # Ok::<(), io::Error>(())
/// ```
fn into_handle(self) -> OwnedHandle;
}
/// A trait to express the ability to consume an object and acquire ownership
/// of its socket.
#[cfg(windows)]
#[deprecated(
since = "1.0.0",
note = "`IntoSocket` is replaced by `From<...> for OwnedSocket` or `Into<OwnedSocket>`"
)]
pub trait IntoSocket {
/// Consumes this object, returning the underlying socket.
fn into_socket(self) -> OwnedSocket;
}
/// A trait to express the ability to construct an object from a file
/// descriptor.
#[cfg(any(unix, target_os = "wasi"))]
pub trait FromFd {
/// Constructs a new instance of `Self` from the given file descriptor.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromFd, IntoFd, OwnedFd};
///
/// let f = File::open("foo.txt")?;
/// let owned_fd: OwnedFd = f.into_fd();
/// let f = File::from_fd(owned_fd);
/// # Ok::<(), io::Error>(())
/// ```
#[deprecated(
since = "1.0.0",
note = "`FromFd::from_fd` is replaced by `From<OwnedFd>::from`"
)]
fn from_fd(owned: OwnedFd) -> Self;
/// Constructs a new instance of `Self` from the given file descriptor
/// converted from `into_owned`.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromFd, IntoFd};
///
/// let f = File::open("foo.txt")?;
/// let f = File::from_into_fd(f);
/// # Ok::<(), io::Error>(())
/// ```
#[inline]
fn from_into_fd<Owned: Into<OwnedFd>>(into_owned: Owned) -> Self
where
Self: Sized + From<OwnedFd>,
{
Self::from(into_owned.into())
}
}
/// A trait to express the ability to construct an object from a handle.
#[cfg(windows)]
pub trait FromHandle {
/// Constructs a new instance of `Self` from the given handle.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromHandle, IntoHandle, OwnedHandle};
///
/// let f = File::open("foo.txt")?;
/// let owned_handle: OwnedHandle = f.into_handle();
/// let f = File::from_handle(owned_handle);
/// # Ok::<(), io::Error>(())
/// ```
#[deprecated(
since = "1.0.0",
note = "`FromHandle::from_handle` is replaced by `From<OwnedHandle>::from`"
)]
fn from_handle(owned: OwnedHandle) -> Self;
/// Constructs a new instance of `Self` from the given handle converted
/// from `into_owned`.
///
/// # Example
///
/// ```rust,no_run
/// use std::fs::File;
/// # use std::io;
/// use io_lifetimes::{FromHandle, IntoHandle};
///
/// let f = File::open("foo.txt")?;
/// let f = File::from_into_handle(f);
/// # Ok::<(), io::Error>(())
/// ```
#[inline]
fn from_into_handle<Owned: Into<OwnedHandle>>(into_owned: Owned) -> Self
where
Self: Sized + From<OwnedHandle>,
{
Self::from(into_owned.into())
}
}
/// A trait to express the ability to construct an object from a socket.
#[cfg(windows)]
pub trait FromSocket {
/// Constructs a new instance of `Self` from the given socket.
#[deprecated(
since = "1.0.0",
note = "`FromSocket::from_socket` is replaced by `From<OwnedSocket>::from`"
)]
fn from_socket(owned: OwnedSocket) -> Self;
/// Constructs a new instance of `Self` from the given socket converted
/// from `into_owned`.
#[inline]
fn from_into_socket<Owned: Into<OwnedSocket>>(into_owned: Owned) -> Self
where
Self: Sized + From<OwnedSocket>,
{
Self::from(into_owned.into())
}
}
#[cfg(not(io_safety_is_in_std))]
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsFd> AsFd for &T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}
#[cfg(not(io_safety_is_in_std))]
#[cfg(any(unix, target_os = "wasi"))]
impl<T: AsFd> AsFd for &mut T {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
T::as_fd(self)
}
}
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
impl<T: AsHandle> AsHandle for &T {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
T::as_handle(self)
}
}
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
impl<T: AsHandle> AsHandle for &mut T {
#[inline]
fn as_handle(&self) -> BorrowedHandle<'_> {
T::as_handle(self)
}
}
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
impl<T: AsSocket> AsSocket for &T {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
T::as_socket(self)
}
}
#[cfg(not(io_safety_is_in_std))]
#[cfg(windows)]
impl<T: AsSocket> AsSocket for &mut T {
#[inline]
fn as_socket(&self) -> BorrowedSocket<'_> {
T::as_socket(self)
}
}

860
vendor/io-lifetimes/src/types.rs vendored Normal file
View file

@ -0,0 +1,860 @@
use std::fmt;
use std::marker::PhantomData;
use std::mem::forget;
#[cfg(unix)]
use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(target_os = "wasi")]
use std::os::wasi::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
#[cfg(windows)]
use std::{
convert::TryFrom,
os::windows::io::{
AsRawHandle, AsRawSocket, FromRawHandle, FromRawSocket, IntoRawHandle, IntoRawSocket,
RawHandle, RawSocket,
},
};
#[cfg(all(windows, feature = "close"))]
use {
windows_sys::Win32::Foundation::{
CloseHandle, DuplicateHandle, SetHandleInformation, BOOL, DUPLICATE_HANDLE_OPTIONS,
DUPLICATE_SAME_ACCESS, HANDLE, HANDLE_FLAG_INHERIT, INVALID_HANDLE_VALUE,
},
windows_sys::Win32::Networking::WinSock::{
closesocket, WSADuplicateSocketW, WSAGetLastError, WSASocketW, INVALID_SOCKET, SOCKET,
SOCKET_ERROR, WSAEINVAL, WSAEPROTOTYPE, WSAPROTOCOL_INFOW, WSA_FLAG_NO_HANDLE_INHERIT,
WSA_FLAG_OVERLAPPED,
},
windows_sys::Win32::System::Threading::{GetCurrentProcess, GetCurrentProcessId},
};
#[cfg(all(windows, not(feature = "close")))]
type HANDLE = isize;
#[cfg(all(windows, not(feature = "close")))]
const INVALID_HANDLE_VALUE: HANDLE = !0 as _;
#[cfg(all(windows, not(feature = "close")))]
const INVALID_SOCKET: usize = !0 as _;
/// A borrowed file descriptor.
///
/// This has a lifetime parameter to tie it to the lifetime of something that
/// owns the file descriptor.
///
/// This uses `repr(transparent)` and has the representation of a host file
/// descriptor, so it can be used in FFI in places where a file descriptor is
/// passed as an argument, it is not captured or consumed, and it never has the
/// value `-1`.
///
/// This type's `.to_owned()` implementation returns another `BorrowedFd`
/// rather than an `OwnedFd`. It just makes a trivial copy of the raw file
/// descriptor, which is then borrowed under the same lifetime.
#[cfg(any(unix, target_os = "wasi"))]
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct BorrowedFd<'fd> {
fd: RawFd,
_phantom: PhantomData<&'fd OwnedFd>,
}
/// A borrowed handle.
///
/// This has a lifetime parameter to tie it to the lifetime of something that
/// owns the handle.
///
/// This uses `repr(transparent)` and has the representation of a host handle,
/// so it can be used in FFI in places where a handle is passed as an argument,
/// it is not captured or consumed, and it is never null.
///
/// Note that it *may* have the value `-1`, which in `BorrowedHandle` always
/// represents a valid handle value, such as [the current process handle], and
/// not `INVALID_HANDLE_VALUE`, despite the two having the same value. See
/// [here] for the full story.
///
/// This type's `.to_owned()` implementation returns another `BorrowedHandle`
/// rather than an `OwnedHandle`. It just makes a trivial copy of the raw
/// handle, which is then borrowed under the same lifetime.
///
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks
#[cfg(windows)]
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct BorrowedHandle<'handle> {
handle: RawHandle,
_phantom: PhantomData<&'handle OwnedHandle>,
}
/// A borrowed socket.
///
/// This has a lifetime parameter to tie it to the lifetime of something that
/// owns the socket.
///
/// This uses `repr(transparent)` and has the representation of a host socket,
/// so it can be used in FFI in places where a socket is passed as an argument,
/// it is not captured or consumed, and it never has the value
/// [`INVALID_SOCKET`].
///
/// This type's `.to_owned()` implementation returns another `BorrowedSocket`
/// rather than an `OwnedSocket`. It just makes a trivial copy of the raw
/// socket, which is then borrowed under the same lifetime.
#[cfg(windows)]
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct BorrowedSocket<'socket> {
socket: RawSocket,
_phantom: PhantomData<&'socket OwnedSocket>,
}
/// An owned file descriptor.
///
/// This closes the file descriptor on drop.
///
/// This uses `repr(transparent)` and has the representation of a host file
/// descriptor, so it can be used in FFI in places where a file descriptor is
/// passed as a consumed argument or returned as an owned value, and it never
/// has the value `-1`.
#[cfg(any(unix, target_os = "wasi"))]
#[repr(transparent)]
pub struct OwnedFd {
fd: RawFd,
}
#[cfg(any(unix, target_os = "wasi"))]
impl OwnedFd {
/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `OwnedFd` instance.
pub fn try_clone(&self) -> std::io::Result<Self> {
crate::AsFd::as_fd(self).try_clone_to_owned()
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl BorrowedFd<'_> {
/// Creates a new `OwnedFd` instance that shares the same underlying file
/// description as the existing `BorrowedFd` instance.
pub fn try_clone_to_owned(&self) -> std::io::Result<OwnedFd> {
#[cfg(feature = "close")]
{
#[cfg(unix)]
{
// We want to atomically duplicate this file descriptor and set the
// CLOEXEC flag, and currently that's done via F_DUPFD_CLOEXEC. This
// is a POSIX flag that was added to Linux in 2.6.24.
#[cfg(not(target_os = "espidf"))]
let cmd = libc::F_DUPFD_CLOEXEC;
// For ESP-IDF, F_DUPFD is used instead, because the CLOEXEC semantics
// will never be supported, as this is a bare metal framework with
// no capabilities for multi-process execution. While F_DUPFD is also
// not supported yet, it might be (currently it returns ENOSYS).
#[cfg(target_os = "espidf")]
let cmd = libc::F_DUPFD;
let fd = match unsafe { libc::fcntl(self.as_raw_fd(), cmd, 0) } {
-1 => return Err(std::io::Error::last_os_error()),
fd => fd,
};
Ok(unsafe { OwnedFd::from_raw_fd(fd) })
}
#[cfg(target_os = "wasi")]
{
unreachable!("try_clone is not yet suppported on wasi");
}
}
// If the `close` feature is disabled, we expect users to avoid cloning
// `OwnedFd` instances, so that we don't have to call `fcntl`.
#[cfg(not(feature = "close"))]
{
unreachable!("try_clone called without the \"close\" feature in io-lifetimes");
}
}
}
/// An owned handle.
///
/// This closes the handle on drop.
///
/// Note that it *may* have the value `-1`, which in `OwnedHandle` always
/// represents a valid handle value, such as [the current process handle], and
/// not `INVALID_HANDLE_VALUE`, despite the two having the same value. See
/// [here] for the full story.
///
/// And, it *may* have the value `NULL` (0), which can occur when consoles are
/// detached from processes, or when `windows_subsystem` is used.
///
/// `OwnedHandle` uses [`CloseHandle`] to close its handle on drop. As such,
/// it must not be used with handles to open registry keys which need to be
/// closed with [`RegCloseKey`] instead.
///
/// [`CloseHandle`]: https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
/// [`RegCloseKey`]: https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regclosekey
///
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks
#[cfg(windows)]
#[repr(transparent)]
pub struct OwnedHandle {
handle: RawHandle,
}
#[cfg(windows)]
impl OwnedHandle {
/// Creates a new `OwnedHandle` instance that shares the same underlying
/// object as the existing `OwnedHandle` instance.
pub fn try_clone(&self) -> std::io::Result<Self> {
crate::AsHandle::as_handle(self).try_clone_to_owned()
}
}
#[cfg(windows)]
impl BorrowedHandle<'_> {
/// Creates a new `OwnedHandle` instance that shares the same underlying
/// object as the existing `BorrowedHandle` instance.
pub fn try_clone_to_owned(&self) -> std::io::Result<OwnedHandle> {
#[cfg(feature = "close")]
{
self.duplicate(0, false, DUPLICATE_SAME_ACCESS)
}
// If the `close` feature is disabled, we expect users to avoid cloning
// `OwnedHandle` instances, so that we don't have to call `fcntl`.
#[cfg(not(feature = "close"))]
{
unreachable!("try_clone called without the \"close\" feature in io-lifetimes");
}
}
#[cfg(feature = "close")]
pub(crate) fn duplicate(
&self,
access: u32,
inherit: bool,
options: DUPLICATE_HANDLE_OPTIONS,
) -> std::io::Result<OwnedHandle> {
let mut ret = 0 as HANDLE;
match unsafe {
let cur_proc = GetCurrentProcess();
DuplicateHandle(
cur_proc,
self.as_raw_handle() as HANDLE,
cur_proc,
&mut ret,
access,
inherit as BOOL,
options,
)
} {
0 => return Err(std::io::Error::last_os_error()),
_ => (),
}
unsafe { Ok(OwnedHandle::from_raw_handle(ret as RawHandle)) }
}
}
/// An owned socket.
///
/// This closes the socket on drop.
///
/// This uses `repr(transparent)` and has the representation of a host socket,
/// so it can be used in FFI in places where a socket is passed as a consumed
/// argument or returned as an owned value, and it never has the value
/// [`INVALID_SOCKET`].
#[cfg(windows)]
#[repr(transparent)]
pub struct OwnedSocket {
socket: RawSocket,
}
#[cfg(windows)]
impl OwnedSocket {
/// Creates a new `OwnedSocket` instance that shares the same underlying
/// object as the existing `OwnedSocket` instance.
pub fn try_clone(&self) -> std::io::Result<Self> {
crate::AsSocket::as_socket(self).try_clone_to_owned()
}
#[cfg(feature = "close")]
#[cfg(not(target_vendor = "uwp"))]
fn set_no_inherit(&self) -> std::io::Result<()> {
match unsafe {
SetHandleInformation(self.as_raw_socket() as HANDLE, HANDLE_FLAG_INHERIT, 0)
} {
0 => return Err(std::io::Error::last_os_error()),
_ => Ok(()),
}
}
#[cfg(feature = "close")]
#[cfg(target_vendor = "uwp")]
fn set_no_inherit(&self) -> std::io::Result<()> {
Err(io::Error::new_const(
std::io::ErrorKind::Unsupported,
&"Unavailable on UWP",
))
}
}
#[cfg(windows)]
impl BorrowedSocket<'_> {
/// Creates a new `OwnedSocket` instance that shares the same underlying
/// object as the existing `BorrowedSocket` instance.
pub fn try_clone_to_owned(&self) -> std::io::Result<OwnedSocket> {
#[cfg(feature = "close")]
{
let mut info = unsafe { std::mem::zeroed::<WSAPROTOCOL_INFOW>() };
let result = unsafe {
WSADuplicateSocketW(self.as_raw_socket() as _, GetCurrentProcessId(), &mut info)
};
match result {
SOCKET_ERROR => return Err(std::io::Error::last_os_error()),
0 => (),
_ => panic!(),
}
let socket = unsafe {
WSASocketW(
info.iAddressFamily,
info.iSocketType,
info.iProtocol,
&mut info,
0,
WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT,
)
};
if socket != INVALID_SOCKET {
unsafe { Ok(OwnedSocket::from_raw_socket(socket as _)) }
} else {
let error = unsafe { WSAGetLastError() };
if error != WSAEPROTOTYPE && error != WSAEINVAL {
return Err(std::io::Error::from_raw_os_error(error));
}
let socket = unsafe {
WSASocketW(
info.iAddressFamily,
info.iSocketType,
info.iProtocol,
&mut info,
0,
WSA_FLAG_OVERLAPPED,
)
};
if socket == INVALID_SOCKET {
return Err(std::io::Error::last_os_error());
}
unsafe {
let socket = OwnedSocket::from_raw_socket(socket as _);
socket.set_no_inherit()?;
Ok(socket)
}
}
}
// If the `close` feature is disabled, we expect users to avoid cloning
// `OwnedSocket` instances, so that we don't have to call `fcntl`.
#[cfg(not(feature = "close"))]
{
unreachable!("try_clone called without the \"close\" feature in io-lifetimes");
}
}
}
/// FFI type for handles in return values or out parameters, where `INVALID_HANDLE_VALUE` is used
/// as a sentry value to indicate errors, such as in the return value of `CreateFileW`. This uses
/// `repr(transparent)` and has the representation of a host handle, so that it can be used in such
/// FFI declarations.
///
/// The only thing you can usefully do with a `HandleOrInvalid` is to convert it into an
/// `OwnedHandle` using its [`TryFrom`] implementation; this conversion takes care of the check for
/// `INVALID_HANDLE_VALUE`. This ensures that such FFI calls cannot start using the handle without
/// checking for `INVALID_HANDLE_VALUE` first.
///
/// This type may hold any handle value that [`OwnedHandle`] may hold, except that when it holds
/// `-1`, that value is interpreted to mean `INVALID_HANDLE_VALUE`.
///
/// If holds a handle other than `INVALID_HANDLE_VALUE`, it will close the handle on drop.
#[cfg(windows)]
#[repr(transparent)]
#[derive(Debug)]
pub struct HandleOrInvalid(RawHandle);
/// FFI type for handles in return values or out parameters, where `NULL` is used
/// as a sentry value to indicate errors, such as in the return value of `CreateThread`. This uses
/// `repr(transparent)` and has the representation of a host handle, so that it can be used in such
/// FFI declarations.
///
/// The only thing you can usefully do with a `HandleOrNull` is to convert it into an
/// `OwnedHandle` using its [`TryFrom`] implementation; this conversion takes care of the check for
/// `NULL`. This ensures that such FFI calls cannot start using the handle without
/// checking for `NULL` first.
///
/// This type may hold any handle value that [`OwnedHandle`] may hold. As with `OwnedHandle`, when
/// it holds `-1`, that value is interpreted as a valid handle value, such as
/// [the current process handle], and not `INVALID_HANDLE_VALUE`.
///
/// If this holds a non-null handle, it will close the handle on drop.
///
/// [the current process handle]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-getcurrentprocess#remarks
#[cfg(windows)]
#[repr(transparent)]
#[derive(Debug)]
pub struct HandleOrNull(RawHandle);
// The Windows [`HANDLE`] type may be transferred across and shared between
// thread boundaries (despite containing a `*mut void`, which in general isn't
// `Send` or `Sync`).
//
// [`HANDLE`]: std::os::windows::raw::HANDLE
#[cfg(windows)]
unsafe impl Send for OwnedHandle {}
#[cfg(windows)]
unsafe impl Send for HandleOrInvalid {}
#[cfg(windows)]
unsafe impl Send for HandleOrNull {}
#[cfg(windows)]
unsafe impl Send for BorrowedHandle<'_> {}
#[cfg(windows)]
unsafe impl Sync for OwnedHandle {}
#[cfg(windows)]
unsafe impl Sync for HandleOrInvalid {}
#[cfg(windows)]
unsafe impl Sync for HandleOrNull {}
#[cfg(windows)]
unsafe impl Sync for BorrowedHandle<'_> {}
#[cfg(any(unix, target_os = "wasi"))]
impl BorrowedFd<'_> {
/// Return a `BorrowedFd` holding the given raw file descriptor.
///
/// # Safety
///
/// The resource pointed to by `raw` must remain open for the duration of
/// the returned `BorrowedFd`, and it must not have the value `-1`.
#[inline]
pub const unsafe fn borrow_raw(fd: RawFd) -> Self {
#[cfg(panic_in_const_fn)]
debug_assert!(fd != -1_i32 as RawFd);
Self {
fd,
_phantom: PhantomData,
}
}
}
#[cfg(windows)]
impl BorrowedHandle<'_> {
/// Return a `BorrowedHandle` holding the given raw handle.
///
/// # Safety
///
/// The resource pointed to by `handle` must be a valid open handle, it
/// must remain open for the duration of the returned `BorrowedHandle`.
///
/// Note that it *may* have the value `INVALID_HANDLE_VALUE` (-1), which is
/// sometimes a valid handle value. See [here] for the full story.
///
/// And, it *may* have the value `NULL` (0), which can occur when consoles are
/// detached from processes, or when `windows_subsystem` is used.
///
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
#[inline]
pub const unsafe fn borrow_raw(handle: RawHandle) -> Self {
Self {
handle,
_phantom: PhantomData,
}
}
}
#[cfg(windows)]
impl BorrowedSocket<'_> {
/// Return a `BorrowedSocket` holding the given raw socket.
///
/// # Safety
///
/// The resource pointed to by `raw` must remain open for the duration of
/// the returned `BorrowedSocket`, and it must not have the value
/// [`INVALID_SOCKET`].
#[inline]
pub const unsafe fn borrow_raw(socket: RawSocket) -> Self {
#[cfg(panic_in_const_fn)]
debug_assert!(socket != INVALID_SOCKET as RawSocket);
Self {
socket,
_phantom: PhantomData,
}
}
}
#[cfg(windows)]
impl TryFrom<HandleOrInvalid> for OwnedHandle {
type Error = InvalidHandleError;
#[inline]
fn try_from(handle_or_invalid: HandleOrInvalid) -> Result<Self, InvalidHandleError> {
let raw = handle_or_invalid.0;
if raw as HANDLE == INVALID_HANDLE_VALUE {
// Don't call `CloseHandle`; it'd be harmless, except that it could
// overwrite the `GetLastError` error.
forget(handle_or_invalid);
Err(InvalidHandleError(()))
} else {
Ok(OwnedHandle { handle: raw })
}
}
}
#[cfg(windows)]
impl TryFrom<HandleOrNull> for OwnedHandle {
type Error = NullHandleError;
#[inline]
fn try_from(handle_or_null: HandleOrNull) -> Result<Self, NullHandleError> {
let raw = handle_or_null.0;
if raw.is_null() {
// Don't call `CloseHandle`; it'd be harmless, except that it could
// overwrite the `GetLastError` error.
forget(handle_or_null);
Err(NullHandleError(()))
} else {
Ok(OwnedHandle { handle: raw })
}
}
}
/// This is the error type used by [`HandleOrNull`] when attempting to convert
/// into a handle, to indicate that the value is null.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NullHandleError(());
impl fmt::Display for NullHandleError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"A HandleOrNull could not be converted to a handle because it was null".fmt(fmt)
}
}
impl std::error::Error for NullHandleError {}
/// This is the error type used by [`HandleOrInvalid`] when attempting to
/// convert into a handle, to indicate that the value is
/// `INVALID_HANDLE_VALUE`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidHandleError(());
impl fmt::Display for InvalidHandleError {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
"A HandleOrInvalid could not be converted to a handle because it was INVALID_HANDLE_VALUE"
.fmt(fmt)
}
}
impl std::error::Error for InvalidHandleError {}
#[cfg(any(unix, target_os = "wasi"))]
impl AsRawFd for BorrowedFd<'_> {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
#[cfg(windows)]
impl AsRawHandle for BorrowedHandle<'_> {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.handle
}
}
#[cfg(windows)]
impl AsRawSocket for BorrowedSocket<'_> {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
self.socket
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl AsRawFd for OwnedFd {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.fd
}
}
#[cfg(windows)]
impl AsRawHandle for OwnedHandle {
#[inline]
fn as_raw_handle(&self) -> RawHandle {
self.handle
}
}
#[cfg(windows)]
impl AsRawSocket for OwnedSocket {
#[inline]
fn as_raw_socket(&self) -> RawSocket {
self.socket
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl IntoRawFd for OwnedFd {
#[inline]
fn into_raw_fd(self) -> RawFd {
let fd = self.fd;
forget(self);
fd
}
}
#[cfg(windows)]
impl IntoRawHandle for OwnedHandle {
#[inline]
fn into_raw_handle(self) -> RawHandle {
let handle = self.handle;
forget(self);
handle
}
}
#[cfg(windows)]
impl IntoRawSocket for OwnedSocket {
#[inline]
fn into_raw_socket(self) -> RawSocket {
let socket = self.socket;
forget(self);
socket
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl FromRawFd for OwnedFd {
/// Constructs a new instance of `Self` from the given raw file descriptor.
///
/// # Safety
///
/// The resource pointed to by `raw` must be open and suitable for assuming
/// ownership.
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> Self {
debug_assert_ne!(fd, -1_i32 as RawFd);
Self { fd }
}
}
#[cfg(windows)]
impl FromRawHandle for OwnedHandle {
#[inline]
unsafe fn from_raw_handle(handle: RawHandle) -> Self {
Self { handle }
}
}
#[cfg(windows)]
impl FromRawSocket for OwnedSocket {
#[inline]
unsafe fn from_raw_socket(socket: RawSocket) -> Self {
debug_assert_ne!(socket, INVALID_SOCKET as RawSocket);
Self { socket }
}
}
#[cfg(windows)]
impl HandleOrInvalid {
/// Constructs a new instance of `Self` from the given `RawHandle` returned
/// from a Windows API that uses `INVALID_HANDLE_VALUE` to indicate
/// failure, such as `CreateFileW`.
///
/// Use `HandleOrNull` instead of `HandleOrInvalid` for APIs that
/// use null to indicate failure.
///
/// # Safety
///
/// The passed `handle` value must either satisfy the safety requirements
/// of [`FromRawHandle::from_raw_handle`], or be
/// `INVALID_HANDLE_VALUE` (-1). Note that not all Windows APIs use
/// `INVALID_HANDLE_VALUE` for errors; see [here] for the full story.
///
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
#[inline]
pub unsafe fn from_raw_handle(handle: RawHandle) -> Self {
Self(handle)
}
}
#[cfg(windows)]
impl HandleOrNull {
/// Constructs a new instance of `Self` from the given `RawHandle` returned
/// from a Windows API that uses null to indicate failure, such as
/// `CreateThread`.
///
/// Use `HandleOrInvalid` instead of `HandleOrNull` for APIs that
/// use `INVALID_HANDLE_VALUE` to indicate failure.
///
/// # Safety
///
/// The passed `handle` value must either satisfy the safety requirements
/// of [`FromRawHandle::from_raw_handle`], or be null. Note that not all
/// Windows APIs use null for errors; see [here] for the full story.
///
/// [here]: https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443
#[inline]
pub unsafe fn from_raw_handle(handle: RawHandle) -> Self {
Self(handle)
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl Drop for OwnedFd {
#[inline]
fn drop(&mut self) {
#[cfg(feature = "close")]
unsafe {
let _ = libc::close(self.fd as std::os::raw::c_int);
}
// If the `close` feature is disabled, we expect users to avoid letting
// `OwnedFd` instances drop, so that we don't have to call `close`.
#[cfg(not(feature = "close"))]
{
unreachable!("drop called without the \"close\" feature in io-lifetimes");
}
}
}
#[cfg(windows)]
impl Drop for OwnedHandle {
#[inline]
fn drop(&mut self) {
#[cfg(feature = "close")]
unsafe {
let _ = CloseHandle(self.handle as HANDLE);
}
// If the `close` feature is disabled, we expect users to avoid letting
// `OwnedHandle` instances drop, so that we don't have to call `close`.
#[cfg(not(feature = "close"))]
{
unreachable!("drop called without the \"close\" feature in io-lifetimes");
}
}
}
#[cfg(windows)]
impl Drop for HandleOrInvalid {
#[inline]
fn drop(&mut self) {
#[cfg(feature = "close")]
unsafe {
let _ = CloseHandle(self.0 as HANDLE);
}
// If the `close` feature is disabled, we expect users to avoid letting
// `HandleOrInvalid` instances drop, so that we don't have to call `close`.
#[cfg(not(feature = "close"))]
{
unreachable!("drop called without the \"close\" feature in io-lifetimes");
}
}
}
#[cfg(windows)]
impl Drop for HandleOrNull {
#[inline]
fn drop(&mut self) {
#[cfg(feature = "close")]
unsafe {
let _ = CloseHandle(self.0 as HANDLE);
}
// If the `close` feature is disabled, we expect users to avoid letting
// `HandleOrNull` instances drop, so that we don't have to call `close`.
#[cfg(not(feature = "close"))]
{
unreachable!("drop called without the \"close\" feature in io-lifetimes");
}
}
}
#[cfg(windows)]
impl Drop for OwnedSocket {
#[inline]
fn drop(&mut self) {
#[cfg(feature = "close")]
unsafe {
let _ = closesocket(self.socket as SOCKET);
}
// If the `close` feature is disabled, we expect users to avoid letting
// `OwnedSocket` instances drop, so that we don't have to call `close`.
#[cfg(not(feature = "close"))]
{
unreachable!("drop called without the \"close\" feature in io-lifetimes");
}
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl fmt::Debug for BorrowedFd<'_> {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedFd").field("fd", &self.fd).finish()
}
}
#[cfg(windows)]
impl fmt::Debug for BorrowedHandle<'_> {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedHandle")
.field("handle", &self.handle)
.finish()
}
}
#[cfg(windows)]
impl fmt::Debug for BorrowedSocket<'_> {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BorrowedSocket")
.field("socket", &self.socket)
.finish()
}
}
#[cfg(any(unix, target_os = "wasi"))]
impl fmt::Debug for OwnedFd {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedFd").field("fd", &self.fd).finish()
}
}
#[cfg(windows)]
impl fmt::Debug for OwnedHandle {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedHandle")
.field("handle", &self.handle)
.finish()
}
}
#[cfg(windows)]
impl fmt::Debug for OwnedSocket {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OwnedSocket")
.field("socket", &self.socket)
.finish()
}
}

277
vendor/io-lifetimes/src/views.rs vendored Normal file
View file

@ -0,0 +1,277 @@
//! Typed views using temporary objects.
//!
//! This module defines the return types for [`AsFilelike::as_filelike_view`]
//! and [`AsSocketlike::as_socketlike_view`].
//!
//! [`AsSocketlike::as_socketlike_view`]: crate::AsSocketlike::as_socketlike_view
use crate::raw::{
AsRawFilelike, AsRawSocketlike, FromRawFilelike, FromRawSocketlike, IntoRawFilelike,
IntoRawSocketlike, RawFilelike, RawSocketlike,
};
#[cfg(any(unix, target_os = "wasi"))]
use crate::OwnedFd;
use crate::{
AsFilelike, AsSocketlike, FromFilelike, FromSocketlike, IntoFilelike, IntoSocketlike,
OwnedFilelike, OwnedSocketlike,
};
#[cfg(windows)]
use crate::{OwnedHandle, OwnedSocket};
use std::fmt;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::Deref;
/// Declare that a type is safe to use in a [`FilelikeView`].
///
/// # Safety
///
/// Types implementing this trait declare that if they are constructed with
/// [`FromFilelike`] and consumed with [`IntoFilelike`], their `IntoFilelike`
/// will return the same `OwnedFd` value that was passed to their
/// `FromFilelike`.
pub unsafe trait FilelikeViewType: FromFilelike + IntoFilelike {}
/// Declare that a type is safe to use in a [`SocketlikeView`].
///
/// # Safety
///
/// Types implementing this trait declare that if they are constructed with
/// [`FromSocketlike`] and consumed with [`IntoSocketlike`], their
/// `IntoSocketlike` will return the same `OwnedFd` value that was passed to
/// their `FromSocketlike`.
pub unsafe trait SocketlikeViewType: FromSocketlike + IntoSocketlike {}
/// A non-owning view of a resource which dereferences to a `&Target` or
/// `&mut Target`. These are returned by [`AsFilelike::as_filelike_view`].
pub struct FilelikeView<'filelike, Target: FilelikeViewType> {
/// The value to dereference to. This is a `ManuallyDrop` so that we can
/// consume it in our `Drop` impl.
target: ManuallyDrop<Target>,
/// `FilelikeViewType` implementors guarantee that their `Into<OwnedFd>`
/// returns the same fd as their `From<OwnedFd>` gave them. This field
/// allows us to verify this.
#[cfg(debug_assertions)]
orig: RawFilelike,
/// This field exists because we don't otherwise explicitly use
/// `'filelike`.
_phantom: PhantomData<&'filelike OwnedFilelike>,
}
/// A non-owning view of a resource which dereferences to a `&Target` or
/// `&mut Target`. These are returned by [`AsSocketlike::as_socketlike_view`].
pub struct SocketlikeView<'socketlike, Target: SocketlikeViewType> {
/// The value to dereference to. This is a `ManuallyDrop` so that we can
/// consume it in our `Drop` impl.
target: ManuallyDrop<Target>,
/// `SocketlikeViewType` implementors guarantee that their `Into<OwnedFd>`
/// returns the same fd as their `From<OwnedFd>` gave them. This field
/// allows us to verify this.
#[cfg(debug_assertions)]
orig: RawSocketlike,
/// This field exists because we don't otherwise explicitly use
/// `'socketlike`.
_phantom: PhantomData<&'socketlike OwnedSocketlike>,
}
impl<Target: FilelikeViewType> FilelikeView<'_, Target> {
/// Construct a temporary `Target` and wrap it in a `FilelikeView` object.
#[inline]
pub(crate) fn new<T: AsFilelike>(filelike: &T) -> Self {
// Safety: The returned `FilelikeView` is scoped to the lifetime of
// `filelike`, which we've borrowed here, so the view won't outlive
// the object it's borrowed from.
unsafe { Self::view_raw(filelike.as_filelike().as_raw_filelike()) }
}
/// Construct a temporary `Target` from raw and wrap it in a `FilelikeView`
/// object.
///
/// # Safety
///
/// `raw` must be a valid raw filelike referencing a resource that outlives
/// the resulting view.
#[inline]
pub unsafe fn view_raw(raw: RawFilelike) -> Self {
let owned = OwnedFilelike::from_raw_filelike(raw);
Self {
target: ManuallyDrop::new(Target::from_filelike(owned)),
#[cfg(debug_assertions)]
orig: raw,
_phantom: PhantomData,
}
}
}
impl<Target: SocketlikeViewType> SocketlikeView<'_, Target> {
/// Construct a temporary `Target` and wrap it in a `SocketlikeView`
/// object.
#[inline]
pub(crate) fn new<T: AsSocketlike>(socketlike: &T) -> Self {
// Safety: The returned `SocketlikeView` is scoped to the lifetime of
// `socketlike`, which we've borrowed here, so the view won't outlive
// the object it's borrowed from.
unsafe { Self::view_raw(socketlike.as_socketlike().as_raw_socketlike()) }
}
/// Construct a temporary `Target` from raw and wrap it in a
/// `SocketlikeView` object.
///
/// # Safety
///
/// `raw` must be a valid raw socketlike referencing a resource that
/// outlives the resulting view.
#[inline]
pub unsafe fn view_raw(raw: RawSocketlike) -> Self {
let owned = OwnedSocketlike::from_raw_socketlike(raw);
Self {
target: ManuallyDrop::new(Target::from_socketlike(owned)),
#[cfg(debug_assertions)]
orig: raw,
_phantom: PhantomData,
}
}
}
impl<Target: FilelikeViewType> Deref for FilelikeView<'_, Target> {
type Target = Target;
#[inline]
fn deref(&self) -> &Self::Target {
&self.target
}
}
impl<Target: SocketlikeViewType> Deref for SocketlikeView<'_, Target> {
type Target = Target;
#[inline]
fn deref(&self) -> &Self::Target {
&self.target
}
}
impl<Target: FilelikeViewType> Drop for FilelikeView<'_, Target> {
#[inline]
fn drop(&mut self) {
// Use `Into*` to consume `self.target` without freeing its resource.
//
// Safety: Using `ManuallyDrop::take` requires us to ensure that
// `self.target` is not used again. We don't use it again here, and
// this is the `drop` function, so we know it's not used afterward.
let _raw = unsafe { ManuallyDrop::take(&mut self.target) }
.into_filelike()
.into_raw_filelike();
#[cfg(debug_assertions)]
debug_assert_eq!(self.orig, _raw);
}
}
impl<Target: SocketlikeViewType> Drop for SocketlikeView<'_, Target> {
#[inline]
fn drop(&mut self) {
// Use `Into*` to consume `self.target` without freeing its resource.
//
// Safety: Using `ManuallyDrop::take` requires us to ensure that
// `self.target` is not used again. We don't use it again here, and
// this is the `drop` function, so we know it's not used afterward.
let _raw = unsafe { ManuallyDrop::take(&mut self.target) }
.into_socketlike()
.into_raw_socketlike();
#[cfg(debug_assertions)]
debug_assert_eq!(self.orig, _raw);
}
}
impl<Target: FilelikeViewType> fmt::Debug for FilelikeView<'_, Target> {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FilelikeView")
.field("target", &*self)
.finish()
}
}
impl<Target: SocketlikeViewType> fmt::Debug for SocketlikeView<'_, Target> {
#[allow(clippy::missing_inline_in_public_items)]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("SocketlikeView")
.field("target", &*self)
.finish()
}
}
#[cfg(any(unix, target_os = "wasi"))]
unsafe impl FilelikeViewType for OwnedFd {}
#[cfg(windows)]
unsafe impl FilelikeViewType for OwnedHandle {}
#[cfg(windows)]
unsafe impl SocketlikeViewType for OwnedSocket {}
unsafe impl FilelikeViewType for std::fs::File {}
unsafe impl SocketlikeViewType for std::net::TcpStream {}
unsafe impl SocketlikeViewType for std::net::TcpListener {}
unsafe impl SocketlikeViewType for std::net::UdpSocket {}
#[cfg(unix)]
unsafe impl SocketlikeViewType for std::os::unix::net::UnixStream {}
#[cfg(unix)]
unsafe impl SocketlikeViewType for std::os::unix::net::UnixListener {}
#[cfg(unix)]
unsafe impl SocketlikeViewType for std::os::unix::net::UnixDatagram {}
#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "os_pipe")]
unsafe impl FilelikeViewType for os_pipe::PipeWriter {}
#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "os_pipe")]
unsafe impl FilelikeViewType for os_pipe::PipeReader {}
#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "socket2")]
unsafe impl SocketlikeViewType for socket2::Socket {}
#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "async_std")]
unsafe impl SocketlikeViewType for async_std::net::TcpStream {}
#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "async_std")]
unsafe impl SocketlikeViewType for async_std::net::TcpListener {}
#[cfg(not(target_os = "wasi"))]
#[cfg(feature = "async_std")]
unsafe impl SocketlikeViewType for async_std::net::UdpSocket {}
#[cfg(unix)]
#[cfg(feature = "async_std")]
unsafe impl SocketlikeViewType for async_std::os::unix::net::UnixStream {}
#[cfg(unix)]
#[cfg(feature = "async_std")]
unsafe impl SocketlikeViewType for async_std::os::unix::net::UnixListener {}
#[cfg(unix)]
#[cfg(feature = "async_std")]
unsafe impl SocketlikeViewType for async_std::os::unix::net::UnixDatagram {}
#[cfg(feature = "mio")]
unsafe impl SocketlikeViewType for mio::net::TcpStream {}
#[cfg(feature = "mio")]
unsafe impl SocketlikeViewType for mio::net::TcpListener {}
#[cfg(feature = "mio")]
unsafe impl SocketlikeViewType for mio::net::UdpSocket {}
#[cfg(unix)]
#[cfg(feature = "mio")]
unsafe impl SocketlikeViewType for mio::net::UnixDatagram {}
#[cfg(unix)]
#[cfg(feature = "mio")]
unsafe impl SocketlikeViewType for mio::net::UnixListener {}
#[cfg(unix)]
#[cfg(feature = "mio")]
unsafe impl SocketlikeViewType for mio::net::UnixStream {}
#[cfg(unix)]
#[cfg(feature = "mio")]
unsafe impl FilelikeViewType for mio::unix::pipe::Receiver {}
#[cfg(unix)]
#[cfg(feature = "mio")]
unsafe impl FilelikeViewType for mio::unix::pipe::Sender {}