diff --git a/src/module.rs b/src/module.rs index 5a137e97e6a..14c79a5cffe 100644 --- a/src/module.rs +++ b/src/module.rs @@ -648,27 +648,30 @@ impl<'ctx> Module<'ctx> { unsafe { GlobalValue::new(value) } } - /// Writes a `Module` to a `Path`. + /// Writes a `Module` to a file. + /// + /// # Arguments + /// + /// * `path` - path to write the module's bitcode to. Must be valid Unicode. /// /// # Example /// /// ```no_run /// use inkwell::context::Context; /// - /// use std::path::Path; - /// - /// let mut path = Path::new("module.bc"); - /// /// let context = Context::create(); /// let module = context.create_module("my_module"); /// let void_type = context.void_type(); /// let fn_type = void_type.fn_type(&[], false); /// /// module.add_function("my_fn", fn_type, None); - /// module.write_bitcode_to_path(&path); + /// module.write_bitcode_to_path("module.bc"); /// ``` - pub fn write_bitcode_to_path(&self, path: &Path) -> bool { - let path_str = path.to_str().expect("Did not find a valid Unicode path string"); + pub fn write_bitcode_to_path(&self, path: impl AsRef) -> bool { + let path_str = path + .as_ref() + .to_str() + .expect("Did not find a valid Unicode path string"); let c_string = to_c_str(path_str); unsafe { LLVMWriteBitcodeToFile(self.module.get(), c_string.as_ptr()) == 0 } diff --git a/tests/all/test_module.rs b/tests/all/test_module.rs index 668dc6e6992..aa88002d47c 100644 --- a/tests/all/test_module.rs +++ b/tests/all/test_module.rs @@ -6,8 +6,7 @@ use inkwell::values::AnyValue; use inkwell::OptimizationLevel; use std::env::temp_dir; -use std::fs::{remove_file, File}; -use std::io::Read; +use std::fs::{self, remove_file}; use std::path::Path; #[test] @@ -24,11 +23,7 @@ fn test_write_bitcode_to_path() { module.add_function("my_fn", fn_type, None); module.write_bitcode_to_path(&path); - let mut contents = Vec::new(); - let mut file = File::open(&path).expect("Could not open temp file"); - - file.read_to_end(&mut contents).expect("Unable to verify written file"); - + let contents = fs::read(&path).expect("Could not read back written file."); assert!(!contents.is_empty()); remove_file(&path).unwrap();