Bug 1664525 - Part 1: Update various xpcom crates to rust2018, r=emilio

Differential Revision: https://phabricator.services.mozilla.com/D89946
This commit is contained in:
Nika Layzell 2020-09-11 20:19:00 +00:00
Родитель e0c3e2f9a8
Коммит 7b154ea902
17 изменённых файлов: 59 добавлений и 113 удалений

Просмотреть файл

@ -235,7 +235,7 @@ def services_rs(output):
output.write("""\
/* THIS FILE IS GENERATED BY Services.py - DO NOT EDIT */
use RefPtr;
use crate::RefPtr;
""")
for (name, iface, _) in services:
@ -246,9 +246,9 @@ use RefPtr;
output.write("""
/// Fetches a cached reference to the `%(name)s`.
/// This function will return `None` during XPCOM shutdown.
pub fn get_%(name)s() -> Option<RefPtr<::interfaces::%(type)s>> {
pub fn get_%(name)s() -> Option<RefPtr<crate::interfaces::%(type)s>> {
extern "C" {
fn XPCOMService_Get%(name)s() -> *mut ::interfaces::%(type)s;
fn XPCOMService_Get%(name)s() -> *mut crate::interfaces::%(type)s;
}
unsafe { RefPtr::from_raw_dont_addref(XPCOMService_Get%(name)s()) }
}

Просмотреть файл

@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["Nika Layzell <nika@thelayzells.com>"]
license = "MPL-2.0"
description = "Rust bindings to xpcom nsresult and NS_ERROR_ values"
edition = "2018"
[dependencies]
nsstring = { path = "../nsstring" }

Просмотреть файл

@ -2,8 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate nsstring;
use nsstring::{nsACString, nsCString};
use std::error::Error;
use std::fmt;

Просмотреть файл

@ -4,6 +4,7 @@ version = "0.1.0"
authors = ["nobody@mozilla.com"]
license = "MPL-2.0"
description = "Rust bindings to xpcom string types"
edition = "2018"
[features]
gecko_debug = []

Просмотреть файл

@ -2,20 +2,14 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
extern crate encoding_rs;
use crate::{
nsACString, nsAString, nsCStringLike, BulkWriteOk, Gecko_FallibleAssignCString,
Latin1StringLike,
};
use encoding_rs::mem::*;
use encoding_rs::Encoding;
use std::slice;
use super::nsACString;
use super::nsAString;
use super::nsCStringLike;
use super::BulkWriteOk;
use super::Gecko_FallibleAssignCString;
use super::Latin1StringLike;
use conversions::encoding_rs::mem::*;
use conversions::encoding_rs::Encoding;
/// Required math stated in the docs of
/// `convert_utf16_to_utf8()`.
#[inline(always)]

Просмотреть файл

@ -114,9 +114,7 @@
#![allow(non_camel_case_types)]
#[macro_use]
extern crate bitflags;
use bitflags::bitflags;
use std::borrow;
use std::cmp;
use std::fmt;
@ -127,7 +125,6 @@ use std::os::raw::c_void;
use std::ptr;
use std::slice;
use std::str;
use std::u32;
mod conversions;
@ -153,38 +150,31 @@ const SHRINKING_THRESHOLD: usize = 64;
// Internal Implementation Flags //
///////////////////////////////////
mod data_flags {
bitflags! {
// While this has the same layout as u16, it cannot be passed
// over FFI safely as a u16.
#[repr(C)]
pub struct DataFlags: u16 {
const TERMINATED = 1 << 0; // IsTerminated returns true
const VOIDED = 1 << 1; // IsVoid returns true
const REFCOUNTED = 1 << 2; // mData points to a heap-allocated, shareable, refcounted
// buffer
const OWNED = 1 << 3; // mData points to a heap-allocated, raw buffer
const INLINE = 1 << 4; // mData points to a writable, inline buffer
const LITERAL = 1 << 5; // mData points to a string literal; TERMINATED will also be set
}
bitflags! {
// While this has the same layout as u16, it cannot be passed
// over FFI safely as a u16.
#[repr(C)]
struct DataFlags: u16 {
const TERMINATED = 1 << 0; // IsTerminated returns true
const VOIDED = 1 << 1; // IsVoid returns true
const REFCOUNTED = 1 << 2; // mData points to a heap-allocated, shareable, refcounted
// buffer
const OWNED = 1 << 3; // mData points to a heap-allocated, raw buffer
const INLINE = 1 << 4; // mData points to a writable, inline buffer
const LITERAL = 1 << 5; // mData points to a string literal; TERMINATED will also be set
}
}
mod class_flags {
bitflags! {
// While this has the same layout as u16, it cannot be passed
// over FFI safely as a u16.
#[repr(C)]
pub struct ClassFlags: u16 {
const INLINE = 1 << 0; // |this|'s buffer is inline
const NULL_TERMINATED = 1 << 1; // |this| requires its buffer is null-terminated
}
bitflags! {
// While this has the same layout as u16, it cannot be passed
// over FFI safely as a u16.
#[repr(C)]
struct ClassFlags: u16 {
const INLINE = 1 << 0; // |this|'s buffer is inline
const NULL_TERMINATED = 1 << 1; // |this| requires its buffer is null-terminated
}
}
use class_flags::ClassFlags;
use data_flags::DataFlags;
////////////////////////////////////
// Generic String Bindings Macros //
////////////////////////////////////
@ -642,7 +632,7 @@ macro_rules! define_string_types {
capacity: usize,
units_to_preserve: usize,
allow_shrinking: bool) -> Result<usize, ()> {
if capacity > u32::max_value() as usize {
if capacity > u32::MAX as usize {
Err(())
} else {
let capacity32 = capacity as u32;
@ -650,7 +640,7 @@ macro_rules! define_string_types {
capacity32,
units_to_preserve as u32,
allow_shrinking && capacity > SHRINKING_THRESHOLD);
if rounded == u32::max_value() {
if rounded == u32::MAX {
return Err(())
}
Ok(rounded as usize)

Просмотреть файл

@ -2,6 +2,7 @@
name = "xpcom"
version = "0.1.0"
authors = ["Nika Layzell <nika@thelayzells.com>"]
edition = "2018"
[dependencies]
libc = "0.2"

Просмотреть файл

@ -2,8 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use interfaces::{nsIInterfaceRequestor, nsISupports};
use {GetterAddrefs, RefCounted, RefPtr};
use crate::interfaces::{nsIInterfaceRequestor, nsISupports};
use crate::{GetterAddrefs, RefCounted, RefPtr};
#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq)]

Просмотреть файл

@ -4,8 +4,8 @@
#![allow(bad_style)]
use interfaces::*;
use *;
use crate::interfaces::*;
use crate::*;
// NOTE: This file contains a series of `include!()` invocations, defining all
// idl interfaces directly within this module.

Просмотреть файл

@ -8,7 +8,7 @@
//! NOTE: The IIDs in these files must be kept in sync with the IDL definitions
//! in the corresponding C++ files.
use nsID;
use crate::nsID;
// XXX: This macro should have an option for a custom base interface instead of
// nsISupports, such that Document can have nsINode as a base, etc. For now,

Просмотреть файл

@ -11,18 +11,8 @@
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
extern crate libc;
extern crate nserror;
extern crate nsstring;
extern crate thin_vec;
extern crate threadbound;
// re-export the xpcom_macros macro
#[macro_use]
#[allow(unused_imports)]
extern crate xpcom_macros;
#[doc(hidden)]
pub use xpcom_macros::*;
pub use xpcom_macros::xpcom;
// Helper functions and data structures are exported in the root of the crate.
mod base;

Просмотреть файл

@ -2,8 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate nserror;
use nserror::{nsresult, NS_ERROR_NULL_POINTER};
/// The xpcom_method macro generates a Rust XPCOM method stub that converts
@ -219,7 +217,7 @@ macro_rules! xpcom_method {
/// itself.
#[doc(hidden)]
pub trait Ensure<T> {
unsafe fn ensure(T) -> Self;
unsafe fn ensure(value: T) -> Self;
}
impl<'a, T: 'a> Ensure<*const T> for Result<&'a T, nsresult> {

Просмотреть файл

@ -2,11 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/// The automatically generated code from `xpcom_macros` depends on some types
/// which are defined in other libraries which `xpcom` depends on, but which may
/// not be `extern crate`-ed into the crate the macros are expanded into. This
/// module re-exports those types from `xpcom` so that they can be used from the
/// macro.
//! The automatically generated code from `xpcom_macros` depends on some types
//! which are defined in other libraries which `xpcom` depends on, but which may
//! not be `extern crate`-ed into the crate the macros are expanded into. This
//! module re-exports those types from `xpcom` so that they can be used from the
//! macro.
// re-export libc so it can be used by the procedural macro.
pub extern crate libc;

Просмотреть файл

@ -2,6 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use crate::interfaces::nsrefcnt;
use libc;
use nserror::{nsresult, NS_OK};
use std::cell::Cell;
use std::fmt;
use std::marker::PhantomData;
@ -9,13 +12,6 @@ use std::mem;
use std::ops::Deref;
use std::ptr;
use std::sync::atomic::{self, AtomicUsize, Ordering};
use nserror::{nsresult, NS_OK};
use libc;
use interfaces::nsrefcnt;
use threadbound::ThreadBound;
/// A trait representing a type which can be reference counted invasively.

Просмотреть файл

@ -2,20 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use crate::interfaces::{nsIComponentManager, nsIComponentRegistrar, nsIServiceManager};
use crate::{GetterAddrefs, RefPtr, XpCom};
use std::ffi::CStr;
use std::ptr;
use {GetterAddrefs, RefPtr, XpCom};
use interfaces::{nsIComponentManager, nsIComponentRegistrar, nsIServiceManager};
macro_rules! try_opt {
($e: expr) => {
match $e {
Some(x) => x,
None => return None,
}
};
}
/// Get a reference to the global `nsIComponentManager`.
///
@ -49,7 +39,7 @@ pub fn component_registrar() -> Option<RefPtr<nsIComponentRegistrar>> {
pub fn create_instance<T: XpCom>(id: &CStr) -> Option<RefPtr<T>> {
unsafe {
let mut ga = GetterAddrefs::<T>::new();
if try_opt!(component_manager())
if component_manager()?
.CreateInstanceByContractID(id.as_ptr(), ptr::null(), &T::IID, ga.void_ptr())
.succeeded()
{
@ -68,7 +58,7 @@ pub fn create_instance<T: XpCom>(id: &CStr) -> Option<RefPtr<T>> {
pub fn get_service<T: XpCom>(id: &CStr) -> Option<RefPtr<T>> {
unsafe {
let mut ga = GetterAddrefs::<T>::new();
if try_opt!(service_manager())
if service_manager()?
.GetServiceByContractID(id.as_ptr(), &T::IID, ga.void_ptr())
.succeeded()
{

Просмотреть файл

@ -2,6 +2,7 @@
name = "xpcom_macros"
version = "0.1.0"
authors = ["Nika Layzell <nika@thelayzells.com>"]
edition = "2018"
[lib]
proc-macro = true

Просмотреть файл

@ -144,32 +144,18 @@
// limit.
#![recursion_limit = "256"]
#[macro_use]
extern crate quote;
extern crate syn;
extern crate proc_macro;
extern crate proc_macro2;
#[macro_use]
extern crate lazy_static;
use lazy_static::lazy_static;
use proc_macro::TokenStream;
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::ToTokens;
use quote::{quote, ToTokens};
use std::collections::{HashMap, HashSet};
use std::error::Error;
use syn::punctuated::Punctuated;
use syn::{
parse, parse_quote, Attribute, Data, DataStruct, DeriveInput, Field, Fields, Ident, Lit, Meta,
NestedMeta, Token, Type,
};
use syn::punctuated::Punctuated;
use std::collections::{HashMap, HashSet};
use std::error::Error;
/* These are the structs generated by the rust_macros.py script */
/// A single parameter to an XPCOM method.