Bug 1927657 - Add Mac IO Surface support for Rust r=webgpu-reviewers,supply-chain-reviewers,nical

The change is preparation for Bug 1927658.

Add io-surface and dependent crates. io-surface is already audited at supply-chain/audits.toml.

Differential Revision: https://phabricator.services.mozilla.com/D227235
This commit is contained in:
sotaro 2024-11-05 00:43:55 +00:00
parent e738c3d2e1
commit c02d07f51e
25 changed files with 1221 additions and 0 deletions

39
Cargo.lock generated
View File

@ -780,6 +780,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "cgl"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ced0551234e87afee12411d535648dd89d2e7f34c78b753395567aff3d447ff"
dependencies = [
"libc",
]
[[package]]
name = "chardetng"
version = "0.1.9"
@ -3170,6 +3179,18 @@ dependencies = [
"unic-langid",
]
[[package]]
name = "io-surface"
version = "0.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "861c6093cbc05599e66436aedf380bb0a23cec2180738393d3a340b80dd135ef"
dependencies = [
"cgl",
"core-foundation",
"leaky-cow",
"libc",
]
[[package]]
name = "ioctl-sys"
version = "0.7.1"
@ -3467,6 +3488,21 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "leak"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd100e01f1154f2908dfa7d02219aeab25d0b9c7fa955164192e3245255a0c73"
[[package]]
name = "leaky-cow"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40a8225d44241fd324a8af2806ba635fc7c8a7e9a7de4d5cf3ef54e71f5926fc"
dependencies = [
"leak",
]
[[package]]
name = "leb128"
version = "0.2.5"
@ -7110,8 +7146,11 @@ dependencies = [
"arrayvec",
"ash",
"bincode",
"io-surface",
"log",
"metal",
"nsstring",
"objc",
"parking_lot",
"serde",
"static_prefs",

View File

@ -57,6 +57,11 @@ features = ["oom_panic", "device_lost_panic", "internal_error_panic"]
[target.'cfg(windows)'.dependencies]
windows = { version = "0.58", default-features = false, features = ["Win32_Graphics_Direct3D12"]}
[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
metal = "0.29"
io-surface = "0.15"
[dependencies]
bincode = "1"
log = "0.4"

View File

@ -1078,6 +1078,11 @@ criteria = "safe-to-deploy"
delta = "0.1.1 -> 0.2.1"
notes = "Very minor changes."
[[audits.cgl]]
who = "Sotaro Ikeda <sotaro.ikeda.g@gmail.com>"
criteria = "safe-to-deploy"
version = "0.3.2"
[[audits.chardetng]]
who = "Henri Sivonen <hsivonen@hsivonen.fi>"
criteria = "safe-to-deploy"
@ -2848,6 +2853,16 @@ who = "Nicolas Silva <nical@fastmail.com>"
criteria = "safe-to-deploy"
delta = "4.1.0 -> 6.0.0"
[[audits.leak]]
who = "Sotaro Ikeda <sotaro.ikeda.g@gmail.com>"
criteria = "safe-to-deploy"
version = "0.1.2"
[[audits.leaky-cow]]
who = "Sotaro Ikeda <sotaro.ikeda.g@gmail.com>"
criteria = "safe-to-deploy"
version = "0.1.1"
[[audits.libc]]
who = "Mike Hommey <mh+mozilla@glandium.org>"
criteria = "safe-to-deploy"

View File

@ -320,6 +320,13 @@ user-id = 3618
user-login = "dtolnay"
user-name = "David Tolnay"
[[publisher.io-surface]]
version = "0.15.1"
when = "2020-06-30"
user-id = 2396
user-login = "jdm"
user-name = "Josh Matthews"
[[publisher.iovec]]
version = "0.1.4"
when = "2019-10-09"

View File

@ -0,0 +1 @@
{"files":{"COPYING":"ec82b96487e9e778ee610c7ab245162464782cfa1f555c2299333f8dbe5c036a","Cargo.toml":"0c60a07e2ed0d8603f866a632b2ed80aaf16b7e9ed97381ea91eefb00eba8b1f","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"62065228e42caebca7e7d7db1204cbb867033de5982ca4009928915e4095f3a3","README.md":"5b2cad1e1bd53b9f986974a23dbcbd951270a567d6c661f3584707d7ad198e82","src/cgl.rs":"39b6f0c5c0d60ad579dabae79ac2ef23e65086948ebaadd196a76b33c40d8216","src/lib.rs":"9af9c2b9cefe11f8527d3a4f9c42fb7285df1f6f2ee098ef45088d4cd6e5ed83"},"package":"0ced0551234e87afee12411d535648dd89d2e7f34c78b753395567aff3d447ff"}

5
third_party/rust/cgl/COPYING vendored Normal file
View File

@ -0,0 +1,5 @@
Licensed under 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. All files in the project carrying such notice may not be
copied, modified, or distributed except according to those terms.

21
third_party/rust/cgl/Cargo.toml vendored Normal file
View File

@ -0,0 +1,21 @@
# 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 believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
name = "cgl"
version = "0.3.2"
authors = ["The Servo Project Developers"]
description = "Rust bindings for CGL on Mac"
license = "MIT / Apache-2.0"
repository = "https://github.com/servo/cgl-rs"
[dependencies.libc]
version = "0.2"

201
third_party/rust/cgl/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.

25
third_party/rust/cgl/LICENSE-MIT vendored Normal file
View File

@ -0,0 +1,25 @@
Copyright (c) 2012-2013 Mozilla Foundation
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.

4
third_party/rust/cgl/README.md vendored Normal file
View File

@ -0,0 +1,4 @@
rust-cgl
========
Rust bindings for CGL on Mac

204
third_party/rust/cgl/src/cgl.rs vendored Normal file
View File

@ -0,0 +1,204 @@
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under 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. This file may not be copied, modified, or distributed
// except according to those terms.
// Mac-specific OpenGL bindings.
#![allow(non_upper_case_globals)]
use libc::{c_void, c_int, c_char};
use std::os::raw;
pub type GLenum = raw::c_uint;
pub type GLint = raw::c_int;
pub type GLsizei = raw::c_int;
pub type GLuint = raw::c_uint;
pub type CGLPixelFormatAttribute = c_int;
pub type CGLContextParameter = c_int;
pub type CGLContextEnable = c_int;
pub type CGLGlobalOption = c_int;
pub type CGLError = c_int;
pub type CGLPixelFormatObj = *mut c_void;
pub type CGLContextObj = *mut c_void;
pub type CGLShareGroupObj = *mut c_void;
pub type IOSurfaceRef = *mut c_void;
pub const kCGLNoError: CGLError = 0;
pub const kCGLPFAAllRenderers: CGLPixelFormatAttribute = 1;
pub const kCGLPFADoubleBuffer: CGLPixelFormatAttribute = 5;
pub const kCGLPFAStereo: CGLPixelFormatAttribute = 6;
pub const kCGLPFAAuxBuffers: CGLPixelFormatAttribute = 7;
pub const kCGLPFAColorSize: CGLPixelFormatAttribute = 8;
pub const kCGLPFAAlphaSize: CGLPixelFormatAttribute = 11;
pub const kCGLPFADepthSize: CGLPixelFormatAttribute = 12;
pub const kCGLPFAStencilSize: CGLPixelFormatAttribute = 13;
pub const kCGLPFAAccumSize: CGLPixelFormatAttribute = 14;
pub const kCGLPFAMinimumPolicy: CGLPixelFormatAttribute = 51;
pub const kCGLPFAMaximumPolicy: CGLPixelFormatAttribute = 52;
pub const kCGLPFAOffScreen: CGLPixelFormatAttribute = 53;
pub const kCGLPFAFullScreen: CGLPixelFormatAttribute = 54;
pub const kCGLPFASampleBuffers: CGLPixelFormatAttribute = 55;
pub const kCGLPFASamples: CGLPixelFormatAttribute = 56;
pub const kCGLPFAAuxDepthStencil: CGLPixelFormatAttribute = 57;
pub const kCGLPFAColorFloat: CGLPixelFormatAttribute = 58;
pub const kCGLPFAMultisample: CGLPixelFormatAttribute = 59;
pub const kCGLPFASupersample: CGLPixelFormatAttribute = 60;
pub const kCGLPFASampleAlpha: CGLPixelFormatAttribute = 61;
pub const kCGLPFARendererID: CGLPixelFormatAttribute = 70;
pub const kCGLPFASingleRenderer: CGLPixelFormatAttribute = 71;
pub const kCGLPFANoRecovery: CGLPixelFormatAttribute = 72;
pub const kCGLPFAAccelerated: CGLPixelFormatAttribute = 73;
pub const kCGLPFAClosestPolicy: CGLPixelFormatAttribute = 74;
pub const kCGLPFARobust: CGLPixelFormatAttribute = 75;
pub const kCGLPFABackingStore: CGLPixelFormatAttribute = 76;
pub const kCGLPFAMPSafe: CGLPixelFormatAttribute = 78;
pub const kCGLPFAWindow: CGLPixelFormatAttribute = 80;
pub const kCGLPFAMultiScreen: CGLPixelFormatAttribute = 81;
pub const kCGLPFACompliant: CGLPixelFormatAttribute = 83;
pub const kCGLPFADisplayMask: CGLPixelFormatAttribute = 84;
pub const kCGLPFAPBuffer: CGLPixelFormatAttribute = 90;
pub const kCGLPFARemotePBuffer: CGLPixelFormatAttribute = 91;
pub const kCGLPFAAllowOfflineRenderers: CGLPixelFormatAttribute = 96;
pub const kCGLPFAAcceleratedCompute: CGLPixelFormatAttribute = 97;
pub const kCGLPFAOpenGLProfile: CGLPixelFormatAttribute = 99;
pub const kCGLPFAVirtualScreenCount: CGLPixelFormatAttribute = 128;
pub const kCGLCESwapRectangle: CGLContextEnable = 201;
pub const kCGLCESwapLimit: CGLContextEnable = 203;
pub const kCGLCERasterization: CGLContextEnable = 221;
pub const kCGLCEStateValidation: CGLContextEnable = 301;
pub const kCGLCESurfaceBackingSize: CGLContextEnable = 305;
pub const kCGLCEDisplayListOptimization: CGLContextEnable = 307;
pub const kCGLCEMPEngine: CGLContextEnable = 313;
pub const kCGLCECrashOnRemovedFunctions: CGLContextEnable = 316;
pub const kCGLCPSwapRectangle: CGLContextParameter = 200;
pub const kCGLCPSwapInterval: CGLContextParameter = 222;
pub const kCGLCPDispatchTableSize: CGLContextParameter = 224;
pub const kCGLCPClientStorage: CGLContextParameter = 226;
pub const kCGLCPSurfaceTexture: CGLContextParameter = 228;
pub const kCGLCPSurfaceOrder: CGLContextParameter = 235;
pub const kCGLCPSurfaceOpacity: CGLContextParameter = 236;
pub const kCGLCPSurfaceBackingSize: CGLContextParameter = 304;
pub const kCGLCPSurfaceSurfaceVolatile: CGLContextParameter = 306;
pub const kCGLCPReclaimResources: CGLContextParameter = 308;
pub const kCGLCPCurrentRendererID: CGLContextParameter = 309;
pub const kCGLCPGPUVertexProcessing: CGLContextParameter = 310;
pub const kCGLCPGPUFragmentProcessing: CGLContextParameter = 311;
pub const kCGLCPHasDrawable: CGLContextParameter = 314;
pub const kCGLCPMPSwapsInFlight: CGLContextParameter = 315;
pub const kCGLGOFormatCacheSize: CGLGlobalOption = 501;
pub const kCGLGOClearFormatCache: CGLGlobalOption = 502;
pub const kCGLGORetainRenderers: CGLGlobalOption = 503;
pub const kCGLGOResetLibrary: CGLGlobalOption = 504;
pub const kCGLGOUseErrorHandler: CGLGlobalOption = 505;
pub const kCGLGOUseBuildCache: CGLGlobalOption = 506;
pub const CORE_BOOLEAN_ATTRIBUTES: &'static [CGLPixelFormatAttribute] =
&[kCGLPFAAllRenderers,
kCGLPFADoubleBuffer,
kCGLPFAStereo,
kCGLPFAAuxBuffers,
kCGLPFAMinimumPolicy,
kCGLPFAMaximumPolicy,
kCGLPFAOffScreen,
kCGLPFAFullScreen,
kCGLPFAAuxDepthStencil,
kCGLPFAColorFloat,
kCGLPFAMultisample,
kCGLPFASupersample,
kCGLPFASampleAlpha,
kCGLPFASingleRenderer,
kCGLPFANoRecovery,
kCGLPFAAccelerated,
kCGLPFAClosestPolicy,
kCGLPFARobust,
kCGLPFABackingStore,
kCGLPFAMPSafe,
kCGLPFAWindow,
kCGLPFAMultiScreen,
kCGLPFACompliant,
kCGLPFAPBuffer,
kCGLPFARemotePBuffer,
kCGLPFAAllowOfflineRenderers,
kCGLPFAAcceleratedCompute];
pub const CORE_INTEGER_ATTRIBUTES: &'static [CGLPixelFormatAttribute] =
&[kCGLPFAColorSize,
kCGLPFAAlphaSize,
kCGLPFADepthSize,
kCGLPFAStencilSize,
kCGLPFAAccumSize,
kCGLPFASampleBuffers,
kCGLPFASamples,
kCGLPFARendererID,
kCGLPFADisplayMask,
kCGLPFAOpenGLProfile,
kCGLPFAVirtualScreenCount];
#[link(name = "OpenGL", kind = "framework")]
extern {
// CGLCurrent.h
pub fn CGLSetCurrentContext(ctx: CGLContextObj) -> CGLError;
pub fn CGLGetCurrentContext() -> CGLContextObj;
pub fn CGLGetShareGroup(context: CGLContextObj) -> CGLShareGroupObj;
// OpenGL.h
// Pixel format functions
pub fn CGLChoosePixelFormat(attribs: *const CGLPixelFormatAttribute,
pix: *mut CGLPixelFormatObj,
npix: *mut GLint) -> CGLError;
pub fn CGLDescribePixelFormat(pix: CGLPixelFormatObj,
pix_num: GLint,
attrib: CGLPixelFormatAttribute,
value: *mut GLint) -> CGLError;
pub fn CGLDestroyPixelFormat(pix: CGLPixelFormatObj) -> CGLError;
pub fn CGLReleasePixelFormat(pix: CGLPixelFormatObj);
pub fn CGLRetainPixelFormat(pix: CGLPixelFormatObj) -> CGLPixelFormatObj;
pub fn CGLGetPixelFormatRetainCount(pix: CGLPixelFormatObj) -> GLuint;
// Context functions
pub fn CGLCreateContext(pix: CGLPixelFormatObj, share: CGLContextObj, ctx: *mut CGLContextObj) ->
CGLError;
pub fn CGLDestroyContext(ctx: CGLContextObj) -> CGLError;
pub fn CGLGetPixelFormat(ctx: CGLContextObj) -> CGLPixelFormatObj;
// Getting and Setting Context Options
pub fn CGLEnable(ctx: CGLContextObj, pname: CGLContextEnable) -> CGLError;
pub fn CGLDisable(ctx: CGLContextObj, pname: CGLContextEnable) -> CGLError;
pub fn CGLIsEnabled(ctx: CGLContextObj, pname: CGLContextEnable, enable: &mut GLint) -> CGLError;
pub fn CGLSetParameter(ctx: CGLContextObj, pname: CGLContextParameter, params: &GLint) -> CGLError;
pub fn CGLGetParameter(ctx: CGLContextObj, pname: CGLContextParameter, params: &mut GLint) -> CGLError;
// Locking functions
pub fn CGLLockContext(ctx: CGLContextObj) -> CGLError;
pub fn CGLUnlockContext(ctx: CGLContextObj) -> CGLError;
// Getting and Setting Global Information
pub fn CGLSetOption(pname: CGLGlobalOption, param: &GLint) -> CGLError;
pub fn CGLGetOption(pname: CGLGlobalOption, param: &mut GLint) -> CGLError;
pub fn CGLSetGlobalOption(pname: CGLGlobalOption, param: &GLint) -> CGLError;
pub fn CGLGetGlobalOption(pname: CGLGlobalOption, param: &mut GLint) -> CGLError;
pub fn CGLGetVersion (major: &mut GLint, minor: &mut GLint) -> CGLError;
// CGLIOSurface.h
pub fn CGLTexImageIOSurface2D(ctx: CGLContextObj, target: GLenum, internal_format: GLenum,
width: GLsizei, height: GLsizei, format: GLenum, ty: GLenum,
ioSurface: IOSurfaceRef, plane: GLuint) -> CGLError;
// https://developer.apple.com/library/mac/documentation/GraphicsImaging/Reference/CGL_OpenGL/#//apple_ref/c/func/CGLErrorString
pub fn CGLErrorString(error: CGLError) -> *const c_char;
}

17
third_party/rust/cgl/src/lib.rs vendored Normal file
View File

@ -0,0 +1,17 @@
// Copyright 2014 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under 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. This file may not be copied, modified, or distributed
// except according to those terms.
#![crate_name = "cgl"]
#![crate_type = "lib"]
extern crate libc;
pub use cgl::*;
mod cgl;

View File

@ -0,0 +1 @@
{"files":{"COPYRIGHT":"ec82b96487e9e778ee610c7ab245162464782cfa1f555c2299333f8dbe5c036a","Cargo.toml":"001c07d668f356949c26b98e0ef0b1e6c53e38eab13515a41208ea8e3268e7d1","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"62065228e42caebca7e7d7db1204cbb867033de5982ca4009928915e4095f3a3","src/lib.rs":"8c0ab701a403823acaf0b636b67119b9022db2dd4d6d48c9e08f2ce1b2658a32"},"package":"861c6093cbc05599e66436aedf380bb0a23cec2180738393d3a340b80dd135ef"}

5
third_party/rust/io-surface/COPYRIGHT vendored Normal file
View File

@ -0,0 +1,5 @@
Licensed under 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. All files in the project carrying such notice may not be
copied, modified, or distributed except according to those terms.

33
third_party/rust/io-surface/Cargo.toml vendored Normal file
View File

@ -0,0 +1,33 @@
# 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 believe there's an error in this file please file an
# issue against the rust-lang/cargo repository. If you're
# editing this file be aware that the upstream Cargo.toml
# will likely look very different (and much more reasonable)
[package]
name = "io-surface"
version = "0.15.1"
authors = ["The Servo Project Developers"]
description = "Bindings to IO Surface for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
license = "MIT / Apache-2.0"
repository = "https://github.com/servo/core-foundation-rs"
[package.metadata.docs.rs]
default-target = "x86_64-apple-darwin"
[dependencies.cgl]
version = "0.3"
[dependencies.core-foundation]
version = "0.9"
[dependencies.leaky-cow]
version = "0.1.1"
[dependencies.libc]
version = "0.2"

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.

25
third_party/rust/io-surface/LICENSE-MIT vendored Normal file
View File

@ -0,0 +1,25 @@
Copyright (c) 2012-2013 Mozilla Foundation
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.

210
third_party/rust/io-surface/src/lib.rs vendored Normal file
View File

@ -0,0 +1,210 @@
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under 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. This file may not be copied, modified, or distributed
// except according to those terms.
#![crate_name = "io_surface"]
#![crate_type = "rlib"]
extern crate libc;
extern crate core_foundation;
extern crate cgl;
extern crate leaky_cow;
// Rust bindings to the IOSurface framework on macOS.
use core_foundation::base::{CFRelease, CFRetain, CFTypeID, CFTypeRef, CFType, TCFType};
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef};
use core_foundation::string::{CFString, CFStringRef};
use cgl::{kCGLNoError, CGLGetCurrentContext, CGLTexImageIOSurface2D, CGLErrorString, GLenum};
use libc::{c_int, size_t};
use std::os::raw::c_void;
use leaky_cow::LeakyCow;
use std::slice;
use std::ffi::CStr;
const BGRA: GLenum = 0x80E1;
const RGBA: GLenum = 0x1908;
const RGB: GLenum = 0x1907;
const TEXTURE_RECTANGLE_ARB: GLenum = 0x84F5;
const UNSIGNED_INT_8_8_8_8_REV: GLenum = 0x8367;
//static kIOSurfaceLockReadOnly: u32 = 0x1;
//static kIOSurfaceLockAvoidSync: u32 = 0x2;
type IOReturn = c_int;
#[repr(C)]
pub struct __IOSurface(c_void);
pub type IOSurfaceRef = *const __IOSurface;
pub struct IOSurface {
pub obj: IOSurfaceRef,
}
impl Drop for IOSurface {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
pub type IOSurfaceID = u32;
impl Clone for IOSurface {
#[inline]
fn clone(&self) -> IOSurface {
unsafe {
TCFType::wrap_under_get_rule(self.obj)
}
}
}
impl TCFType for IOSurface {
type Ref = IOSurfaceRef;
#[inline]
fn as_concrete_TypeRef(&self) -> IOSurfaceRef {
self.obj
}
#[inline]
unsafe fn wrap_under_create_rule(obj: IOSurfaceRef) -> IOSurface {
assert!(!obj.is_null(), "Attempted to create a NULL object.");
IOSurface {
obj: obj,
}
}
#[inline]
fn type_id() -> CFTypeID {
unsafe {
IOSurfaceGetTypeID()
}
}
#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
self.as_concrete_TypeRef() as CFTypeRef
}
#[inline]
unsafe fn wrap_under_get_rule(reference: IOSurfaceRef) -> IOSurface {
assert!(!reference.is_null(), "Attempted to create a NULL object.");
let reference = CFRetain(reference as *const c_void) as IOSurfaceRef;
TCFType::wrap_under_create_rule(reference)
}
}
pub fn new(properties: &CFDictionary<CFString, CFType>) -> IOSurface {
unsafe {
TCFType::wrap_under_create_rule(IOSurfaceCreate(properties.as_concrete_TypeRef()))
}
}
/// Looks up an `IOSurface` by its global ID.
///
/// FIXME(pcwalton): This should return an `Option`.
pub fn lookup(csid: IOSurfaceID) -> IOSurface {
unsafe {
TCFType::wrap_under_create_rule(IOSurfaceLookup(csid))
}
}
impl IOSurface {
pub fn get_id(&self) -> IOSurfaceID {
unsafe {
IOSurfaceGetID(self.as_concrete_TypeRef())
}
}
/// Binds to the current GL texture.
pub fn bind_to_gl_texture(&self, width: i32, height: i32, has_alpha: bool) {
unsafe {
let context = CGLGetCurrentContext();
let gl_error = CGLTexImageIOSurface2D(context,
TEXTURE_RECTANGLE_ARB,
if has_alpha { RGBA as GLenum } else { RGB as GLenum },
width,
height,
BGRA as GLenum,
UNSIGNED_INT_8_8_8_8_REV,
self.as_concrete_TypeRef() as *mut libc::c_void,
0);
if gl_error != kCGLNoError {
let error_msg = CStr::from_ptr(CGLErrorString(gl_error));
let error_msg = error_msg.to_string_lossy();
// This will only actually leak memory if error_msg is a `Cow::Owned`, which
// will only happen if the platform gives us invalid unicode.
panic!(error_msg.leak());
}
}
}
pub fn upload(&self, data: &[u8]) {
unsafe {
let surface = self.as_concrete_TypeRef();
let mut seed = 0;
IOSurfaceLock(surface, 0, &mut seed);
let height = IOSurfaceGetHeight(surface);
let stride = IOSurfaceGetBytesPerRow(surface);
let size = (height * stride) as usize;
let address = IOSurfaceGetBaseAddress(surface) as *mut u8;
let dest: &mut [u8] = slice::from_raw_parts_mut(address, size);
dest.clone_from_slice(data);
// FIXME(pcwalton): RAII
IOSurfaceUnlock(surface, 0, &mut seed);
}
}
}
#[link(name = "IOSurface", kind = "framework")]
extern {
pub static kIOSurfaceAllocSize: CFStringRef;
pub static kIOSurfaceWidth: CFStringRef;
pub static kIOSurfaceHeight: CFStringRef;
pub static kIOSurfaceBytesPerRow: CFStringRef;
pub static kIOSurfaceBytesPerElement: CFStringRef;
pub static kIOSurfaceElementWidth: CFStringRef;
pub static kIOSurfaceElementHeight: CFStringRef;
pub static kIOSurfaceOffset: CFStringRef;
pub static kIOSurfacePlaneInfo: CFStringRef;
pub static kIOSurfacePlaneWidth: CFStringRef;
pub static kIOSurfacePlaneHeight: CFStringRef;
pub static kIOSurfacePlaneBytesPerRow: CFStringRef;
pub static kIOSurfacePlaneOffset: CFStringRef;
pub static kIOSurfacePlaneSize: CFStringRef;
pub static kIOSurfacePlaneBase: CFStringRef;
pub static kIOSurfacePlaneBytesPerElement: CFStringRef;
pub static kIOSurfacePlaneElementWidth: CFStringRef;
pub static kIOSurfacePlaneElementHeight: CFStringRef;
pub static kIOSurfaceCacheMode: CFStringRef;
pub static kIOSurfaceIsGlobal: CFStringRef;
pub static kIOSurfacePixelFormat: CFStringRef;
fn IOSurfaceCreate(properties: CFDictionaryRef) -> IOSurfaceRef;
fn IOSurfaceLookup(csid: IOSurfaceID) -> IOSurfaceRef;
fn IOSurfaceGetID(buffer: IOSurfaceRef) -> IOSurfaceID;
fn IOSurfaceGetTypeID() -> CFTypeID;
fn IOSurfaceLock(buffer: IOSurfaceRef, options: u32, seed: *mut u32) -> IOReturn;
fn IOSurfaceUnlock(buffer: IOSurfaceRef, options: u32, seed: *mut u32) -> IOReturn;
fn IOSurfaceGetHeight(buffer: IOSurfaceRef) -> size_t;
fn IOSurfaceGetBytesPerRow(buffer: IOSurfaceRef) -> size_t;
fn IOSurfaceGetBaseAddress(buffer: IOSurfaceRef) -> *mut c_void;
}

View File

@ -0,0 +1 @@
{"files":{"Cargo.toml":"8800b6172da0ce015fdc69cbe3fdc48c22f4223babe254e207255c681c83e8dd","src/lib.rs":"1bf5c3379719fb99ef78ae60220d70c377d75273b3eb9a7d538c996fabe3077c"},"package":"bd100e01f1154f2908dfa7d02219aeab25d0b9c7fa955164192e3245255a0c73"}

10
third_party/rust/leak/Cargo.toml vendored Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "leak"
version = "0.1.2"
authors = ["Cody P Schafer <dev@codyps.com>"]
license = "Apache-2.0 OR MIT"
description = "Safely leak data from owned data structures"
keywords = [ "leak", "rfc1233", "own" ]
documentation = "http://codyps.com/docs/leak/x86_64-unknown-linux-gnu/stable/leak/trait.Leak.html"
repository = "https://github.com/jmesmon/leak.git"
include = [ "Cargo.toml", "src/*.rs" ]

133
third_party/rust/leak/src/lib.rs vendored Normal file
View File

@ -0,0 +1,133 @@
use std::mem;
/**
* Leak a piece of data by never calling its destructor
*
* Useful for things that are going to be used for the life of the program, but aren't technically
* static (because they are created in response to arguments, environment, or other
* configuration/data read at program start).
*
* This is a modified version of the proposed rfc: https://github.com/rust-lang/rfcs/pull/1233
*
*
* Notable changes:
* - for user convenience, leak() is a non-static method
* - Return `&T` instead of `&mut T`
*
* While it would be ideal to return a `&'a mut T`, we apparently can't do that due to limitations
* in rust's borrow checker causing soundness issues. Details are in the RFC liked above.
*/
pub trait Leak<T : ?Sized> {
fn leak<'a>(self) -> &'a T where T: 'a;
}
impl<T : ?Sized> Leak<T> for Box<T> {
fn leak<'a>(self) -> &'a T where T: 'a {
let r = Self::into_raw(self);
unsafe { &mut *r }
}
}
/*
* while String and Vec<T> could have impls in terms of Box, we specialize them because their
* conversions to Box (into_boxed_slice and into_boxed_str) result in resizing underlying storage
*/
impl Leak<str> for String {
fn leak<'a>(mut self) -> &'a str where Self: 'a {
let r: *mut str = &mut self[..];
mem::forget(self);
unsafe { &mut *r }
}
}
impl<T> Leak<[T]> for Vec<T> {
fn leak<'a>(mut self) -> &'a [T] where [T]: 'a {
let r: *mut [T] = &mut self[..];
mem::forget(self);
unsafe { &mut *r }
}
}
#[cfg(test)]
mod test {
#[test]
fn leak_str() {
use super::Leak;
use std::borrow::ToOwned;
let v = "hi";
let leaked : &str = {
let o = v.to_owned();
o.leak()
};
assert_eq!(leaked, v);
let leaked : &'static str = {
let o = v.to_owned();
o.leak()
};
assert_eq!(leaked, v);
}
#[test]
fn leak_empty_str() {
use super::Leak;
use std::borrow::ToOwned;
let v = "";
let leaked : &'static str = {
let o = v.to_owned();
o.leak()
};
assert_eq!(leaked, v);
}
#[test]
fn leak_vec() {
use super::Leak;
let v = vec![3, 5];
let leaked : &'static [u8] = {
let o = v.clone();
o.leak()
};
assert_eq!(leaked, &*v);
}
#[test]
fn leak_empty_vec() {
use super::Leak;
let v = vec![];
let leaked : &'static [u8] = {
let o = v.clone();
o.leak()
};
assert_eq!(leaked, &*v);
}
#[test]
fn leak_box() {
use super::Leak;
let v : Box<[&str]> = vec!["hi", "there"].into_boxed_slice();
let leaked : &'static [&str] = {
let o = v.clone();
o.leak()
};
assert_eq!(leaked, &*v);
}
#[test]
fn leak_nested() {
use super::Leak;
let v : Box<Vec<&str>> = Box::new(vec!["hi", "there"]);
let leaked : &'static [&str] = {
let o = v.clone();
o.leak()
};
assert_eq!(leaked, &**v);
}
}

