mirror of
https://gitee.com/openharmony/third_party_rust_cxx
synced 2024-11-27 01:11:38 +00:00
Add alloc feature behind feature gate
This commit is contained in:
parent
42a36efccc
commit
339fbf9083
4
BUCK
4
BUCK
@ -1,6 +1,10 @@
|
||||
rust_library(
|
||||
name = "cxx",
|
||||
srcs = glob(["src/**"]),
|
||||
features = [
|
||||
"alloc",
|
||||
"std",
|
||||
],
|
||||
visibility = ["PUBLIC"],
|
||||
deps = [
|
||||
":core",
|
||||
|
4
BUILD
4
BUILD
@ -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",
|
||||
],
|
||||
|
@ -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" }
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![cfg(feature = "alloc")]
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use core::fmt::{self, Display};
|
||||
|
||||
|
@ -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"
|
||||
}
|
||||
|
16
src/lib.rs
16
src/lib.rs
@ -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;
|
||||
|
@ -1,3 +1,4 @@
|
||||
#![cfg(feature = "alloc")]
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use crate::exception::Exception;
|
||||
|
@ -1,3 +1,4 @@
|
||||
#![cfg(feature = "alloc")]
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use alloc::string::String;
|
||||
|
@ -1,3 +1,4 @@
|
||||
#![cfg(feature = "alloc")]
|
||||
#![allow(missing_docs)]
|
||||
|
||||
use crate::rust_string::RustString;
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![cfg(feature = "alloc")]
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use alloc::string::String;
|
||||
use core::slice;
|
||||
|
@ -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();
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![cfg(feature = "alloc")]
|
||||
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::string::String;
|
||||
use core::mem::{ManuallyDrop, MaybeUninit};
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![cfg(feature = "alloc")]
|
||||
|
||||
use crate::c_char::c_char;
|
||||
use crate::rust_string::RustString;
|
||||
use crate::rust_vec::RustVec;
|
||||
|
Loading…
Reference in New Issue
Block a user