rust-lang / rust-lang/rfcs

Common Truncate trait

Open
#1,376 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

I'd like to be able to generalise File using io::Read, io::Write, and io::Seek instead, but there's no common interface for truncating such an object. For example, it's useful to test file reads/writes on a Vec, but I can't ever reduce the size of the object if I delete data. This function is called set_len for files and truncate for Vec.

My best option right now is to make a Trait like so:

trait Truncate {
  fn truncate_len(&mut self, len: usize) -> io::Result<()>;
}
impl Truncate for File {
  fn truncate_len(&mut self, len: usize) -> io::Result<()> { self.set_len(len) }
}
impl Truncate for Vec<u8> {
  fn truncate_len(&mut self, len: usize) -> io::Result<()> { Ok(self.resize(len)) }
}

It'd be great to have something similar be part of std::io with support for more structs.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the issue's proposed Truncate trait and the existing File::set_len and Vec::truncate APIs. Research how a common std::io abstraction could cover File, Vec, and other structs, then define the API, supported implementations, and acceptance criteria for a completed design.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend, operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.