Skip to content

Commit 251c09e

Browse files
FreezyLemonshssoichiro
authored andcommitted
Add utils for ptr->&str conversion
1 parent 814f8b9 commit 251c09e

File tree

24 files changed

+93
-152
lines changed

24 files changed

+93
-152
lines changed

src/codec/codec.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use std::ffi::CStr;
2-
use std::str::from_utf8_unchecked;
3-
41
use super::{Audio, Capabilities, Id, Profile, Video};
52
use crate::ffi::*;
6-
use crate::{media, Error};
3+
use crate::{media, utils, Error};
74

85
#[derive(PartialEq, Eq, Copy, Clone)]
96
pub struct Codec {
@@ -37,18 +34,11 @@ impl Codec {
3734
}
3835

3936
pub fn name(&self) -> &str {
40-
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).name).to_bytes()) }
37+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).name) }
4138
}
4239

4340
pub fn description(&self) -> &str {
44-
unsafe {
45-
let long_name = (*self.as_ptr()).long_name;
46-
if long_name.is_null() {
47-
""
48-
} else {
49-
from_utf8_unchecked(CStr::from_ptr(long_name).to_bytes())
50-
}
51-
}
41+
unsafe { utils::optional_str_from_c_ptr((*self.as_ptr()).long_name).unwrap_or("") }
5242
}
5343

5444
pub fn medium(&self) -> media::Type {

src/codec/id.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use std::ffi::CStr;
2-
use std::str::from_utf8_unchecked;
3-
41
use crate::ffi::AVCodecID::*;
52
use crate::ffi::*;
63
use crate::util::media;
4+
use crate::utils;
75
#[cfg(feature = "serialize")]
86
use serde::{Deserialize, Serialize};
97

@@ -661,7 +659,7 @@ impl Id {
661659
}
662660

663661
pub fn name(&self) -> &'static str {
664-
unsafe { from_utf8_unchecked(CStr::from_ptr(avcodec_get_name((*self).into())).to_bytes()) }
662+
unsafe { utils::str_from_c_ptr(avcodec_get_name((*self).into())) }
665663
}
666664
}
667665

src/codec/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,17 @@ pub mod decoder;
4848
pub mod encoder;
4949
pub mod traits;
5050

51-
use std::ffi::CStr;
52-
use std::str::from_utf8_unchecked;
53-
5451
use crate::ffi::*;
52+
use crate::utils;
5553

5654
pub fn version() -> u32 {
5755
unsafe { avcodec_version() }
5856
}
5957

6058
pub fn configuration() -> &'static str {
61-
unsafe { from_utf8_unchecked(CStr::from_ptr(avcodec_configuration()).to_bytes()) }
59+
unsafe { utils::str_from_c_ptr(avcodec_configuration()) }
6260
}
6361

6462
pub fn license() -> &'static str {
65-
unsafe { from_utf8_unchecked(CStr::from_ptr(avcodec_license()).to_bytes()) }
63+
unsafe { utils::str_from_c_ptr(avcodec_license()) }
6664
}

src/codec/subtitle/rect.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::ffi::CStr;
21
use std::marker::PhantomData;
3-
use std::str::from_utf8_unchecked;
42

53
use super::{Flags, Type};
64
use crate::ffi::*;
5+
use crate::utils;
76
#[cfg(not(feature = "ffmpeg_5_0"))]
87
use crate::{format, Picture};
98

@@ -122,7 +121,7 @@ impl<'a> Text<'a> {
122121

123122
impl<'a> Text<'a> {
124123
pub fn get(&self) -> &str {
125-
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).text).to_bytes()) }
124+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).text) }
126125
}
127126
}
128127

@@ -147,6 +146,6 @@ impl<'a> Ass<'a> {
147146

148147
impl<'a> Ass<'a> {
149148
pub fn get(&self) -> &str {
150-
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).ass).to_bytes()) }
149+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).ass) }
151150
}
152151
}