View File

@ -0,0 +1 @@
{"files":{"Cargo.toml":"cd5e595a4b4eede02b0c1b96bb86eb3860bf660fafd7ca6eb0f92b96d9e733f5","README.md":"de022877415d090896cc7050408bdd2973fd34816f654327b139e32b9e6ddf44","src/lib.rs":"5c34776cad80147e94ce1c4af40135d645b478276abf16dc44265cd57da77ea0"},"package":"40a8225d44241fd324a8af2806ba635fc7c8a7e9a7de4d5cf3ef54e71f5926fc"}

13
third_party/rust/leaky-cow/Cargo.toml vendored Normal file
View File

@ -0,0 +1,13 @@
[package]
name = "leaky-cow"
version = "0.1.1"
authors = ["Michael Howell <michael@notriddle.com>"]
documentation = "https://www.notriddle.com/rustdoc/leaky_cow/"
readme = "README.md"
repository = "https://github.com/notriddle/rust-leaky-cow"
description = "Leak the contents of a Clone-On-Write pointer"
license = "MIT / Apache-2.0"
[dependencies]
leak = "0.1.1"

9
third_party/rust/leaky-cow/README.md vendored Normal file
View File

@ -0,0 +1,9 @@
Leaky cows
==========
[![Build Status](https://travis-ci.org/notriddle/rust-leaky-cow.svg)](https://travis-ci.org/notriddle/rust-leaky-cow)
[Documentation](https://www.notriddle.com/rustdoc/leaky-cow/)
Adds a `leak()` method to Rust's Clone-On-Write pointer.

35
third_party/rust/leaky-cow/src/lib.rs vendored Normal file
View File

@ -0,0 +1,35 @@
// This library is released under the same terms as Rust itself.
//! Leaky cows: convert a clone-on-write reference into a plain reference. If it's just the
//! plain reference, just return that. If it's the owned structure, leak it and return a
//! reference to it.
extern crate leak;
use leak::Leak;
use std::borrow::{Borrow, Cow, ToOwned};
pub trait LeakyCow<'a, B: ?Sized> {
fn leak(self) -> &'a B;
}
impl<'a, B: ?Sized> LeakyCow<'a, B> for Cow<'a, B> where B: 'a + ToOwned, B::Owned: Leak<B> {
fn leak(self) -> &'a B {
match self {
Cow::Owned(x) => x.leak(),
Cow::Borrowed(x) => x,
}
}
}
#[cfg(test)]
mod test {
use super::LeakyCow;
use std::borrow::Cow;
#[test]
fn test_string() {
let _ = {
let c = Cow::Borrowed("Let it go!");
c.leak();
};
}
}