Skip to main content

read_to_string_capped

Function read_to_string_capped 

Source
pub fn read_to_string_capped(
    path: &Path,
    max_bytes: u64,
) -> Result<Option<String>>
Expand description

Counted, size-bounded wrapper around std::fs::File::open + Read::take.

Reads at most max_bytes + 1 bytes and returns Ok(None) if that read produced more than max_bytes — the one extra byte is what distinguishes “exactly max_bytes long” from “longer than max_bytes” without reading the whole (potentially huge) file. Unlike a stat-then-read_to_string sequence, this bound is enforced by the read call itself, so it holds even if the file grows, or is swapped via a symlink, between a caller’s earlier stat and this call.

§Errors

Returns an error under the same conditions as std::fs::read_to_string — most commonly, path does not exist, is not accessible, or the bounded content is not valid UTF-8.