src/device/mod.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ pub mod extensions;
22
pub mod input;
33
pub mod output;
44

5-
use std::ffi::CStr;
65
use std::marker::PhantomData;
7-
use std::str::from_utf8_unchecked;
86

97
use crate::ffi::*;
8+
use crate::utils;
109

1110
pub struct Info<'a> {
1211
ptr: *mut AVDeviceInfo,
@@ -33,13 +32,11 @@ impl<'a> Info<'a> {
3332

3433
impl<'a> Info<'a> {
3534
pub fn name(&self) -> &str {
36-
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).device_name).to_bytes()) }
35+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).device_name) }
3736
}
3837

3938
pub fn description(&self) -> &str {
40-
unsafe {
41-
from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).device_description).to_bytes())
42-
}
39+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).device_description) }
4340
}
4441
}
4542

@@ -54,9 +51,9 @@ pub fn version() -> u32 {
5451
}
5552

5653
pub fn configuration() -> &'static str {
57-
unsafe { from_utf8_unchecked(CStr::from_ptr(avdevice_configuration()).to_bytes()) }
54+
unsafe { utils::str_from_c_ptr(avdevice_configuration()) }
5855
}
5956

6057
pub fn license() -> &'static str {
61-
unsafe { from_utf8_unchecked(CStr::from_ptr(avdevice_license()).to_bytes()) }
58+
unsafe { utils::str_from_c_ptr(avdevice_license()) }
6259
}

src/filter/filter.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::ffi::CStr;
21
use std::marker::PhantomData;
3-
use std::str::from_utf8_unchecked;
42

53
use super::{Flags, Pad};
64
use crate::ffi::*;
5+
use crate::utils;
76

87
pub struct Filter {
98
ptr: *mut AVFilter,
@@ -25,19 +24,11 @@ impl Filter {
2524

2625
impl Filter {
2726
pub fn name(&self) -> &str {
28-
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).name).to_bytes()) }
27+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).name) }
2928
}
3029

3130
pub fn description(&self) -> Option<&str> {
32-
unsafe {
33-
let ptr = (*self.as_ptr()).description;
34-
35-
if ptr.is_null() {
36-
None
37-
} else {
38-
Some(from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes()))
39-
}
40-
}
31+
unsafe { utils::optional_str_from_c_ptr((*self.as_ptr()).description) }
4132
}
4233

4334
pub fn inputs(&self) -> Option<PadIter> {

src/filter/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ pub use self::context::{Context, Sink, Source};
1313
pub mod graph;
1414
pub use self::graph::Graph;
1515

16-
use std::ffi::{CStr, CString};
17-
use std::str::from_utf8_unchecked;
16+
use std::ffi::CString;
1817

1918
use crate::ffi::*;
19+
use crate::utils;
2020
#[cfg(not(feature = "ffmpeg_5_0"))]
2121
use crate::Error;
2222

@@ -42,11 +42,11 @@ pub fn version() -> u32 {
4242
}
4343

4444
pub fn configuration() -> &'static str {
45-
unsafe { from_utf8_unchecked(CStr::from_ptr(avfilter_configuration()).to_bytes()) }
45+
unsafe { utils::str_from_c_ptr(avfilter_configuration()) }
4646
}
4747

4848
pub fn license() -> &'static str {
49-
unsafe { from_utf8_unchecked(CStr::from_ptr(avfilter_license()).to_bytes()) }
49+
unsafe { utils::str_from_c_ptr(avfilter_license()) }
5050
}
5151

