From 654e626e7cd61eec54ff599fee4bdbb7f3cc591b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Fri, 26 Jan 2024 12:53:49 +0000 Subject: [PATCH] Give File an infallible constructor. This is useful for data sources whose init() method doesn't actually need to do anything, because it allows the function constructing one to statically know that no error will be raised, and not have to write an ugly unwrap(). --- src/file.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/file.rs b/src/file.rs index 915529e..f29425b 100644 --- a/src/file.rs +++ b/src/file.rs @@ -454,7 +454,20 @@ impl File { show_new: bool, ) -> Result { source.init(client)?; + Ok(Self::new_infallible( + client, source, desc, file_desc, saved_pos, unfolded, show_new, + )) + } + fn new_infallible( + client: &mut Client, + source: Source, + desc: ColouredString, + file_desc: Type, + saved_pos: Option<&SavedFilePos>, + unfolded: Option>>>, + show_new: bool, + ) -> Self { let extender = if source.extendable() { Some(ExtendableIndicator::new()) } else { @@ -506,7 +519,7 @@ impl File { initial_pos = initial_pos.clip(contents.first_index(), contents.last_index()); - let ff = File { + File { contents, rendered: HashMap::new(), pos: initial_pos, @@ -520,8 +533,7 @@ impl File { search_direction: None, last_search: None, latest_read_index, - }; - Ok(ff) + } } fn ensure_item_rendered( -- 2.30.2