use std::ptr::NonNull;
-use super::RustStringRef;
+use super::RustString;
pub type Bool = c_uint;
pub fn LLVMRustDIBuilderCreateOpDeref() -> i64;
pub fn LLVMRustDIBuilderCreateOpPlusUconst() -> i64;
- pub fn LLVMRustWriteTypeToString(Type: &Type, s: RustStringRef);
- pub fn LLVMRustWriteValueToString(value_ref: &Value, s: RustStringRef);
+ pub fn LLVMRustWriteTypeToString(Type: &Type, s: &RustString);
+ pub fn LLVMRustWriteValueToString(value_ref: &Value, s: &RustString);
pub fn LLVMIsAConstantInt(value_ref: &Value) -> Option<&Value>;
pub fn LLVMIsAConstantFP(value_ref: &Value) -> Option<&Value>;
pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t;
- pub fn LLVMRustWriteTwineToString(T: &Twine, s: RustStringRef);
+ pub fn LLVMRustWriteTwineToString(T: &Twine, s: &RustString);
pub fn LLVMContextSetDiagnosticHandler(C: &Context,
Handler: DiagnosticHandler,
DiagnosticContext: *mut c_void);
pub fn LLVMRustUnpackOptimizationDiagnostic(DI: &'a DiagnosticInfo,
- pass_name_out: RustStringRef,
- function_out: *mut Option<&'a Value>,
- loc_line_out: *mut c_uint,
- loc_column_out: *mut c_uint,
- loc_filename_out: RustStringRef,
- message_out: RustStringRef);
+ pass_name_out: &RustString,
+ function_out: &mut Option<&'a Value>,
+ loc_line_out: &mut c_uint,
+ loc_column_out: &mut c_uint,
+ loc_filename_out: &RustString,
+ message_out: &RustString);
pub fn LLVMRustUnpackInlineAsmDiagnostic(DI: &'a DiagnosticInfo,
cookie_out: *mut c_uint,
message_out: *mut Option<&'a Twine>,
instruction_out: *mut Option<&'a Value>);
- pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: RustStringRef);
+ pub fn LLVMRustWriteDiagnosticInfoToString(DI: &DiagnosticInfo, s: &RustString);
pub fn LLVMRustGetDiagInfoKind(DI: &DiagnosticInfo) -> DiagnosticKind;
pub fn LLVMRustSetInlineAsmDiagnosticHandler(C: &Context,
H: InlineAsmDiagHandler,
CX: *mut c_void);
- pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: RustStringRef);
+ pub fn LLVMRustWriteSMDiagnosticToString(d: &SMDiagnostic, s: &RustString);
pub fn LLVMRustWriteArchive(Dst: *const c_char,
NumMembers: size_t,
pub use self::Linkage::*;
use std::str::FromStr;
+use std::string::FromUtf8Error;
use std::slice;
use std::ffi::{CString, CStr};
use std::cell::RefCell;
}
}
-#[allow(missing_copy_implementations)]
-extern { pub type RustString; }
-type RustStringRef = *mut RustString;
-type RustStringRepr = *mut RefCell<Vec<u8>>;
+#[repr(C)]
+pub struct RustString {
+ bytes: RefCell<Vec<u8>>,
+}
/// Appending to a Rust string -- used by RawRustStringOstream.
#[no_mangle]
-pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: RustStringRef,
+pub unsafe extern "C" fn LLVMRustStringWriteImpl(sr: &RustString,
ptr: *const c_char,
size: size_t) {
let slice = slice::from_raw_parts(ptr as *const u8, size as usize);
- let sr = sr as RustStringRepr;
- (*sr).borrow_mut().extend_from_slice(slice);
+ sr.bytes.borrow_mut().extend_from_slice(slice);
}
pub fn SetInstructionCallConv(instr: &'a Value, cc: CallConv) {
}
}
-pub fn build_string<F>(f: F) -> Option<String>
- where F: FnOnce(RustStringRef)
-{
- let mut buf = RefCell::new(Vec::new());
- f(&mut buf as RustStringRepr as RustStringRef);
- String::from_utf8(buf.into_inner()).ok()
+pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
+ let sr = RustString {
+ bytes: RefCell::new(Vec::new()),
+ };
+ f(&sr);
+ String::from_utf8(sr.bytes.into_inner())
}
-pub unsafe fn twine_to_string(tr: &Twine) -> String {
- build_string(|s| LLVMRustWriteTwineToString(tr, s)).expect("got a non-UTF8 Twine from LLVM")
+pub fn twine_to_string(tr: &Twine) -> String {
+ unsafe {
+ build_string(|s| LLVMRustWriteTwineToString(tr, s))
+ .expect("got a non-UTF8 Twine from LLVM")
+ }
}
pub fn last_error() -> Option<String> {