MCPcopy Create free account
hub / github.com/RetypeOS/parcode / open

Method open

src/reader.rs:615–642  ·  view source on GitHub ↗
(path: P)

Source from the content-addressed store, hash-verified

613 /// Returns error if file doesn't exist, lacks permissions, or is invalid.
614 #[cfg(not(target_arch = "wasm32"))]
615 pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
616 let path = path.as_ref();
617
618 // Memory mapping
619 #[cfg(feature = "mmap")]
620 {
621 let file = File::open(path)?;
622 #[allow(unsafe_code)]
623 let mmap = unsafe { Mmap::map(&file)? };
624
625 Self::init(DataSource::Mmap(Arc::new(mmap)), file.metadata()?.len())
626 }
627
628 // Read from RAM
629 #[cfg(not(feature = "mmap"))]
630 {
631 use std::io::Read;
632
633 let mut file = File::open(path)?;
634 let file_size = file.metadata()?.len();
635 let mut buffer =
636 Vec::with_capacity(usize::try_from(file_size).expect("File size too large"));
637
638 file.read_to_end(&mut buffer)?;
639
640 Self::init(DataSource::Memory(Arc::new(buffer)), file_size)
641 }
642 }
643
644 /// Opens a Parcode file from an in-memory buffer.
645 ///

Callers

nothing calls this directly

Calls 1

lenMethod · 0.45

Tested by

no test coverage detected