Skip to content

Commit 1cb7606

Browse files
committed
clippy changes
1 parent f0f4d6f commit 1cb7606

File tree

4 files changed

+37
-29
lines changed

4 files changed

+37
-29
lines changed

src/rust/src/hardsubx/decoder.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::hardsubx_color_type;
2323
use super::hardsubx_ocr_mode;
2424
use super::utility::*;
2525
use super::HardsubxContext;
26-
use super::CCX_ENC_UTF_8;
26+
use crate::ccx_encoding_type::*;
2727

2828
use std::cmp;
2929

@@ -313,13 +313,16 @@ pub unsafe fn _process_frame_tickertext(
313313
subtitle_text
314314
}
315315

316+
/// # Safety
317+
/// dereferences a raw pointer
318+
/// calls potentially unsafe C functions
316319
pub unsafe fn hardsubx_process_frames_linear(ctx: &mut HardsubxContext, enc_ctx: *mut encoder_ctx) {
317320
let mut prev_sub_encoded: bool = true;
318321
let mut got_frame = 0;
319322
let mut dist = 0;
320323
let mut cur_sec = 0;
321-
let mut total_sec = 0;
322-
let mut progress = 0;
324+
let mut total_sec;
325+
let mut progress;
323326

324327
let mut frame_number = 0;
325328

@@ -328,7 +331,7 @@ pub unsafe fn hardsubx_process_frames_linear(ctx: &mut HardsubxContext, enc_ctx:
328331

329332
let mut prev_packet_pts: i64 = 0;
330333

331-
let mut subtitle_text: String = String::new();
334+
let mut subtitle_text = String::new();
332335
let mut prev_subtitle_text: String = String::new();
333336

334337
while av_read_frame(ctx.format_ctx, &mut ctx.packet as *mut AVPacket) >= 0 {
@@ -418,11 +421,9 @@ pub unsafe fn hardsubx_process_frames_linear(ctx: &mut HardsubxContext, enc_ctx:
418421

419422
if !subtitle_text.is_empty() {
420423
let double_enter = subtitle_text.find("\n\n");
421-
match double_enter {
422-
Some(T) => {
423-
subtitle_text = subtitle_text[0..T].to_string();
424-
}
425-
_ => {}
424+
425+
if let Some(T) = double_enter {
426+
subtitle_text = subtitle_text[0..T].to_string();
426427
}
427428
}
428429

@@ -460,7 +461,7 @@ pub unsafe fn hardsubx_process_frames_linear(ctx: &mut HardsubxContext, enc_ctx:
460461
prev_begin_time,
461462
empty_chr,
462463
mode_chr,
463-
CCX_ENC_UTF_8,
464+
CCX_ENC_UTF_8 as u32,
464465
);
465466

466467
encode_sub(enc_ctx, &mut *ctx.dec_sub);
@@ -469,7 +470,7 @@ pub unsafe fn hardsubx_process_frames_linear(ctx: &mut HardsubxContext, enc_ctx:
469470
subtitle_text = ffi::CString::from_raw(sub_text_chr)
470471
.to_string_lossy()
471472
.into_owned();
472-
prev_subtitle_text = ffi::CString::from_raw(prev_text_chr)
473+
ffi::CString::from_raw(prev_text_chr)
473474
.to_string_lossy()
474475
.into_owned();
475476
ffi::CString::from_raw(empty_chr);
@@ -524,21 +525,20 @@ pub unsafe fn hardsubx_process_frames_linear(ctx: &mut HardsubxContext, enc_ctx:
524525
prev_begin_time,
525526
empty_chr,
526527
mode_chr,
527-
CCX_ENC_UTF_8,
528+
CCX_ENC_UTF_8 as u32,
528529
);
529530

530531
// Deallocation
531-
subtitle_text = ffi::CString::from_raw(sub_text_chr)
532+
ffi::CString::from_raw(sub_text_chr)
532533
.to_string_lossy()
533534
.into_owned();
534-
prev_subtitle_text = ffi::CString::from_raw(prev_text_chr)
535+
ffi::CString::from_raw(prev_text_chr)
535536
.to_string_lossy()
536537
.into_owned();
537538
ffi::CString::from_raw(empty_chr);
538539
ffi::CString::from_raw(mode_chr);
539540

540541
encode_sub(enc_ctx, &mut *ctx.dec_sub as *mut cc_subtitle);
541-
prev_sub_encoded = true;
542542
}
543543

544544
activity_progress(
@@ -548,20 +548,23 @@ pub unsafe fn hardsubx_process_frames_linear(ctx: &mut HardsubxContext, enc_ctx:
548548
);
549549
}
550550

551+
552+
/// # Safety
553+
/// dereferences a raw pointer
554+
/// calls potentially unsafe C functions
551555
pub unsafe fn hardsubx_process_frames_tickertext(
552556
ctx: &mut HardsubxContext,
553557
_enc_ctx: *mut encoder_ctx,
554558
) {
555559
let mut got_frame: bool = false;
556560

557561
let mut cur_sec: i64 = 0;
558-
let mut total_sec: i64 = 0;
559-
let mut progress: i64 = 0;
562+
let mut total_sec: i64;
563+
let mut progress: i64;
560564

561565
let mut frame_number = 0;
562566

563-
let mut ticker_text = String::new();
564-
567+
let mut ticker_text;
565568
while av_read_frame(ctx.format_ctx, &mut ctx.packet) >= 0 {
566569
if ctx.packet.stream_index == ctx.video_stream_id {
567570
frame_number += 1;

src/rust/src/hardsubx/main.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ extern "C" {
2626

2727

2828

29-
29+
/// # Safety
30+
/// Dereferences a raw pointer (datamember of the object ctx)
31+
/// calls potentially unsafe C functions
3032
pub unsafe fn hardsubx_process_data(ctx: &mut HardsubxContext, _ctx_normal: *mut lib_ccx_ctx) {
3133
let mut format_ctx_tmp: *mut AVFormatContext = null::<AVFormatContext>() as *mut AVFormatContext;
3234
let file_name = string_to_c_char(&ctx.inputfile[0]);
@@ -194,7 +196,8 @@ pub unsafe fn hardsubx_process_data(ctx: &mut HardsubxContext, _ctx_normal: *mut
194196
avcodec_free_context(&mut codec_ctx as *mut *mut AVCodecContext);
195197
}
196198

197-
199+
/// # Safety
200+
/// calls potentially unsafe C functions
198201
pub unsafe fn _dinit_hardsubx(ctx: &mut HardsubxContext)
199202
{
200203

@@ -203,6 +206,8 @@ pub unsafe fn _dinit_hardsubx(ctx: &mut HardsubxContext)
203206
TessBaseAPIDelete(ctx.tess_handle);
204207
}
205208

209+
/// # Safety
210+
/// Dereferences a raw pointer (datamember of the object ctx)
206211
#[no_mangle]
207212
pub unsafe extern "C" fn hardsubx(options: *mut ccx_s_options, ctx_normal: *mut lib_ccx_ctx) {
208213
let mut ctx = HardsubxContext::new(options);

src/rust/src/hardsubx/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ use std::os::raw::c_void;
3232
// definitions taken from ccx_common_common.h
3333
static EXIT_NOT_ENOUGH_MEMORY: i32 = 500;
3434
static EXIT_READ_ERROR: i32 = 8;
35-
static CCX_ENC_UNICODE: u32 = 0;
36-
static CCX_ENC_LATIN_1: u32 = 1;
37-
static CCX_ENC_UTF_8: u32 = 2;
38-
static CCX_ENC_ASCII: u32 = 3;
35+
3936

4037
static EXIT_MALFORMED_PARAMETER: i32 = 7;
4138

@@ -238,6 +235,9 @@ impl Default for HardsubxContext {
238235
}
239236

240237
impl HardsubxContext {
238+
/// # Safety
239+
/// dereferences a raw pointer
240+
/// Calls C functions and sends them raw pointers, their safety is not guaranteed
241241
pub unsafe fn new(options: *mut ccx_s_options) -> Self {
242242
let tess_handle = &mut (*TessBaseAPICreate()) as &mut TessBaseAPI;
243243

@@ -256,7 +256,7 @@ impl HardsubxContext {
256256
let tessdata_path = {
257257
let path = probe_tessdata_location(lang_cstr);
258258

259-
if path == (null::<c_char>() as *mut c_char) && lang != "eng".to_string() {
259+
if path == (null::<c_char>() as *mut c_char) && lang != *"eng" {
260260
let eng_cstr = string_to_c_char("eng");
261261
let tmp = probe_tessdata_location(eng_cstr);
262262
ffi::CString::from_raw(eng_cstr); // deallocation
@@ -327,7 +327,7 @@ impl HardsubxContext {
327327
ffi::CString::from_raw(lang_cstr); //deallocate
328328
// function to be used for only converting the C struct to rust
329329
Self {
330-
tess_handle: tess_handle,
330+
tess_handle,
331331
basefilename: {
332332
if (*options).output_filename == null::<c_char>() as *mut c_char {
333333
"".to_string()

src/rust/src/hardsubx/utility.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ pub unsafe extern "C" fn edit_distance(
8888
}
8989

9090
pub fn edit_distance_string(
91-
word1: &String,
92-
word2: &String,
91+
word1: &str,
92+
word2: &str,
9393
) -> i32 {
9494

9595
let len1 = word1.chars().count();

0 commit comments

Comments
 (0)