diff options
author | David Tolnay <dtolnay@gmail.com> | 2020-04-30 07:28:21 -0700 |
---|---|---|
committer | David Tolnay <dtolnay@gmail.com> | 2020-04-30 07:45:34 -0700 |
commit | 3c90cd2ef80e799c249436f4679aa3c12d1f2b91 (patch) | |
tree | 919e0f3503c222ddbe026833f8fd0907a5f57b68 | |
parent | 74dd379f093a2955d7657dffb7d5acfa9e9aeaaa (diff) | |
download | cxx-3c90cd2ef80e799c249436f4679aa3c12d1f2b91.tar.gz |
Move Rust symbols required by C++ to a separate Buck target
Closes #168.
-rw-r--r-- | BUCK | 10 | ||||
-rw-r--r-- | src/exception.rs | 8 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | src/macros/assert.rs (renamed from src/assert.rs) | 0 | ||||
-rw-r--r-- | src/macros/concat.rs (renamed from src/concat.rs) | 0 | ||||
-rw-r--r-- | src/macros/mod.rs | 4 | ||||
-rw-r--r-- | src/rust_str.rs | 6 | ||||
-rw-r--r-- | src/rust_string.rs | 46 | ||||
-rw-r--r-- | src/rust_vec.rs | 54 | ||||
-rw-r--r-- | src/symbols/exception.rs | 8 | ||||
-rw-r--r-- | src/symbols/lib.rs | 5 | ||||
-rw-r--r-- | src/symbols/mod.rs | 4 | ||||
-rw-r--r-- | src/symbols/rust_str.rs | 8 | ||||
-rw-r--r-- | src/symbols/rust_string.rs | 45 | ||||
-rw-r--r-- | src/symbols/rust_vec.rs | 65 |
15 files changed, 153 insertions, 117 deletions
@@ -1,7 +1,8 @@ rust_library( name = "cxx", - srcs = glob(["src/**"]), + srcs = glob(["src/**"], exclude = ["src/symbols/**"]), visibility = ["PUBLIC"], + rustc_flags = ["--cfg", "no_export_symbols"], deps = [ ":core", ":macro", @@ -33,6 +34,13 @@ cxx_library( "cxx.h": "include/cxx.h", }, exported_linker_flags = ["-lstdc++"], + deps = [":symbols"], +) + +rust_library( + name = "symbols", + srcs = glob(["src/macros/**", "src/symbols/**"]), + crate_root = "src/symbols/lib.rs", ) rust_library( diff --git a/src/exception.rs b/src/exception.rs index 52f0cf6d..125e4843 100644 --- a/src/exception.rs +++ b/src/exception.rs @@ -1,5 +1,4 @@ use std::fmt::{self, Debug, Display}; -use std::slice; /// Exception thrown from an `extern "C"` function. #[derive(Debug)] @@ -20,10 +19,3 @@ impl Exception { &self.what } } - -#[export_name = "cxxbridge03$exception"] -unsafe extern "C" fn exception(ptr: *const u8, len: usize) -> *const u8 { - let slice = slice::from_raw_parts(ptr, len); - let boxed = String::from_utf8_lossy(slice).into_owned().into_boxed_str(); - Box::leak(boxed).as_ptr() -} @@ -363,9 +363,7 @@ extern crate link_cplusplus; #[macro_use] -mod assert; -#[macro_use] -mod concat; +mod macros; mod cxx_string; mod cxx_vector; @@ -380,6 +378,9 @@ mod rust_vec; mod unique_ptr; mod unwind; +#[cfg(not(no_export_symbols))] +mod symbols; + pub use crate::cxx_string::CxxString; pub use crate::cxx_vector::CxxVector; pub use crate::exception::Exception; diff --git a/src/assert.rs b/src/macros/assert.rs index 738e5bbd..738e5bbd 100644 --- a/src/assert.rs +++ b/src/macros/assert.rs diff --git a/src/concat.rs b/src/macros/concat.rs index e67e50d4..e67e50d4 100644 --- a/src/concat.rs +++ b/src/macros/concat.rs diff --git a/src/macros/mod.rs b/src/macros/mod.rs new file mode 100644 index 00000000..d12d96bd --- /dev/null +++ b/src/macros/mod.rs @@ -0,0 +1,4 @@ +#[macro_use] +mod assert; +#[macro_use] +mod concat; diff --git a/src/rust_str.rs b/src/rust_str.rs index fe114735..b944ede9 100644 --- a/src/rust_str.rs +++ b/src/rust_str.rs @@ -25,10 +25,4 @@ impl RustStr { } } -#[export_name = "cxxbridge03$str$valid"] -unsafe extern "C" fn str_valid(ptr: *const u8, len: usize) -> bool { - let slice = slice::from_raw_parts(ptr, len); - str::from_utf8(slice).is_ok() -} - const_assert_eq!(mem::size_of::<Option<RustStr>>(), mem::size_of::<RustStr>()); diff --git a/src/rust_string.rs b/src/rust_string.rs index 6f3e64bd..a923ced9 100644 --- a/src/rust_string.rs +++ b/src/rust_string.rs @@ -1,7 +1,4 @@ -use std::mem::{self, ManuallyDrop, MaybeUninit}; -use std::ptr; -use std::slice; -use std::str; +use std::mem; #[repr(C)] pub struct RustString { @@ -30,46 +27,5 @@ impl RustString { } } -#[export_name = "cxxbridge03$string$new"] -unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) { - ptr::write(this.as_mut_ptr(), String::new()); -} - -#[export_name = "cxxbridge03$string$clone"] -unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) { - ptr::write(this.as_mut_ptr(), other.clone()); -} - -#[export_name = "cxxbridge03$string$from"] -unsafe extern "C" fn string_from( - this: &mut MaybeUninit<String>, - ptr: *const u8, - len: usize, -) -> bool { - let slice = slice::from_raw_parts(ptr, len); - match str::from_utf8(slice) { - Ok(s) => { - ptr::write(this.as_mut_ptr(), s.to_owned()); - true - } - Err(_) => false, - } -} - -#[export_name = "cxxbridge03$string$drop"] -unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) { - ManuallyDrop::drop(this); -} - -#[export_name = "cxxbridge03$string$ptr"] -unsafe extern "C" fn string_ptr(this: &String) -> *const u8 { - this.as_ptr() -} - -#[export_name = "cxxbridge03$string$len"] -unsafe extern "C" fn string_len(this: &String) -> usize { - this.len() -} - const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<String>()); const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<String>()); diff --git a/src/rust_vec.rs b/src/rust_vec.rs index c17310d9..4c5035d6 100644 --- a/src/rust_vec.rs +++ b/src/rust_vec.rs @@ -1,6 +1,3 @@ -use std::mem; -use std::ptr; - #[repr(C)] pub struct RustVec<T> { repr: Vec<T>, @@ -39,54 +36,3 @@ impl<T> RustVec<T> { self.repr.as_ptr() } } - -macro_rules! rust_vec_shims_for_primitive { - ($ty:ident) => { - const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<$ty>>()); - const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<$ty>>()); - - const _: () = { - attr! { - #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$new")] - unsafe extern "C" fn __new(this: *mut RustVec<$ty>) { - ptr::write(this, RustVec::new()); - } - } - attr! { - #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$drop")] - unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) { - ptr::drop_in_place(this); - } - } - attr! { - #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$len")] - unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize { - (*this).len() - } - } - attr! { - #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$data")] - unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty { - (*this).as_ptr() - } - } - attr! { - #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$stride")] - unsafe extern "C" fn __stride() -> usize { - mem::size_of::<$ty>() - } - } - }; - }; -} - -rust_vec_shims_for_primitive!(u8); -rust_vec_shims_for_primitive!(u16); -rust_vec_shims_for_primitive!(u32); -rust_vec_shims_for_primitive!(u64); -rust_vec_shims_for_primitive!(i8); -rust_vec_shims_for_primitive!(i16); -rust_vec_shims_for_primitive!(i32); -rust_vec_shims_for_primitive!(i64); -rust_vec_shims_for_primitive!(f32); -rust_vec_shims_for_primitive!(f64); diff --git a/src/symbols/exception.rs b/src/symbols/exception.rs new file mode 100644 index 00000000..849db3bc --- /dev/null +++ b/src/symbols/exception.rs @@ -0,0 +1,8 @@ +use std::slice; + +#[export_name = "cxxbridge03$exception"] +unsafe extern "C" fn exception(ptr: *const u8, len: usize) -> *const u8 { + let slice = slice::from_raw_parts(ptr, len); + let boxed = String::from_utf8_lossy(slice).into_owned().into_boxed_str(); + Box::leak(boxed).as_ptr() +} diff --git a/src/symbols/lib.rs b/src/symbols/lib.rs new file mode 100644 index 00000000..2c052ecf --- /dev/null +++ b/src/symbols/lib.rs @@ -0,0 +1,5 @@ +#[path = "../macros/mod.rs"] +#[macro_use] +mod macros; + +include!("mod.rs"); diff --git a/src/symbols/mod.rs b/src/symbols/mod.rs new file mode 100644 index 00000000..a9d158db --- /dev/null +++ b/src/symbols/mod.rs @@ -0,0 +1,4 @@ +mod exception; +mod rust_str; +mod rust_string; +mod rust_vec; diff --git a/src/symbols/rust_str.rs b/src/symbols/rust_str.rs new file mode 100644 index 00000000..6dc04acd --- /dev/null +++ b/src/symbols/rust_str.rs @@ -0,0 +1,8 @@ +use std::slice; +use std::str; + +#[export_name = "cxxbridge03$str$valid"] +unsafe extern "C" fn str_valid(ptr: *const u8, len: usize) -> bool { + let slice = slice::from_raw_parts(ptr, len); + str::from_utf8(slice).is_ok() +} diff --git a/src/symbols/rust_string.rs b/src/symbols/rust_string.rs new file mode 100644 index 00000000..63d4ba76 --- /dev/null +++ b/src/symbols/rust_string.rs @@ -0,0 +1,45 @@ +use std::mem::{ManuallyDrop, MaybeUninit}; +use std::ptr; +use std::slice; +use std::str; + +#[export_name = "cxxbridge03$string$new"] +unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) { + ptr::write(this.as_mut_ptr(), String::new()); +} + +#[export_name = "cxxbridge03$string$clone"] +unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) { + ptr::write(this.as_mut_ptr(), other.clone()); +} + +#[export_name = "cxxbridge03$string$from"] +unsafe extern "C" fn string_from( + this: &mut MaybeUninit<String>, + ptr: *const u8, + len: usize, +) -> bool { + let slice = slice::from_raw_parts(ptr, len); + match str::from_utf8(slice) { + Ok(s) => { + ptr::write(this.as_mut_ptr(), s.to_owned()); + true + } + Err(_) => false, + } +} + +#[export_name = "cxxbridge03$string$drop"] +unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) { + ManuallyDrop::drop(this); +} + +#[export_name = "cxxbridge03$string$ptr"] +unsafe extern "C" fn string_ptr(this: &String) -> *const u8 { + this.as_ptr() +} + +#[export_name = "cxxbridge03$string$len"] +unsafe extern "C" fn string_len(this: &String) -> usize { + this.len() +} diff --git a/src/symbols/rust_vec.rs b/src/symbols/rust_vec.rs new file mode 100644 index 00000000..712a9e8c --- /dev/null +++ b/src/symbols/rust_vec.rs @@ -0,0 +1,65 @@ +use std::mem; +use std::ptr; + +#[repr(C)] +pub struct RustVec<T> { + repr: Vec<T>, +} + +macro_rules! attr { + (#[$name:ident = $value:expr] $($rest:tt)*) => { + #[$name = $value] + $($rest)* + }; +} + +macro_rules! rust_vec_shims_for_primitive { + ($ty:ident) => { + const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<Vec<$ty>>()); + const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<Vec<$ty>>()); + + const _: () = { + attr! { + #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$new")] + unsafe extern "C" fn __new(this: *mut RustVec<$ty>) { + ptr::write(this, RustVec { repr: Vec::new() }); + } + } + attr! { + #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$drop")] + unsafe extern "C" fn __drop(this: *mut RustVec<$ty>) { + ptr::drop_in_place(this); + } + } + attr! { + #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$len")] + unsafe extern "C" fn __len(this: *const RustVec<$ty>) -> usize { + (*this).repr.len() + } + } + attr! { + #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$data")] + unsafe extern "C" fn __data(this: *const RustVec<$ty>) -> *const $ty { + (*this).repr.as_ptr() + } + } + attr! { + #[export_name = concat!("cxxbridge03$rust_vec$", stringify!($ty), "$stride")] + unsafe extern "C" fn __stride() -> usize { + mem::size_of::<$ty>() + } + } + }; + }; +} + +rust_vec_shims_for_primitive!(u8); +rust_vec_shims_for_primitive!(u16); +rust_vec_shims_for_primitive!(u32); +rust_vec_shims_for_primitive!(u64); +rust_vec_shims_for_primitive!(i8); +rust_vec_shims_for_primitive!(i16); +rust_vec_shims_for_primitive!(i32); +rust_vec_shims_for_primitive!(i64); +rust_vec_shims_for_primitive!(f32); +rust_vec_shims_for_primitive!(f64); |