Skip to content

Commit 5cec41c

Browse files
committed
Use as_ptr and bring shape_full and shape_with_flags into the same format
1 parent 6d06e63 commit 5cec41c

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

pango/src/functions.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Take a look at the license at the top of the repository in the LICENSE file.
22

33
use glib::translate::*;
4-
use std::ffi::c_char;
4+
use std::{ffi::c_char, ptr};
55

66
pub use crate::auto::functions::*;
77
#[cfg(feature = "v1_44")]
@@ -24,19 +24,17 @@ pub fn shape_full(
2424
analysis: &Analysis,
2525
glyphs: &mut GlyphString,
2626
) {
27-
let paragraph_length = match paragraph_text {
28-
Some(s) => s.len(),
29-
None => 0,
30-
} as i32;
3127
let item_length = item_text.len() as i32;
28+
let paragraph_length = paragraph_text.map(|t| t.len() as i32).unwrap_or_default();
29+
let paragraph_ptr = paragraph_text.map_or(ptr::null(), |t| t.as_ptr() as *const c_char);
3230
unsafe {
3331
// The function does not take null-terminated strings when a length is provided.
3432
// It also requires item_text to point to a subsequence of paragraph_text.
3533
// Using to_glib_none() on &str will copy the string and cause problems.
3634
ffi::pango_shape_full(
37-
item_text.as_bytes().to_glib_none().0 as *const c_char,
35+
item_text.as_ptr() as *const c_char,
3836
item_length,
39-
paragraph_text.map(|t| t.as_bytes()).to_glib_none().0 as *const c_char,
37+
paragraph_ptr,
4038
paragraph_length,
4139
analysis.to_glib_none().0,
4240
glyphs.to_glib_none_mut().0,
@@ -56,12 +54,13 @@ pub fn shape_with_flags(
5654
) {
5755
let item_length = item_text.len() as i32;
5856
let paragraph_length = paragraph_text.map(|t| t.len() as i32).unwrap_or_default();
57+
let paragraph_ptr = paragraph_text.map_or(ptr::null(), |t| t.as_ptr() as *const c_char);
5958
unsafe {
6059
// See: shape_full
6160
ffi::pango_shape_with_flags(
62-
item_text.as_bytes().to_glib_none().0 as *const c_char,
61+
item_text.as_ptr() as *const c_char,
6362
item_length,
64-
paragraph_text.map(|t| t.as_bytes()).to_glib_none().0 as *const c_char,
63+
paragraph_ptr,
6564
paragraph_length,
6665
analysis.to_glib_none().0,
6766
glyphs.to_glib_none_mut().0,

0 commit comments

Comments
 (0)