Skip to content

Commit 71634f5

Browse files
committed
second
1 parent 406f2a7 commit 71634f5

File tree

46 files changed

+247
-432
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+247
-432
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ sha2 = "0.10"
5353
tar = "0.4"
5454
tempfile = "3.14"
5555
thiserror = "2"
56+
anyhow = "1.0"
5657
threadpool = "1.8"
5758
toml = "0.8"
5859
tokio = { version = "1", features = ["full"] }

src/algorithms/randomness/rand-passwd.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ fn main() {
2222
```
2323

2424
[`Alphanumeric`]: https://docs.rs/rand/*/rand/distributions/struct.Alphanumeric.html
25+
26+
27+
28+

src/compression/tar/tar-compress.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ under `backup/logs` path with [`Builder::append_dir_all`].
1111
data prior to writing it into `archive.tar.gz`.
1212

1313
```rust,edition2018,no_run
14+
extern crate flate2;
15+
extern crate tar;
1416
1517
use std::fs::File;
1618
use flate2::Compression;
@@ -29,6 +31,8 @@ fn main() -> Result<(), std::io::Error> {
2931
To add the contents without renaming them, an empty string can be used as the first argument of [`Builder::append_dir_all`]:
3032

3133
```rust,edition2018,no_run
34+
extern crate flate2;
35+
extern crate tar;
3236
3337
use std::fs::File;
3438
use flate2::Compression;

src/compression/tar/tar-strip-prefix.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ the specified path prefix (`bundle/logs`). Finally, extract the [`tar::Entry`]
77
via [`Entry::unpack`].
88

99
```rust,edition2018,no_run
10-
# use error_chain::error_chain;
10+
extern crate anyhow;
11+
extern crate flate2;
12+
extern crate tar;
13+
use anyhow::Result;
1114
use std::fs::File;
1215
use std::path::PathBuf;
1316
use flate2::read::GzDecoder;
1417
use tar::Archive;
15-
#
16-
# error_chain! {
17-
# foreign_links {
18-
# Io(std::io::Error);
19-
# StripPrefixError(::std::path::StripPrefixError);
20-
# }
21-
# }
2218
23-
fn main() -> Result<(), std::io::Error> {
19+
fn main() -> Result<()> {
2420
let file = File::open("archive.tar.gz")?;
2521
let mut archive = Archive::new(GzDecoder::new(file));
2622
let prefix = "bundle/logs";

src/concurrency/parallel/rayon-thumbnails.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,25 @@ images in parallel using [`par_iter`] calling [`DynamicImage::resize`].
1010

1111
```rust,edition2018,no_run
1212
extern crate rayon;
13+
extern crate image;
14+
extern crate glob;
15+
extern crate anyhow;
1316
use rayon::prelude::*;
14-
15-
# use error_chain::error_chain;
17+
use anyhow::{Result, anyhow, Context};
1618
1719
use std::path::Path;
1820
use std::fs::create_dir_all;
19-
20-
# use error_chain::ChainedError;
2121
use glob::{glob_with, MatchOptions};
2222
use image::{FilterType, ImageError};
2323
24-
# error_chain! {
25-
# foreign_links {
26-
# Image(ImageError);
27-
# Io(std::io::Error);
28-
# Glob(glob::PatternError);
29-
# }
30-
# }
31-
3224
fn main() -> Result<()> {
3325
let options: MatchOptions = Default::default();
3426
let files: Vec<_> = glob_with("*.jpg", options)?
3527
.filter_map(|x| x.ok())
3628
.collect();
3729
3830
if files.len() == 0 {
39-
error_chain::bail!("No .jpg files found in current directory");
31+
return Err(anyhow!("No .jpg files found in current directory"));
4032
}
4133
4234
let thumb_dir = "thumbnails";
@@ -48,12 +40,12 @@ fn main() -> Result<()> {
4840
.par_iter()
4941
.map(|path| {
5042
make_thumbnail(path, thumb_dir, 300)
51-
.map_err(|e| e.chain_err(|| path.display().to_string()))
43+
.with_context(|| format!("Failed to process {}", path.display()))
5244
})
5345
.filter_map(|x| x.err())
5446
.collect();
5547
56-
image_failures.iter().for_each(|x| println!("{}", x.display_chain()));
48+
image_failures.iter().for_each(|x| println!("{}", x));
5749
5850
println!("{} thumbnails saved successfully", files.len() - image_failures.len());
5951
Ok(())

src/concurrency/thread/global-mut-state.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ race conditions. A [`MutexGuard`] must be acquired to read or mutate the
1010
value stored in a [`Mutex`].
1111

1212
```rust,edition2018
13-
# use error_chain::error_chain;
13+
extern crate anyhow;
14+
extern crate lazy_static;
15+
use anyhow::{Result, anyhow};
1416
use lazy_static::lazy_static;
1517
use std::sync::Mutex;
16-
#
17-
# error_chain!{ }
1818
1919
lazy_static! {
2020
static ref FRUIT: Mutex<Vec<String>> = Mutex::new(Vec::new());
2121
}
2222
2323
fn insert(fruit: &str) -> Result<()> {
24-
let mut db = FRUIT.lock().map_err(|_| "Failed to acquire MutexGuard")?;
24+
let mut db = FRUIT.lock().map_err(|_| anyhow!("Failed to acquire MutexGuard"))?;
2525
db.push(fruit.to_string());
2626
Ok(())
2727
}
@@ -31,7 +31,7 @@ fn main() -> Result<()> {
3131
insert("orange")?;
3232
insert("peach")?;
3333
{
34-
let db = FRUIT.lock().map_err(|_| "Failed to acquire MutexGuard")?;
34+
let db = FRUIT.lock().map_err(|_| anyhow!("Failed to acquire MutexGuard"))?;
3535
3636
db.iter().enumerate().for_each(|(i, item)| println!("{}: {}", i, item));
3737
}

src/concurrency/thread/threadpool-fractal.md

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -19,71 +19,66 @@ Create [`ThreadPool`] with thread count equal to number of cores with [`num_cpus
1919
```rust,edition2018,no_run
2020
extern crate num;
2121
extern crate num_cpus;
22-
# use error_chain::error_chain;
22+
extern crate anyhow;
23+
extern crate threadpool;
24+
extern crate image;
25+
use anyhow::Result;
2326
use std::sync::mpsc::{channel, RecvError};
2427
use threadpool::ThreadPool;
2528
use num::complex::Complex;
2629
use image::{ImageBuffer, Pixel, Rgb};
27-
#
28-
# error_chain! {
29-
# foreign_links {
30-
# MpscRecv(RecvError);
31-
# Io(std::io::Error);
32-
# Image(image::ImageError);
33-
# }
34-
# }
35-
#
36-
# // Function converting intensity values to RGB
37-
# // Based on http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
38-
# fn wavelength_to_rgb(wavelength: u32) -> Rgb<u8> {
39-
# let wave = wavelength as f32;
40-
#
41-
# let (r, g, b) = match wavelength {
42-
# 380..=439 => ((440. - wave) / (440. - 380.), 0.0, 1.0),
43-
# 440..=489 => (0.0, (wave - 440.) / (490. - 440.), 1.0),
44-
# 490..=509 => (0.0, 1.0, (510. - wave) / (510. - 490.)),
45-
# 510..=579 => ((wave - 510.) / (580. - 510.), 1.0, 0.0),
46-
# 580..=644 => (1.0, (645. - wave) / (645. - 580.), 0.0),
47-
# 645..=780 => (1.0, 0.0, 0.0),
48-
# _ => (0.0, 0.0, 0.0),
49-
# };
50-
#
51-
# let factor = match wavelength {
52-
# 380..=419 => 0.3 + 0.7 * (wave - 380.) / (420. - 380.),
53-
# 701..=780 => 0.3 + 0.7 * (780. - wave) / (780. - 700.),
54-
# _ => 1.0,
55-
# };
56-
#
57-
# let (r, g, b) = (normalize(r, factor), normalize(g, factor), normalize(b, factor));
58-
# Rgb::from_channels(r, g, b, 0)
59-
# }
60-
#
61-
# // Maps Julia set distance estimation to intensity values
62-
# fn julia(c: Complex<f32>, x: u32, y: u32, width: u32, height: u32, max_iter: u32) -> u32 {
63-
# let width = width as f32;
64-
# let height = height as f32;
65-
#
66-
# let mut z = Complex {
67-
# // scale and translate the point to image coordinates
68-
# re: 3.0 * (x as f32 - 0.5 * width) / width,
69-
# im: 2.0 * (y as f32 - 0.5 * height) / height,
70-
# };
71-
#
72-
# let mut i = 0;
73-
# for t in 0..max_iter {
74-
# if z.norm() >= 2.0 {
75-
# break;
76-
# }
77-
# z = z * z + c;
78-
# i = t;
79-
# }
80-
# i
81-
# }
82-
#
83-
# // Normalizes color intensity values within RGB range
84-
# fn normalize(color: f32, factor: f32) -> u8 {
85-
# ((color * factor).powf(0.8) * 255.) as u8
86-
# }
30+
31+
// Function converting intensity values to RGB
32+
// Based on http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm
33+
fn wavelength_to_rgb(wavelength: u32) -> Rgb<u8> {
34+
let wave = wavelength as f32;
35+
36+
let (r, g, b) = match wavelength {
37+
380..=439 => ((440. - wave) / (440. - 380.), 0.0, 1.0),
38+
440..=489 => (0.0, (wave - 440.) / (490. - 440.), 1.0),
39+
490..=509 => (0.0, 1.0, (510. - wave) / (510. - 490.)),
40+
510..=579 => ((wave - 510.) / (580. - 510.), 1.0, 0.0),
41+
580..=644 => (1.0, (645. - wave) / (645. - 580.), 0.0),
42+
645..=780 => (1.0, 0.0, 0.0),
43+
_ => (0.0, 0.0, 0.0),
44+
};
45+
46+
let factor = match wavelength {
47+
380..=419 => 0.3 + 0.7 * (wave - 380.) / (420. - 380.),
48+
701..=780 => 0.3 + 0.7 * (780. - wave) / (780. - 700.),
49+
_ => 1.0,
50+
};
51+
52+
let (r, g, b) = (normalize(r, factor), normalize(g, factor), normalize(b, factor));
53+
Rgb::from_channels(r, g, b, 0)
54+
}
55+
56+
// Maps Julia set distance estimation to intensity values
57+
fn julia(c: Complex<f32>, x: u32, y: u32, width: u32, height: u32, max_iter: u32) -> u32 {
58+
let width = width as f32;
59+
let height = height as f32;
60+
61+
let mut z = Complex {
62+
// scale and translate the point to image coordinates
63+
re: 3.0 * (x as f32 - 0.5 * width) / width,
64+
im: 2.0 * (y as f32 - 0.5 * height) / height,
65+
};
66+
67+
let mut i = 0;
68+
for t in 0..max_iter {
69+
if z.norm() >= 2.0 {
70+
break;
71+
}
72+
z = z * z + c;
73+
i = t;
74+
}
75+
i
76+
}
77+
78+
// Normalizes color intensity values within RGB range
79+
fn normalize(color: f32, factor: f32) -> u8 {
80+
((color * factor).powf(0.8) * 255.) as u8
81+
}
8782
8883
fn main() -> Result<()> {
8984
let (width, height) = (1920, 1080);

src/concurrency/thread/threadpool-walk.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and computing SHA256 hash.
1111
```rust,edition2018,no_run
1212
extern crate num_cpus;
1313
extern crate ring;
14+
extern crate threadpool;
15+
extern crate walkdir;
1416
use walkdir::WalkDir;
1517
use std::fs::File;
1618
use std::io::{BufReader, Read, Error};

src/cryptography/hashing/sha-digest.md

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,12 @@ the file's contents using [`digest::Context`].
88
```rust,edition2018
99
extern crate ring;
1010
extern crate data_encoding;
11-
use error_chain::error_chain;
11+
extern crate anyhow;
12+
use anyhow::Result;
1213
use ring::digest::{Context, Digest, SHA256};
1314
use data_encoding::HEXUPPER;
1415
use std::fs::File;
1516
use std::io::{BufReader, Read, Write};
16-
#
17-
# error_chain! {
18-
# foreign_links {
19-
# Io(std::io::Error);
20-
# Decode(data_encoding::DecodeError);
21-
# }
22-
# }
2317
2418
fn sha256_digest<R: Read>(mut reader: R) -> Result<Digest> {
2519
let mut context = Context::new(&SHA256);

src/database/postgres/aggregate_data.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
This recipe lists the nationalities of the first 7999 artists in the database of the [`Museum of Modern Art`] in descending order.
66

77
```rust,edition2018,no_run
8+
extern crate postgres;
89
use postgres::{Client, Error, NoTls};
910
1011
struct Nation {

0 commit comments

Comments
 (0)