rustc_codegen_llvm/llvm/
archive_ro.rs
1use std::path::Path;
4use std::{slice, str};
5
6use rustc_fs_util::path_to_c_string;
7
8pub struct ArchiveRO {
9 pub raw: &'static mut super::Archive,
10}
11
12unsafe impl Send for ArchiveRO {}
13
14pub struct Iter<'a> {
15 raw: &'a mut super::ArchiveIterator<'a>,
16}
17
18pub struct Child<'a> {
19 pub raw: &'a mut super::ArchiveChild<'a>,
20}
21
22impl ArchiveRO {
23 pub(crate) fn open(dst: &Path) -> Result<ArchiveRO, String> {
30 unsafe {
31 let s = path_to_c_string(dst);
32 let ar = super::LLVMRustOpenArchive(s.as_ptr()).ok_or_else(|| {
33 super::last_error().unwrap_or_else(|| "failed to open archive".to_owned())
34 })?;
35 Ok(ArchiveRO { raw: ar })
36 }
37 }
38
39 pub(crate) fn iter(&self) -> Iter<'_> {
40 unsafe { Iter { raw: super::LLVMRustArchiveIteratorNew(self.raw) } }
41 }
42}
43
44impl Drop for ArchiveRO {
45 fn drop(&mut self) {
46 unsafe {
47 super::LLVMRustDestroyArchive(&mut *(self.raw as *mut _));
48 }
49 }
50}
51
52impl<'a> Iterator for Iter<'a> {
53 type Item = Result<Child<'a>, String>;
54
55 fn next(&mut self) -> Option<Result<Child<'a>, String>> {
56 unsafe {
57 match super::LLVMRustArchiveIteratorNext(self.raw) {
58 Some(raw) => Some(Ok(Child { raw })),
59 None => super::last_error().map(Err),
60 }
61 }
62 }
63}
64
65impl<'a> Drop for Iter<'a> {
66 fn drop(&mut self) {
67 unsafe {
68 super::LLVMRustArchiveIteratorFree(&mut *(self.raw as *mut _));
69 }
70 }
71}
72
73impl<'a> Child<'a> {
74 pub(crate) fn name(&self) -> Option<&'a str> {
75 unsafe {
76 let mut name_len = 0;
77 let name_ptr = super::LLVMRustArchiveChildName(self.raw, &mut name_len);
78 if name_ptr.is_null() {
79 None
80 } else {
81 let name = slice::from_raw_parts(name_ptr as *const u8, name_len as usize);
82 str::from_utf8(name).ok().map(|s| s.trim())
83 }
84 }
85 }
86}
87
88impl<'a> Drop for Child<'a> {
89 fn drop(&mut self) {
90 unsafe {
91 super::LLVMRustArchiveChildFree(&mut *(self.raw as *mut _));
92 }
93 }
94}