Skip to content

Commit 5cc72ac

Browse files
committed
Refactor the cli Source to have a new()
Makes the main a little less busy.
1 parent 111d772 commit 5cc72ac

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/bin/rav1e.rs

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ struct Source<D: Decoder> {
5858
}
5959

6060
impl<D: Decoder> Source<D> {
61+
cfg_if::cfg_if! {
62+
if #[cfg(all(unix, feature = "signal-hook"))] {
63+
fn new(limit: usize, input: D) -> Self {
64+
let exit_requested = {
65+
use std::sync::atomic::*;
66+
let e = Arc::new(AtomicBool::from(false));
67+
68+
fn setup_signal(sig: i32, e: Arc<AtomicBool>) {
69+
unsafe {
70+
signal_hook::register(sig, move || {
71+
if e.load(Ordering::SeqCst) {
72+
std::process::exit(128 + sig);
73+
}
74+
e.store(true, Ordering::SeqCst);
75+
info!("Exit requested, flushing.");
76+
})
77+
.expect("Cannot register the signal hooks");
78+
}
79+
}
80+
81+
setup_signal(signal_hook::SIGTERM, e.clone());
82+
setup_signal(signal_hook::SIGQUIT, e.clone());
83+
setup_signal(signal_hook::SIGINT, e.clone());
84+
85+
e
86+
};
87+
Self { limit, input, count: 0, exit_requested, }
88+
}
89+
} else {
90+
fn new(limit: usize, input: D) -> Self {
91+
Self { limit, input, count: 0, }
92+
}
93+
}
94+
}
95+
6196
fn read_frame<T: Pixel>(
6297
&mut self, ctx: &mut Context<T>, video_info: VideoDetails,
6398
) -> Result<(), CliError> {
@@ -446,36 +481,7 @@ fn run() -> Result<(), error::CliError> {
446481
};
447482
}
448483

449-
#[cfg(all(unix, feature = "signal-hook"))]
450-
let exit_requested = {
451-
use std::sync::atomic::*;
452-
let e = Arc::new(AtomicBool::from(false));
453-
454-
fn setup_signal(sig: i32, e: Arc<AtomicBool>) {
455-
unsafe {
456-
signal_hook::register(sig, move || {
457-
if e.load(Ordering::SeqCst) {
458-
std::process::exit(128 + sig);
459-
}
460-
e.store(true, Ordering::SeqCst);
461-
info!("Exit requested, flushing.");
462-
})
463-
.expect("Cannot register the signal hooks");
464-
}
465-
}
466-
467-
setup_signal(signal_hook::SIGTERM, e.clone());
468-
setup_signal(signal_hook::SIGQUIT, e.clone());
469-
setup_signal(signal_hook::SIGINT, e.clone());
470-
471-
e
472-
};
473-
474-
#[cfg(all(unix, feature = "signal-hook"))]
475-
let mut source =
476-
Source { limit: cli.limit, input: y4m_dec, count: 0, exit_requested };
477-
#[cfg(not(all(unix, feature = "signal-hook")))]
478-
let mut source = Source { limit: cli.limit, input: y4m_dec, count: 0 };
484+
let mut source = Source::new(cli.limit, y4m_dec);
479485

480486
if video_info.bit_depth == 8 {
481487
do_encode::<u8, y4m::Decoder<'_, Box<dyn Read>>>(

0 commit comments

Comments
 (0)