5

Github Implement Extend and FromIterator for OsString by lopopolo · Pull Request...

 3 years ago
source link: https://github.com/rust-lang/rust/pull/82121
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Contributor

lopopolo commented 28 days ago

Add the following trait impls:

  • impl Extend<OsString> for OsString
  • impl<'a> Extend<&'a OsStr> for OsString
  • impl FromIterator<OsString> for OsString
  • impl<'a> FromIterator<&'a OsStr> for OsString

Because OsString is a platform string with no particular semantics, concatenating them together seems acceptable.

I came across a use case for these trait impls in artichoke/artichoke#1089:

Artichoke is a Ruby interpreter. Its CLI accepts multiple -e switches for executing inline Ruby code, like:

$ cargo -q run --bin artichoke -- -e '2.times {' -e 'puts "foo: #{__LINE__}"' -e '}'
foo: 2
foo: 2

I use clap for command line argument parsing, which collects these -e commands into a Vec<OsString>. To pass these commands to the interpreter for Eval, I need to join them together. Combining these impls with Iterator::intersperse #79524 would enable me to build a single bit of Ruby code.

Currently, I'm doing something like:

let mut commands = commands.into_iter();
let mut buf = if let Some(command) = commands.next() {
    command
} else {
    return Ok(Ok(()));
};
for command in commands {
    buf.push("\n");
    buf.push(command);
}

If there's interest, I'd also like to add impls for Cow<'a, OsStr>, which would avoid allocating the "\n" OsString in the concatenate + intersperse use case.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK