From 4028abc9d147e5b0a16acedc7047545fc2213c3f Mon Sep 17 00:00:00 2001 From: Etienne Wodey <44871469+airwoodix@users.noreply.github.com> Date: Thu, 22 Aug 2024 18:51:56 +0200 Subject: [PATCH] module: make write_bitcode_to_path accept more general path representations (#528) Co-authored-by: Dan Kolsoi --- src/module.rs | 19 +++++++++++-------- tests/all/test_module.rs | 9 ++------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/module.rs b/src/module.rs index 5a137e97e6..14c79a5cff 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 668dc6e699..aa88002d47 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();