From 2d9db49fefd143ae266ddeede838e28b223d3438 Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Wed, 3 Jan 2024 12:51:20 +0100 Subject: [PATCH] Remove borrow in borrow --- Cargo.lock | 11 +++++++++++ Cargo.toml | 1 + src/console/types.rs | 28 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2919eee..223b3cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console_error_panic_hook" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06aeb73f470f66dcdbf7223caeebb85984942f22f1adb2a088cf9668146bbbc" +dependencies = [ + "cfg-if", + "wasm-bindgen", +] + [[package]] name = "log" version = "0.4.20" @@ -119,5 +129,6 @@ checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f" name = "wasm-terminal" version = "0.1.0" dependencies = [ + "console_error_panic_hook", "wasm-bindgen", ] diff --git a/Cargo.toml b/Cargo.toml index 6ac004a..836e19a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ repository = "https://git.c0ntroller.de/c0ntroller/wasm-terminal" crate-type = ["cdylib"] [dependencies] +console_error_panic_hook = "0.1.7" wasm-bindgen = "0.2.89" diff --git a/src/console/types.rs b/src/console/types.rs index 3a69c30..09e5b8b 100644 --- a/src/console/types.rs +++ b/src/console/types.rs @@ -182,27 +182,27 @@ impl Directory { }*/ pub fn remove_file(&self, name: String) -> Result<(), &'static str> { - let mut index = 0; - for file in self.files.borrow().iter() { - if file.borrow().get_name() == name { + let index = self.files.borrow().iter().position(|file| file.borrow().get_name() == name); + + match index { + Some(index) => { self.files.borrow_mut().remove(index); - return Ok(()); - } - index += 1; + Ok(()) + }, + None => Err("File not found"), } - Err("File not found") } pub fn remove_dir(&self, name: String) -> Result<(), &'static str> { - let mut index = 0; - for dir in self.subdirs.borrow().iter() { - if dir.borrow().get_name() == name { + let index: Option = self.subdirs.borrow().iter().position(|dir: &Rc>| dir.borrow().get_name() == name); + + match index { + Some(index) => { self.subdirs.borrow_mut().remove(index); - return Ok(()); - } - index += 1; + Ok(()) + }, + None => Err("Directory not found"), } - Err("Directory not found") } pub fn rename(&mut self, name: String) {