Skip to content

Commit 4f825ff

Browse files
authored
Merge pull request #1 from geky/master
Replaced mem::uninitialized with mem::MaybeUninit
2 parents 754fdf8 + 193bb51 commit 4f825ff

File tree

6 files changed

+41
-39
lines changed

6 files changed

+41
-39
lines changed

Cargo.lock

+5-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/buffer.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ native_ref!(&MemoryBuffer = LLVMMemoryBufferRef);
1313
impl MemoryBuffer {
1414
pub fn new_from_file(path: &str) -> Result<CBox<MemoryBuffer>, CBox<str>> {
1515
util::with_cstr(path, |path| unsafe {
16-
let mut output = mem::uninitialized();
17-
let mut error = mem::uninitialized();
18-
if core::LLVMCreateMemoryBufferWithContentsOfFile(path, &mut output, &mut error) == 1 {
19-
Err(CBox::new(error))
16+
let mut output = mem::MaybeUninit::uninit();
17+
let mut error = mem::MaybeUninit::uninit();
18+
if core::LLVMCreateMemoryBufferWithContentsOfFile(path, output.as_mut_ptr(), error.as_mut_ptr()) == 1 {
19+
Err(CBox::new(error.assume_init()))
2020
} else {
21-
Ok(CBox::new(output))
21+
Ok(CBox::new(output.assume_init()))
2222
}
2323
})
2424
}

src/engine.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ pub trait ExecutionEngine<'a>:'a + Sized + DisposeRef where LLVMExecutionEngineR
3030
/// Remove a module from the list of modules to interpret or compile.
3131
fn remove_module(&'a self, module: &'a Module) -> &'a Module {
3232
unsafe {
33-
let mut out = mem::uninitialized();
34-
engine::LLVMRemoveModule(self.into(), module.into(), &mut out, ptr::null_mut());
35-
out.into()
33+
let mut out = mem::MaybeUninit::uninit();
34+
engine::LLVMRemoveModule(self.into(), module.into(), out.as_mut_ptr(), ptr::null_mut());
35+
out.assume_init().into()
3636
}
3737
}
3838
/// Execute all of the static constructors for this program.
@@ -134,7 +134,7 @@ impl<'a> ExecutionEngine<'a> for JitEngine {
134134
type Options = JitOptions;
135135
fn new(module: &'a Module, options: JitOptions) -> Result<CSemiBox<'a, JitEngine>, CBox<str>> {
136136
unsafe {
137-
let mut ee = mem::uninitialized();
137+
let mut ee = mem::MaybeUninit::uninit();
138138
let mut out = mem::zeroed();
139139
engine::LLVMLinkInMCJIT();
140140
if target::LLVM_InitializeNativeTarget() == 1 {
@@ -151,9 +151,9 @@ impl<'a> ExecutionEngine<'a> for JitEngine {
151151
MCJMM: ptr::null_mut()
152152
};
153153
let size = mem::size_of::<LLVMMCJITCompilerOptions>();
154-
let result = engine::LLVMCreateMCJITCompilerForModule(&mut ee, (&*module).into(), &mut options, size, &mut out);
154+
let result = engine::LLVMCreateMCJITCompilerForModule(ee.as_mut_ptr(), (&*module).into(), &mut options, size, &mut out);
155155
if result == 0 {
156-
Ok(ee.into())
156+
Ok(ee.assume_init().into())
157157
} else {
158158
Err(CBox::new(out))
159159
}
@@ -168,12 +168,12 @@ impl<'a> ExecutionEngine<'a> for Interpreter {
168168
type Options = ();
169169
fn new(module: &'a Module, _: ()) -> Result<CSemiBox<'a, Interpreter>, CBox<str>> {
170170
unsafe {
171-
let mut ee = mem::uninitialized();
171+
let mut ee = mem::MaybeUninit::uninit();
172172
let mut out = mem::zeroed();
173173
engine::LLVMLinkInInterpreter();
174-
let result = engine::LLVMCreateInterpreterForModule(&mut ee, (&*module).into(), &mut out);
174+
let result = engine::LLVMCreateInterpreterForModule(ee.as_mut_ptr(), (&*module).into(), &mut out);
175175
if result == 0 {
176-
Ok(ee.into())
176+
Ok(ee.assume_init().into())
177177
} else {
178178
Err(CBox::new(out))
179179
}

src/module.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ impl Module {
8282
/// Parse this bitcode file into a module, or return an error string.
8383
pub fn parse_bitcode<'a>(context: &'a Context, path: &str) -> Result<CSemiBox<'a, Module>, CBox<str>> {
8484
unsafe {
85-
let mut out = mem::uninitialized();
86-
let mut err = mem::uninitialized();
87-
let buf = try!(MemoryBuffer::new_from_file(path));
88-
if reader::LLVMParseBitcodeInContext(context.into(), buf.as_ptr(), &mut out, &mut err) == 1 {
89-
Err(CBox::new(err))
85+
let mut out = mem::MaybeUninit::uninit();
86+
let mut err = mem::MaybeUninit::uninit();
87+
let buf = MemoryBuffer::new_from_file(path)?;
88+
if reader::LLVMParseBitcodeInContext(context.into(), buf.as_ptr(), out.as_mut_ptr(), err.as_mut_ptr()) == 1 {
89+
Err(CBox::new(err.assume_init()))
9090
} else {
91-
Ok(CSemiBox::new(out))
91+
Ok(CSemiBox::new(out.assume_init()))
9292
}
9393
}
9494
}
@@ -161,10 +161,10 @@ impl Module {
161161
/// when an error occurs.
162162
pub fn verify(&self) -> Result<(), CBox<str>> {
163163
unsafe {
164-
let mut error = mem::uninitialized();
164+
let mut error = mem::MaybeUninit::uninit();
165165
let action = LLVMVerifierFailureAction::LLVMReturnStatusAction;
166-
if analysis::LLVMVerifyModule(self.into(), action, &mut error) == 1 {
167-
Err(CBox::new(error))
166+
if analysis::LLVMVerifyModule(self.into(), action, error.as_mut_ptr()) == 1 {
167+
Err(CBox::new(error.assume_init()))
168168
} else {
169169
Ok(())
170170
}
@@ -180,7 +180,7 @@ impl Module {
180180
let path = path.to_str().unwrap();
181181
let mod_path = dir.join("module.bc");
182182
let mod_path = mod_path.to_str().unwrap();
183-
try!(self.write_bitcode(mod_path));
183+
self.write_bitcode(mod_path)?;
184184
Command::new("llc")
185185
.arg(&format!("-O={}", opt_level))
186186
.arg("-filetype=obj")
@@ -197,9 +197,9 @@ impl Module {
197197
unsafe {
198198
let dest = self.into();
199199
let src = src.into();
200-
let mut message = mem::uninitialized();
201-
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerPreserveSource, &mut message) == 1 {
202-
Err(CBox::new(message))
200+
let mut message = mem::MaybeUninit::uninit();
201+
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerPreserveSource, message.as_mut_ptr()) == 1 {
202+
Err(CBox::new(message.assume_init()))
203203
} else {
204204
Ok(())
205205
}
@@ -213,9 +213,9 @@ impl Module {
213213
unsafe {
214214
let dest = self.into();
215215
let src = src.as_ptr();
216-
let mut message = mem::uninitialized();
217-
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerDestroySource, &mut message) == 1 {
218-
Err(CBox::new(message))
216+
let mut message = mem::MaybeUninit::uninit();
217+
if linker::LLVMLinkModules(dest, src, linker::LLVMLinkerMode::LLVMLinkerDestroySource, message.as_mut_ptr()) == 1 {
218+
Err(CBox::new(message.assume_init()))
219219
} else {
220220
Ok(())
221221
}

src/object.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ native_ref!(ObjectFile, obj: LLVMObjectFileRef);
1616
impl ObjectFile {
1717
/// Parse the object file at the path given, or return an error string if an error occurs.
1818
pub fn read(path: &str) -> Result<ObjectFile, CBox<str>> {
19-
let buf = try!(MemoryBuffer::new_from_file(path));
19+
let buf = MemoryBuffer::new_from_file(path)?;
2020
unsafe {
2121
let ptr = object::LLVMCreateObjectFile(buf.as_ptr());
2222
if ptr.is_null() {

src/types.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ impl StructType {
108108
pub fn get_elements(&self) -> Vec<&Type> {
109109
unsafe {
110110
let size = core::LLVMCountStructElementTypes(self.into());
111-
let mut els:Vec<_> = (0..size).map(|_| mem::uninitialized()).collect();
111+
let mut els:Vec<_> = (0..size).map(|_| mem::MaybeUninit::<&Type>::uninit()).collect();
112112
core::LLVMGetStructElementTypes(self.into(), els.as_mut_ptr() as *mut LLVMTypeRef);
113-
els
113+
els.into_iter().map(|el| el.assume_init()).collect()
114114
}
115115
}
116116
}
@@ -145,9 +145,9 @@ impl FunctionType {
145145
pub fn get_params(&self) -> Vec<&Type> {
146146
unsafe {
147147
let count = core::LLVMCountParamTypes(self.into());
148-
let mut types:Vec<_> = (0..count).map(|_| mem::uninitialized()).collect();
148+
let mut types:Vec<_> = (0..count).map(|_| mem::MaybeUninit::<&Type>::uninit()).collect();
149149
core::LLVMGetParamTypes(self.into(), types.as_mut_ptr() as *mut LLVMTypeRef);
150-
types
150+
types.into_iter().map(|type_| type_.assume_init()).collect()
151151
}
152152
}
153153
/// Returns the type that this function returns.
@@ -229,4 +229,4 @@ impl ArrayType {
229229
pub fn get_length(&self) -> usize {
230230
unsafe { core::LLVMGetArrayLength(self.into()) as usize }
231231
}
232-
}
232+
}

0 commit comments

Comments
 (0)