Skip to content

Commit 27e1a3c

Browse files
PunitLodhacanihavesomecoffee
authored andcommitted
Pass raw pointer to avoid mut ref to global variable warning
1 parent 0912ac8 commit 27e1a3c

File tree

2 files changed

+187
-181
lines changed

2 files changed

+187
-181
lines changed

src/rust/src/common.rs

Lines changed: 185 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -46,189 +46,195 @@ pub trait FromRust<T> {
4646
unsafe fn copy_from_rust(&mut self, options: T);
4747
}
4848

49-
impl FromRust<Options> for ccx_s_options {
50-
/// # Safety
51-
///
52-
/// This function is unsafe because it dereferences the pointer passed to it.
53-
unsafe fn copy_from_rust(self: &mut ccx_s_options, options: Options) {
54-
self.extract = options.extract as _;
55-
self.no_rollup = options.no_rollup as _;
56-
self.noscte20 = options.noscte20 as _;
57-
self.webvtt_create_css = options.webvtt_create_css as _;
58-
self.cc_channel = options.cc_channel as _;
59-
self.buffer_input = options.buffer_input as _;
60-
self.nofontcolor = options.nofontcolor as _;
61-
self.write_format = options.write_format.to_ctype();
62-
self.send_to_srv = options.send_to_srv as _;
63-
self.nohtmlescape = options.nohtmlescape as _;
64-
self.notypesetting = options.notypesetting as _;
65-
self.extraction_start = options.extraction_start.to_ctype();
66-
self.extraction_end = options.extraction_end.to_ctype();
67-
self.print_file_reports = options.print_file_reports as _;
68-
self.settings_608 = options.settings_608.to_ctype();
69-
self.settings_dtvcc = options.settings_dtvcc.to_ctype();
70-
self.is_608_enabled = options.is_608_enabled as _;
71-
self.is_708_enabled = options.is_708_enabled as _;
72-
self.millis_separator = options.millis_separator() as _;
73-
self.binary_concat = options.binary_concat as _;
74-
self.use_gop_as_pts = if let Some(usegops) = options.use_gop_as_pts {
75-
if usegops {
76-
1
77-
} else {
78-
-1
79-
}
80-
} else {
81-
0
82-
};
83-
self.fix_padding = options.fix_padding as _;
84-
self.gui_mode_reports = options.gui_mode_reports as _;
85-
self.no_progress_bar = options.no_progress_bar as _;
86-
87-
if options.sentence_cap_file.try_exists().unwrap_or_default() {
88-
self.sentence_cap_file = string_to_c_char(
89-
options
90-
.sentence_cap_file
91-
.clone()
92-
.to_str()
93-
.unwrap_or_default(),
94-
);
95-
}
96-
97-
self.live_stream = if let Some(live_stream) = options.live_stream {
98-
live_stream.seconds() as _
49+
/// Convert the rust struct (CcxOptions) to C struct (ccx_s_options), so that it can be used by the C code.
50+
///
51+
/// Using the FromRust trait here requires a &mut self on the global variable ccx_options. This leads to
52+
/// the warning: creating a mutable reference to mutable static is discouraged. So we instead pass a raw pointer
53+
///
54+
/// # Safety
55+
///
56+
/// This function is unsafe because we are modifying a global static mut variable
57+
/// and we are dereferencing the pointer passed to it.
58+
pub unsafe fn copy_from_rust(ccx_s_options: *mut ccx_s_options, options: Options) {
59+
(*ccx_s_options).extract = options.extract as _;
60+
(*ccx_s_options).no_rollup = options.no_rollup as _;
61+
(*ccx_s_options).noscte20 = options.noscte20 as _;
62+
(*ccx_s_options).webvtt_create_css = options.webvtt_create_css as _;
63+
(*ccx_s_options).cc_channel = options.cc_channel as _;
64+
(*ccx_s_options).buffer_input = options.buffer_input as _;
65+
(*ccx_s_options).nofontcolor = options.nofontcolor as _;
66+
(*ccx_s_options).write_format = options.write_format.to_ctype();
67+
(*ccx_s_options).send_to_srv = options.send_to_srv as _;
68+
(*ccx_s_options).nohtmlescape = options.nohtmlescape as _;
69+
(*ccx_s_options).notypesetting = options.notypesetting as _;
70+
(*ccx_s_options).extraction_start = options.extraction_start.to_ctype();
71+
(*ccx_s_options).extraction_end = options.extraction_end.to_ctype();
72+
(*ccx_s_options).print_file_reports = options.print_file_reports as _;
73+
(*ccx_s_options).settings_608 = options.settings_608.to_ctype();
74+
(*ccx_s_options).settings_dtvcc = options.settings_dtvcc.to_ctype();
75+
(*ccx_s_options).is_608_enabled = options.is_608_enabled as _;
76+
(*ccx_s_options).is_708_enabled = options.is_708_enabled as _;
77+
(*ccx_s_options).millis_separator = options.millis_separator() as _;
78+
(*ccx_s_options).binary_concat = options.binary_concat as _;
79+
(*ccx_s_options).use_gop_as_pts = if let Some(usegops) = options.use_gop_as_pts {
80+
if usegops {
81+
1
9982
} else {
10083
-1
101-
};
102-
if options
103-
.filter_profanity_file
104-
.try_exists()
105-
.unwrap_or_default()
106-
{
107-
self.filter_profanity_file = string_to_c_char(
108-
options
109-
.filter_profanity_file
110-
.clone()
111-
.to_str()
112-
.unwrap_or_default(),
113-
);
114-
}
115-
self.messages_target = options.messages_target as _;
116-
self.timestamp_map = options.timestamp_map as _;
117-
self.dolevdist = options.dolevdist.into();
118-
self.levdistmincnt = options.levdistmincnt as _;
119-
self.levdistmaxpct = options.levdistmaxpct as _;
120-
self.investigate_packets = options.investigate_packets as _;
121-
self.fullbin = options.fullbin as _;
122-
self.nosync = options.nosync as _;
123-
self.hauppauge_mode = options.hauppauge_mode as _;
124-
self.wtvconvertfix = options.wtvconvertfix as _;
125-
self.wtvmpeg2 = options.wtvmpeg2 as _;
126-
self.auto_myth = if let Some(auto_myth) = options.auto_myth {
127-
auto_myth as _
128-
} else {
129-
2
130-
};
131-
self.mp4vidtrack = options.mp4vidtrack as _;
132-
self.extract_chapters = options.extract_chapters as _;
133-
self.usepicorder = options.usepicorder as _;
134-
self.xmltv = options.xmltv as _;
135-
self.xmltvliveinterval = options.xmltvliveinterval.seconds() as _;
136-
self.xmltvoutputinterval = options.xmltvoutputinterval.seconds() as _;
137-
self.xmltvonlycurrent = options.xmltvonlycurrent.into();
138-
self.keep_output_closed = options.keep_output_closed as _;
139-
self.force_flush = options.force_flush as _;
140-
self.append_mode = options.append_mode as _;
141-
self.ucla = options.ucla as _;
142-
self.tickertext = options.tickertext as _;
143-
self.hardsubx = options.hardsubx as _;
144-
self.hardsubx_and_common = options.hardsubx_and_common as _;
145-
if let Some(dvblang) = options.dvblang {
146-
self.dvblang = string_to_c_char(dvblang.to_ctype().as_str());
147-
}
148-
if options.ocrlang.try_exists().unwrap_or_default() {
149-
self.ocrlang = string_to_c_char(options.ocrlang.to_str().unwrap());
150-
}
151-
self.ocr_oem = options.ocr_oem as _;
152-
self.ocr_quantmode = options.ocr_quantmode as _;
153-
if let Some(mkvlang) = options.mkvlang {
154-
self.mkvlang = string_to_c_char(mkvlang.to_ctype().as_str());
155-
}
156-
self.analyze_video_stream = options.analyze_video_stream as _;
157-
self.hardsubx_ocr_mode = options.hardsubx_ocr_mode.to_ctype();
158-
self.hardsubx_subcolor = options.hardsubx_hue.to_ctype();
159-
self.hardsubx_min_sub_duration = options.hardsubx_min_sub_duration.seconds() as _;
160-
self.hardsubx_detect_italics = options.hardsubx_detect_italics as _;
161-
self.hardsubx_conf_thresh = options.hardsubx_conf_thresh as _;
162-
self.hardsubx_hue = options.hardsubx_hue.get_hue() as _;
163-
self.hardsubx_lum_thresh = options.hardsubx_lum_thresh as _;
164-
self.transcript_settings = options.transcript_settings.to_ctype();
165-
self.date_format = options.date_format.to_ctype();
166-
self.write_format_rewritten = options.write_format_rewritten as _;
167-
self.use_ass_instead_of_ssa = options.use_ass_instead_of_ssa as _;
168-
self.use_webvtt_styling = options.use_webvtt_styling as _;
169-
self.debug_mask = options.debug_mask.normal_mask().bits() as _;
170-
self.debug_mask_on_debug = options.debug_mask.debug_mask().bits() as _;
171-
if options.udpsrc.is_some() {
172-
self.udpsrc = string_to_c_char(&options.udpsrc.clone().unwrap());
173-
}
174-
if options.udpaddr.is_some() {
175-
self.udpaddr = string_to_c_char(&options.udpaddr.clone().unwrap());
176-
}
177-
self.udpport = options.udpport as _;
178-
if options.tcpport.is_some() {
179-
self.tcpport = string_to_c_char(&options.tcpport.unwrap().to_string());
180-
}
181-
if options.tcp_password.is_some() {
182-
self.tcp_password = string_to_c_char(&options.tcp_password.clone().unwrap());
183-
}
184-
if options.tcp_desc.is_some() {
185-
self.tcp_desc = string_to_c_char(&options.tcp_desc.clone().unwrap());
186-
}
187-
if options.srv_addr.is_some() {
188-
self.srv_addr = string_to_c_char(&options.srv_addr.clone().unwrap());
189-
}
190-
if options.srv_port.is_some() {
191-
self.srv_port = string_to_c_char(&options.srv_port.unwrap().to_string());
192-
}
193-
self.noautotimeref = options.noautotimeref as _;
194-
self.input_source = options.input_source as _;
195-
if options.output_filename.is_some() {
196-
self.output_filename = string_to_c_char(&options.output_filename.clone().unwrap());
19784
}
198-
if options.inputfile.is_some() {
199-
self.inputfile = string_to_c_chars(options.inputfile.clone().unwrap());
200-
self.num_input_files = options.inputfile.iter().filter(|s| !s.is_empty()).count() as _;
201-
}
202-
self.demux_cfg = options.demux_cfg.to_ctype();
203-
self.enc_cfg = options.enc_cfg.to_ctype();
204-
self.subs_delay = options.subs_delay.millis();
205-
self.cc_to_stdout = options.cc_to_stdout as _;
206-
self.pes_header_to_stdout = options.pes_header_to_stdout as _;
207-
self.ignore_pts_jumps = options.ignore_pts_jumps as _;
208-
self.multiprogram = options.multiprogram as _;
209-
self.out_interval = options.out_interval;
210-
self.segment_on_key_frames_only = options.segment_on_key_frames_only as _;
211-
#[cfg(feature = "with_libcurl")]
212-
{
213-
if options.curlposturl.is_some() {
214-
self.curlposturl =
215-
string_to_c_char(&options.curlposturl.as_ref().unwrap_or_default().as_str());
216-
}
85+
} else {
86+
0
87+
};
88+
(*ccx_s_options).fix_padding = options.fix_padding as _;
89+
(*ccx_s_options).gui_mode_reports = options.gui_mode_reports as _;
90+
(*ccx_s_options).no_progress_bar = options.no_progress_bar as _;
91+
92+
if options.sentence_cap_file.try_exists().unwrap_or_default() {
93+
(*ccx_s_options).sentence_cap_file = string_to_c_char(
94+
options
95+
.sentence_cap_file
96+
.clone()
97+
.to_str()
98+
.unwrap_or_default(),
99+
);
100+
}
101+
102+
(*ccx_s_options).live_stream = if let Some(live_stream) = options.live_stream {
103+
live_stream.seconds() as _
104+
} else {
105+
-1
106+
};
107+
if options
108+
.filter_profanity_file
109+
.try_exists()
110+
.unwrap_or_default()
111+
{
112+
(*ccx_s_options).filter_profanity_file = string_to_c_char(
113+
options
114+
.filter_profanity_file
115+
.clone()
116+
.to_str()
117+
.unwrap_or_default(),
118+
);
119+
}
120+
(*ccx_s_options).messages_target = options.messages_target as _;
121+
(*ccx_s_options).timestamp_map = options.timestamp_map as _;
122+
(*ccx_s_options).dolevdist = options.dolevdist.into();
123+
(*ccx_s_options).levdistmincnt = options.levdistmincnt as _;
124+
(*ccx_s_options).levdistmaxpct = options.levdistmaxpct as _;
125+
(*ccx_s_options).investigate_packets = options.investigate_packets as _;
126+
(*ccx_s_options).fullbin = options.fullbin as _;
127+
(*ccx_s_options).nosync = options.nosync as _;
128+
(*ccx_s_options).hauppauge_mode = options.hauppauge_mode as _;
129+
(*ccx_s_options).wtvconvertfix = options.wtvconvertfix as _;
130+
(*ccx_s_options).wtvmpeg2 = options.wtvmpeg2 as _;
131+
(*ccx_s_options).auto_myth = if let Some(auto_myth) = options.auto_myth {
132+
auto_myth as _
133+
} else {
134+
2
135+
};
136+
(*ccx_s_options).mp4vidtrack = options.mp4vidtrack as _;
137+
(*ccx_s_options).extract_chapters = options.extract_chapters as _;
138+
(*ccx_s_options).usepicorder = options.usepicorder as _;
139+
(*ccx_s_options).xmltv = options.xmltv as _;
140+
(*ccx_s_options).xmltvliveinterval = options.xmltvliveinterval.seconds() as _;
141+
(*ccx_s_options).xmltvoutputinterval = options.xmltvoutputinterval.seconds() as _;
142+
(*ccx_s_options).xmltvonlycurrent = options.xmltvonlycurrent.into();
143+
(*ccx_s_options).keep_output_closed = options.keep_output_closed as _;
144+
(*ccx_s_options).force_flush = options.force_flush as _;
145+
(*ccx_s_options).append_mode = options.append_mode as _;
146+
(*ccx_s_options).ucla = options.ucla as _;
147+
(*ccx_s_options).tickertext = options.tickertext as _;
148+
(*ccx_s_options).hardsubx = options.hardsubx as _;
149+
(*ccx_s_options).hardsubx_and_common = options.hardsubx_and_common as _;
150+
if let Some(dvblang) = options.dvblang {
151+
(*ccx_s_options).dvblang = string_to_c_char(dvblang.to_ctype().as_str());
152+
}
153+
if options.ocrlang.try_exists().unwrap_or_default() {
154+
(*ccx_s_options).ocrlang = string_to_c_char(options.ocrlang.to_str().unwrap());
155+
}
156+
(*ccx_s_options).ocr_oem = options.ocr_oem as _;
157+
(*ccx_s_options).ocr_quantmode = options.ocr_quantmode as _;
158+
if let Some(mkvlang) = options.mkvlang {
159+
(*ccx_s_options).mkvlang = string_to_c_char(mkvlang.to_ctype().as_str());
160+
}
161+
(*ccx_s_options).analyze_video_stream = options.analyze_video_stream as _;
162+
(*ccx_s_options).hardsubx_ocr_mode = options.hardsubx_ocr_mode.to_ctype();
163+
(*ccx_s_options).hardsubx_subcolor = options.hardsubx_hue.to_ctype();
164+
(*ccx_s_options).hardsubx_min_sub_duration = options.hardsubx_min_sub_duration.seconds() as _;
165+
(*ccx_s_options).hardsubx_detect_italics = options.hardsubx_detect_italics as _;
166+
(*ccx_s_options).hardsubx_conf_thresh = options.hardsubx_conf_thresh as _;
167+
(*ccx_s_options).hardsubx_hue = options.hardsubx_hue.get_hue() as _;
168+
(*ccx_s_options).hardsubx_lum_thresh = options.hardsubx_lum_thresh as _;
169+
(*ccx_s_options).transcript_settings = options.transcript_settings.to_ctype();
170+
(*ccx_s_options).date_format = options.date_format.to_ctype();
171+
(*ccx_s_options).write_format_rewritten = options.write_format_rewritten as _;
172+
(*ccx_s_options).use_ass_instead_of_ssa = options.use_ass_instead_of_ssa as _;
173+
(*ccx_s_options).use_webvtt_styling = options.use_webvtt_styling as _;
174+
(*ccx_s_options).debug_mask = options.debug_mask.normal_mask().bits() as _;
175+
(*ccx_s_options).debug_mask_on_debug = options.debug_mask.debug_mask().bits() as _;
176+
if options.udpsrc.is_some() {
177+
(*ccx_s_options).udpsrc = string_to_c_char(&options.udpsrc.clone().unwrap());
178+
}
179+
if options.udpaddr.is_some() {
180+
(*ccx_s_options).udpaddr = string_to_c_char(&options.udpaddr.clone().unwrap());
181+
}
182+
(*ccx_s_options).udpport = options.udpport as _;
183+
if options.tcpport.is_some() {
184+
(*ccx_s_options).tcpport = string_to_c_char(&options.tcpport.unwrap().to_string());
185+
}
186+
if options.tcp_password.is_some() {
187+
(*ccx_s_options).tcp_password = string_to_c_char(&options.tcp_password.clone().unwrap());
188+
}
189+
if options.tcp_desc.is_some() {
190+
(*ccx_s_options).tcp_desc = string_to_c_char(&options.tcp_desc.clone().unwrap());
191+
}
192+
if options.srv_addr.is_some() {
193+
(*ccx_s_options).srv_addr = string_to_c_char(&options.srv_addr.clone().unwrap());
194+
}
195+
if options.srv_port.is_some() {
196+
(*ccx_s_options).srv_port = string_to_c_char(&options.srv_port.unwrap().to_string());
197+
}
198+
(*ccx_s_options).noautotimeref = options.noautotimeref as _;
199+
(*ccx_s_options).input_source = options.input_source as _;
200+
if options.output_filename.is_some() {
201+
(*ccx_s_options).output_filename =
202+
string_to_c_char(&options.output_filename.clone().unwrap());
203+
}
204+
if options.inputfile.is_some() {
205+
(*ccx_s_options).inputfile = string_to_c_chars(options.inputfile.clone().unwrap());
206+
(*ccx_s_options).num_input_files =
207+
options.inputfile.iter().filter(|s| !s.is_empty()).count() as _;
208+
}
209+
(*ccx_s_options).demux_cfg = options.demux_cfg.to_ctype();
210+
(*ccx_s_options).enc_cfg = options.enc_cfg.to_ctype();
211+
(*ccx_s_options).subs_delay = options.subs_delay.millis();
212+
(*ccx_s_options).cc_to_stdout = options.cc_to_stdout as _;
213+
(*ccx_s_options).pes_header_to_stdout = options.pes_header_to_stdout as _;
214+
(*ccx_s_options).ignore_pts_jumps = options.ignore_pts_jumps as _;
215+
(*ccx_s_options).multiprogram = options.multiprogram as _;
216+
(*ccx_s_options).out_interval = options.out_interval;
217+
(*ccx_s_options).segment_on_key_frames_only = options.segment_on_key_frames_only as _;
218+
#[cfg(feature = "with_libcurl")]
219+
{
220+
if options.curlposturl.is_some() {
221+
(*ccx_s_options).curlposturl =
222+
string_to_c_char(&options.curlposturl.as_ref().unwrap_or_default().as_str());
217223
}
218-
#[cfg(feature = "enable_sharing")]
219-
{
220-
self.sharing_enabled = options.sharing_enabled as _;
221-
if options.sharing_url.is_some() {
222-
self.sharing_url =
223-
string_to_c_char(&options.sharing_url.as_ref().unwrap().as_str());
224-
}
225-
self.translate_enabled = options.translate_enabled as _;
226-
if options.translate_langs.is_some() {
227-
self.translate_langs = string_to_c_char(&options.translate_langs.unwrap());
228-
}
229-
if options.translate_key.is_some() {
230-
self.translate_key = string_to_c_char(&options.translate_key.unwrap());
231-
}
224+
}
225+
#[cfg(feature = "enable_sharing")]
226+
{
227+
(*ccx_s_options).sharing_enabled = options.sharing_enabled as _;
228+
if options.sharing_url.is_some() {
229+
(*ccx_s_options).sharing_url =
230+
string_to_c_char(&options.sharing_url.as_ref().unwrap().as_str());
231+
}
232+
(*ccx_s_options).translate_enabled = options.translate_enabled as _;
233+
if options.translate_langs.is_some() {
234+
(*ccx_s_options).translate_langs = string_to_c_char(&options.translate_langs.unwrap());
235+
}
236+
if options.translate_key.is_some() {
237+
(*ccx_s_options).translate_key = string_to_c_char(&options.translate_key.unwrap());
232238
}
233239
}
234240
}

src/rust/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use std::os::windows::io::{FromRawHandle, RawHandle};
2828
use args::Args;
2929
use bindings::*;
3030
use clap::{error::ErrorKind, Parser};
31-
use common::{CType, CType2, FromRust};
31+
use common::{copy_from_rust, CType, CType2};
3232
use decoder::Dtvcc;
3333
use lib_ccxr::{common::Options, teletext::TeletextConfig, util::log::ExitCause};
3434
use parser::OptionsExt;
@@ -282,7 +282,7 @@ pub unsafe extern "C" fn ccxr_parse_parameters(argc: c_int, argv: *mut *mut c_ch
282282
tlt_config = _tlt_config.to_ctype(&opt);
283283

284284
// Convert the rust struct (CcxOptions) to C struct (ccx_s_options), so that it can be used by the C code
285-
ccx_options.copy_from_rust(opt);
285+
copy_from_rust(&raw mut ccx_options, opt);
286286

287287
if !_capitalization_list.is_empty() {
288288
capitalization_list = _capitalization_list.to_ctype();

0 commit comments

Comments
 (0)