summaryrefslogtreecommitdiffstats
path: root/vendor/snap/examples
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/snap/examples
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/snap/examples')
-rw-r--r--vendor/snap/examples/compress-escaped.rs39
-rw-r--r--vendor/snap/examples/compress.rs13
-rw-r--r--vendor/snap/examples/decompress.rs13
3 files changed, 65 insertions, 0 deletions
diff --git a/vendor/snap/examples/compress-escaped.rs b/vendor/snap/examples/compress-escaped.rs
new file mode 100644
index 000000000..6fc97e9ac
--- /dev/null
+++ b/vendor/snap/examples/compress-escaped.rs
@@ -0,0 +1,39 @@
+use std::env;
+use std::io::{self, Write};
+use std::process;
+
+fn main() {
+ let bytes = match env::args().nth(1) {
+ None => {
+ writeln!(&mut io::stderr(), "Usage: compress-escaped string")
+ .unwrap();
+ process::exit(1);
+ }
+ Some(arg) => arg.into_bytes(),
+ };
+ let compressed = frame_press(&bytes);
+ println!("{}", escape(&compressed));
+ println!("{}", escape(&frame_depress(&compressed)));
+}
+
+fn frame_press(bytes: &[u8]) -> Vec<u8> {
+ use snap::write;
+
+ let mut wtr = write::FrameEncoder::new(vec![]);
+ wtr.write_all(bytes).unwrap();
+ wtr.into_inner().unwrap()
+}
+
+fn frame_depress(bytes: &[u8]) -> Vec<u8> {
+ use snap::read;
+ use std::io::Read;
+
+ let mut buf = vec![];
+ read::FrameDecoder::new(bytes).read_to_end(&mut buf).unwrap();
+ buf
+}
+
+fn escape(bytes: &[u8]) -> String {
+ use std::ascii::escape_default;
+ bytes.iter().flat_map(|&b| escape_default(b)).map(|b| b as char).collect()
+}
diff --git a/vendor/snap/examples/compress.rs b/vendor/snap/examples/compress.rs
new file mode 100644
index 000000000..31d302c61
--- /dev/null
+++ b/vendor/snap/examples/compress.rs
@@ -0,0 +1,13 @@
+use snap;
+
+use std::io;
+
+fn main() {
+ let stdin = io::stdin();
+ let stdout = io::stdout();
+
+ let mut rdr = stdin.lock();
+ // Wrap the stdout writer in a Snappy writer.
+ let mut wtr = snap::write::FrameEncoder::new(stdout.lock());
+ io::copy(&mut rdr, &mut wtr).expect("I/O operation failed");
+}
diff --git a/vendor/snap/examples/decompress.rs b/vendor/snap/examples/decompress.rs
new file mode 100644
index 000000000..7548f2f03
--- /dev/null
+++ b/vendor/snap/examples/decompress.rs
@@ -0,0 +1,13 @@
+use snap;
+
+use std::io;
+
+fn main() {
+ let stdin = io::stdin();
+ let stdout = io::stdout();
+
+ // Wrap the stdin reader in a Snappy reader.
+ let mut rdr = snap::read::FrameDecoder::new(stdin.lock());
+ let mut wtr = stdout.lock();
+ io::copy(&mut rdr, &mut wtr).expect("I/O operation failed");
+}