]> git.lizzy.rs Git - rust.git/commitdiff
rustc_codegen_llvm: use safe references for Archive.
authorIrina Popa <irinagpopa@gmail.com>
Fri, 13 Jul 2018 10:06:06 +0000 (13:06 +0300)
committerIrina Popa <irinagpopa@gmail.com>
Mon, 30 Jul 2018 17:10:32 +0000 (20:10 +0300)
src/librustc_codegen_llvm/llvm/archive_ro.rs
src/librustc_codegen_llvm/llvm/ffi.rs

index e25f66919614cd771d429ad9bf23a2b4cf397aba..98a08f8501af9b482a558501f1488e439b8b135c 100644 (file)
@@ -10,8 +10,6 @@
 
 //! A wrapper around LLVM's archive (.a) code
 
-use super::ArchiveRef;
-
 use std::ffi::CString;
 use std::marker;
 use std::path::Path;
@@ -19,7 +17,7 @@
 use std::str;
 
 pub struct ArchiveRO {
-    ptr: ArchiveRef,
+    raw: &'static mut super::Archive,
 }
 
 unsafe impl Send for ArchiveRO {}
@@ -44,12 +42,9 @@ impl ArchiveRO {
     pub fn open(dst: &Path) -> Result<ArchiveRO, String> {
         return unsafe {
             let s = path2cstr(dst);
-            let ar = super::LLVMRustOpenArchive(s.as_ptr());
-            if ar.is_null() {
-                Err(super::last_error().unwrap_or("failed to open archive".to_string()))
-            } else {
-                Ok(ArchiveRO { ptr: ar })
-            }
+            let ar = super::LLVMRustOpenArchive(s.as_ptr())
+                .ok_or_else(|| super::last_error().unwrap_or("failed to open archive".to_string()))?;
+            Ok(ArchiveRO { raw: ar })
         };
 
         #[cfg(unix)]
@@ -65,14 +60,14 @@ fn path2cstr(p: &Path) -> CString {
         }
     }
 
-    pub fn raw(&self) -> ArchiveRef {
-        self.ptr
+    pub fn raw(&self) -> &super::Archive {
+        self.raw
     }
 
     pub fn iter(&self) -> Iter {
         unsafe {
             Iter {
-                ptr: super::LLVMRustArchiveIteratorNew(self.ptr),
+                ptr: super::LLVMRustArchiveIteratorNew(self.raw),
                 _data: marker::PhantomData,
             }
         }
@@ -82,7 +77,7 @@ pub fn iter(&self) -> Iter {
 impl Drop for ArchiveRO {
     fn drop(&mut self) {
         unsafe {
-            super::LLVMRustDestroyArchive(self.ptr);
+            super::LLVMRustDestroyArchive(&mut *(self.raw as *mut _));
         }
     }
 }
index 566bd3c2c187038a24a3e8dd00f800b5da4e0ba4..f6d206cf5adce40e3441f50fa35ba4764e94774b 100644 (file)
@@ -399,7 +399,6 @@ pub enum ThreadLocalMode {
 extern { pub type Pass; }
 extern { pub type TargetMachine; }
 extern { pub type Archive; }
-pub type ArchiveRef = *mut Archive;
 extern { pub type ArchiveIterator; }
 pub type ArchiveIteratorRef = *mut ArchiveIterator;
 extern { pub type ArchiveChild; }
@@ -1471,14 +1470,14 @@ pub fn LLVMRustPrintModule(PM: PassManagerRef,
     pub fn LLVMRustRunRestrictionPass(M: &Module, syms: *const *const c_char, len: size_t);
     pub fn LLVMRustMarkAllFunctionsNounwind(M: &Module);
 
-    pub fn LLVMRustOpenArchive(path: *const c_char) -> ArchiveRef;
-    pub fn LLVMRustArchiveIteratorNew(AR: ArchiveRef) -> ArchiveIteratorRef;
+    pub fn LLVMRustOpenArchive(path: *const c_char) -> Option<&'static mut Archive>;
+    pub fn LLVMRustArchiveIteratorNew(AR: &Archive) -> ArchiveIteratorRef;
     pub fn LLVMRustArchiveIteratorNext(AIR: ArchiveIteratorRef) -> ArchiveChildRef;
     pub fn LLVMRustArchiveChildName(ACR: ArchiveChildRef, size: *mut size_t) -> *const c_char;
     pub fn LLVMRustArchiveChildData(ACR: ArchiveChildRef, size: *mut size_t) -> *const c_char;
     pub fn LLVMRustArchiveChildFree(ACR: ArchiveChildRef);
     pub fn LLVMRustArchiveIteratorFree(AIR: ArchiveIteratorRef);
-    pub fn LLVMRustDestroyArchive(AR: ArchiveRef);
+    pub fn LLVMRustDestroyArchive(AR: &'static mut Archive);
 
     pub fn LLVMRustGetSectionName(SI: SectionIteratorRef, data: *mut *const c_char) -> size_t;