Add alloc feature behind feature gate

This commit is contained in:
David Tolnay 2021-12-07 14:49:41 -08:00
parent 42a36efccc
commit 339fbf9083
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
15 changed files with 53 additions and 3 deletions

4
BUCK
View File

@ -1,6 +1,10 @@
rust_library(
name = "cxx",
srcs = glob(["src/**"]),
features = [
"alloc",
"std",
],
visibility = ["PUBLIC"],
deps = [
":core",

4
BUILD
View File

@ -4,6 +4,10 @@ load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library", "rust_proc_mac
rust_library(
name = "cxx",
srcs = glob(["src/**/*.rs"]),
crate_features = [
"alloc",
"std",
],
proc_macro_deps = [
":cxxbridge-macro",
],

View File

@ -20,7 +20,8 @@ default = ["std", "cxxbridge-flags/default"] # c++11
"c++14" = ["cxxbridge-flags/c++14"]
"c++17" = ["cxxbridge-flags/c++17"]
"c++20" = ["cxxbridge-flags/c++20"]
std = []
alloc = []
std = ["alloc"]
[dependencies]
cxxbridge-macro = { version = "=1.0.57", path = "macro" }

View File

@ -1,3 +1,5 @@
#![cfg(feature = "alloc")]
pub type c_char = c_char_definition::c_char;
// Validate that our definition is consistent with libstd's definition, without

View File

@ -1,7 +1,10 @@
use crate::actually_private::Private;
#[cfg(feature = "alloc")]
use alloc::borrow::Cow;
#[cfg(feature = "alloc")]
use alloc::string::String;
use core::cmp::Ordering;
#[cfg(feature = "alloc")]
use core::fmt::{self, Debug, Display};
use core::hash::{Hash, Hasher};
use core::marker::{PhantomData, PhantomPinned};
@ -144,6 +147,7 @@ impl CxxString {
/// Cow::Owned String.
///
/// [replacement character]: https://doc.rust-lang.org/std/char/constant.REPLACEMENT_CHARACTER.html
#[cfg(feature = "alloc")]
pub fn to_string_lossy(&self) -> Cow<str> {
String::from_utf8_lossy(self.as_bytes())
}
@ -202,12 +206,14 @@ impl CxxString {
}
}
#[cfg(feature = "alloc")]
impl Display for CxxString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(self.to_string_lossy().as_ref(), f)
}
}
#[cfg(feature = "alloc")]
impl Debug for CxxString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Debug::fmt(self.to_string_lossy().as_ref(), f)

View File

@ -1,3 +1,5 @@
#![cfg(feature = "alloc")]
use alloc::boxed::Box;
use core::fmt::{self, Display};

View File

@ -1,5 +1,6 @@
use self::kind::{Kind, Opaque, Trivial};
use crate::CxxString;
#[cfg(feature = "alloc")]
use alloc::string::String;
/// A type for which the layout is determined by its C++ definition.
@ -212,8 +213,13 @@ impl_extern_type! {
isize = "rust::isize"
f32 = "float"
f64 = "double"
String = "rust::String"
[Opaque]
CxxString = "std::string"
}
#[cfg(feature = "alloc")]
impl_extern_type! {
[Trivial]
String = "rust::String"
}

View File

@ -395,9 +395,14 @@
#[cfg(built_with_cargo)]
extern crate link_cplusplus;
extern crate alloc;
extern crate self as cxx;
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(not(feature = "alloc"))]
extern crate core as alloc;
#[cfg(feature = "std")]
extern crate std;
@ -406,6 +411,11 @@ extern crate std;
#[cfg(not(feature = "std"))]
extern crate core as std;
#[cfg(not(any(feature = "alloc", cxx_experimental_no_alloc)))]
compile_error! {
r#"cxx support for no_alloc is incomplete and semver exempt; you must build with at least one of feature="std", feature="alloc", or RUSTFLAGS='--cfg cxx_experimental_no_alloc'"#
}
#[macro_use]
mod macros;
@ -434,6 +444,7 @@ pub mod vector;
mod weak_ptr;
pub use crate::cxx_vector::CxxVector;
#[cfg(feature = "alloc")]
pub use crate::exception::Exception;
pub use crate::extern_type::{kind, ExternType};
pub use crate::shared_ptr::SharedPtr;
@ -463,11 +474,14 @@ pub mod private {
pub use crate::extern_type::{verify_extern_kind, verify_extern_type};
pub use crate::function::FatFunction;
pub use crate::opaque::Opaque;
#[cfg(feature = "alloc")]
pub use crate::result::{r#try, Result};
pub use crate::rust_slice::RustSlice;
pub use crate::rust_str::RustStr;
#[cfg(feature = "alloc")]
pub use crate::rust_string::RustString;
pub use crate::rust_type::{ImplBox, ImplVec, RustType};
#[cfg(feature = "alloc")]
pub use crate::rust_vec::RustVec;
pub use crate::shared_ptr::SharedPtrTarget;
pub use crate::string::StackString;

View File

@ -1,3 +1,4 @@
#![cfg(feature = "alloc")]
#![allow(missing_docs)]
use crate::exception::Exception;

View File

@ -1,3 +1,4 @@
#![cfg(feature = "alloc")]
#![allow(missing_docs)]
use alloc::string::String;

View File

@ -1,3 +1,4 @@
#![cfg(feature = "alloc")]
#![allow(missing_docs)]
use crate::rust_string::RustString;

View File

@ -1,3 +1,5 @@
#![cfg(feature = "alloc")]
use alloc::boxed::Box;
use alloc::string::String;
use core::slice;

View File

@ -1,3 +1,4 @@
#[cfg(feature = "alloc")]
use alloc::string::String;
use core::mem::MaybeUninit;
use core::ptr;
@ -10,6 +11,7 @@ unsafe extern "C" fn str_new(this: &mut MaybeUninit<&str>) {
unsafe { ptr::write(this, "") }
}
#[cfg(feature = "alloc")]
#[export_name = "cxxbridge1$str$ref"]
unsafe extern "C" fn str_ref<'a>(this: &mut MaybeUninit<&'a str>, string: &'a String) {
let this = this.as_mut_ptr();

View File

@ -1,3 +1,5 @@
#![cfg(feature = "alloc")]
use alloc::borrow::ToOwned;
use alloc::string::String;
use core::mem::{ManuallyDrop, MaybeUninit};

View File

@ -1,3 +1,5 @@
#![cfg(feature = "alloc")]
use crate::c_char::c_char;
use crate::rust_string::RustString;
use crate::rust_vec::RustVec;