chiark / gitweb /
Give File an infallible constructor.
authorSimon Tatham <anakin@pobox.com>
Fri, 26 Jan 2024 12:53:49 +0000 (12:53 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 27 Jan 2024 09:08:30 +0000 (09:08 +0000)
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

index 915529eac311f9c70c06c8978383a461abaffac4..f29425b091b7787b0b889e9c63cb467600394ae7 100644 (file)
@@ -454,7 +454,20 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
         show_new: bool,
     ) -> Result<Self, ClientError> {
         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<Rc<RefCell<HashSet<String>>>>,
+        show_new: bool,
+    ) -> Self {
         let extender = if source.extendable() {
             Some(ExtendableIndicator::new())
         } else {
@@ -506,7 +519,7 @@ impl<Type: FileType, Source: FileDataSource> File<Type, Source> {
         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<Type: FileType, Source: FileDataSource> File<Type, Source> {
             search_direction: None,
             last_search: None,
             latest_read_index,
-        };
-        Ok(ff)
+        }
     }
 
     fn ensure_item_rendered(