5252
pub fn find(name: &str) -> Option<Filter> {

src/filter/pad.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::ffi::CStr;
21
use std::marker::PhantomData;
3-
use std::str::from_utf8_unchecked;
42

53
use crate::ffi::*;
64
use crate::media;
5+
use crate::utils;
76

87
pub struct Pad<'a> {
98
ptr: *const AVFilterPad,
@@ -34,12 +33,7 @@ impl<'a> Pad<'a> {
3433
pub fn name(&self) -> Option<&str> {
3534
unsafe {
3635
let ptr = avfilter_pad_get_name(self.ptr, self.idx as i32);
37-
38-
if ptr.is_null() {
39-
None
40-
} else {
41-
Some(from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes()))
42-
}
36+
utils::optional_str_from_c_ptr(ptr)
4337
}
4438
}
4539

src/format/format/input.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use std::ffi::CStr;
2-
use std::str::from_utf8_unchecked;
3-
41
use crate::ffi::*;
2+
use crate::utils;
53

64
pub struct Input {
75
ptr: *mut AVInputFormat,
@@ -23,19 +21,11 @@ impl Input {
2321

2422
impl Input {
2523
pub fn name(&self) -> &str {
26-
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).name).to_bytes()) }
24+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).name) }
2725
}
2826

2927
pub fn description(&self) -> &str {
30-
unsafe {
31-
let long_name = (*self.as_ptr()).long_name;
32-
33-
if long_name.is_null() {
34-
""
35-
} else {
36-
from_utf8_unchecked(CStr::from_ptr(long_name).to_bytes())
37-
}
38-
}
28+
unsafe { utils::optional_str_from_c_ptr((*self.as_ptr()).long_name).unwrap_or("") }
3929
}
4030

4131
pub fn extensions(&self) -> Vec<&str> {
@@ -45,9 +35,7 @@ impl Input {
4535
if ptr.is_null() {
4636
Vec::new()
4737
} else {
48-
from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes())
49-
.split(',')
50-
.collect()
38+
utils::str_from_c_ptr(ptr).split(',').collect()
5139
}
5240
}
5341
}
@@ -59,9 +47,7 @@ impl Input {
5947
if ptr.is_null() {
6048
Vec::new()
6149
} else {
62-
from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes())
63-
.split(',')
64-
.collect()
50+
utils::str_from_c_ptr(ptr).split(',').collect()
6551
}
6652
}
6753
}

src/format/format/output.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use std::path::Path;
22

3-
use std::ffi::{CStr, CString};
3+
use std::ffi::CString;
44
use std::ptr;
5-
use std::str::from_utf8_unchecked;
65

76
use super::Flags;
87
use crate::ffi::*;
9-
use crate::{codec, media};
8+
use crate::{codec, media, utils};
109

1110
pub struct Output {
1211
ptr: *mut AVOutputFormat,
@@ -28,19 +27,11 @@ impl Output {
2827

2928
impl Output {
3029
pub fn name(&self) -> &str {
31-
unsafe { from_utf8_unchecked(CStr::from_ptr((*self.as_ptr()).name).to_bytes()) }
30+
unsafe { utils::str_from_c_ptr((*self.as_ptr()).name) }
3231
}
3332

3433
pub fn description(&self) -> &str {
35-
unsafe {
36-
let long_name = (*self.as_ptr()).long_name;
37-
38-
if long_name.is_null() {
39-
""
40-
} else {
41-
from_utf8_unchecked(CStr::from_ptr(long_name).to_bytes())
42-
}
43-
}
34+
unsafe { utils::optional_str_from_c_ptr((*self.as_ptr()).long_name).unwrap_or("") }
4435
}
4536

4637
pub fn extensions(&self) -> Vec<&str> {
@@ -50,9 +41,7 @@ impl Output {
5041
if ptr.is_null() {
5142
Vec::new()
5243
} else {
53-
from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes())
54-
.split(',')
55-
.collect()
44+
utils::str_from_c_ptr(ptr).split(',').collect()
5645
}
5746
}
5847
}
@@ -64,9 +53,7 @@ impl Output {
6453
if ptr.is_null() {
6554
Vec::new()
6655
} else {
67-
from_utf8_unchecked(CStr::from_ptr(ptr).to_bytes())
68-
.split(',')
69-
.collect()
56+
utils::str_from_c_ptr(ptr).split(',').collect()
7057
}
7158
}
7259
}

0 commit comments

Comments
 (0)