rust: finish up EGC type safe wrapping.

pull/931/head
joseLuís 4 years ago
parent 11a9ce0f5e
commit 356b44b015

@ -59,7 +59,7 @@
use crate as ffi;
use ffi::types::{
AlphaBits, Channel, ChannelPair, Color, IntResult, PaletteIndex, StyleMask,
AlphaBits, Channel, ChannelPair, CellGcluster, Color, EGC, IntResult, PaletteIndex, StyleMask,
};
use ffi::{cell, ncplane};
@ -261,8 +261,10 @@ pub fn cell_wide_left_p(cell: &cell) -> bool {
/// result is not tied to the ncplane, and persists across erases / destruction.
// TODO: TEST
#[inline]
pub fn cell_strdup(plane: &ncplane, cell: &cell) -> *mut i8 {
unsafe { libc::strdup(ffi::cell_extended_gcluster(plane, cell)) }
pub fn cell_strdup(plane: &ncplane, cell: &cell) -> EGC {
unsafe {
core::char::from_u32_unchecked(libc::strdup(ffi::cell_extended_gcluster(plane, cell)) as i32 as u32)
}
}
/// Extract the three elements of a cell.
@ -273,7 +275,7 @@ pub fn cell_extract(
cell: &cell,
stylemask: &mut StyleMask,
channels: &mut ChannelPair,
) -> *mut i8 {
) -> EGC {
if *stylemask != 0 {
*stylemask = cell.stylemask;
}
@ -307,12 +309,12 @@ pub fn cellcmp(plane1: &ncplane, cell1: &cell, plane2: &ncplane, cell2: &cell) -
// TODO: TEST
// NOTE: remove casting for CELL_WIEDASIAN_MASK when fixed: https://github.com/rust-lang/rust-bindgen/issues/1875
#[inline]
pub fn cell_load_simple(plane: &mut ncplane, cell: &mut cell, ch: char) -> i32 {
pub fn cell_load_simple(plane: &mut ncplane, cell: &mut cell, ch: EGC) -> i32 {
unsafe {
ffi::cell_release(plane, cell);
}
cell.channels &= !(ffi::CELL_WIDEASIAN_MASK as u64 | ffi::CELL_NOBACKGROUND_MASK);
cell.gcluster = ch as u32;
cell.gcluster = ch as CellGcluster;
1
}

@ -142,7 +142,7 @@ use core::ptr::null_mut;
use cstr_core::CString;
use crate as ffi;
use ffi::types::{AlphaBits, Channel, ChannelPair, Color, EGCBackstop, IntResult, StyleMask};
use ffi::types::{AlphaBits, Channel, ChannelPair, Color, EGC, EGCBackstop, IntResult, StyleMask};
use ffi::{cell, ncalign_e, ncplane};
/// Return the column at which 'cols' columns ought start in order to be aligned
@ -470,7 +470,7 @@ pub fn ncplane_putc(plane: &mut ncplane, cell: &cell) -> IntResult {
/// Call ncplane_putsimple_yx() at the current cursor location.
// TODO: TEST
#[inline]
pub fn ncplane_putsimple(plane: &mut ncplane, ch: char) -> IntResult {
pub fn ncplane_putsimple(plane: &mut ncplane, ch: EGC) -> IntResult {
ffi::ncplane_putsimple_yx(plane, -1, -1, ch)
}
@ -484,12 +484,12 @@ pub fn ncplane_putegc(plane: &mut ncplane, gcluster: i8, sbytes: &mut i32) -> In
/// Replace the EGC underneath us, but retain the styling. The current styling
/// of the plane will not be changed.
///
/// Replace the cell at the specified coordinates with the provided 7-bit char
/// 'ch'. Advance the cursor by 1. On success, returns 1. On failure, returns -1.
/// This works whether the underlying char is signed or unsigned.
/// Replace the cell at the specified coordinates with the provided 7-bit char 'ch'.
/// Advance the cursor by 1. On success, returns 1. On failure, returns -1.
///
// TODO: TEST
#[inline]
pub fn ncplane_putsimple_yx(plane: &mut ncplane, y: i32, x: i32, ch: char) -> IntResult {
pub fn ncplane_putsimple_yx(plane: &mut ncplane, y: i32, x: i32, ch: EGC) -> IntResult {
let newcell = cell_initializer![ch, unsafe { ffi::ncplane_attr(plane) }, unsafe {
ffi::ncplane_channels(plane)
}];

@ -140,7 +140,7 @@ pub type Pixel = u32;
/// EGC (Extended Grapheme Cluster)
///
/// These 32 bits, together with the associated plane's associated egcpool,
/// This 32 bit char, together with the associated plane's associated egcpool,
/// completely define this cell's EGC. Unless the EGC requires more than four
/// bytes to encode as UTF-8, it will be inlined here:
///
@ -174,9 +174,9 @@ pub type Pixel = u32;
/// type in C: gcluster (uint32_t)
///
// WIP towards a safe abstraction for Cell & functions receiving
// pub type EGC = u32;
// pub type EGC = char;
// pub type EGC<'a> = &'a[u8];
pub type EGC = char;
// pub type EGCPool<'a> = &'a[u8];
pub type CellGcluster = u32; // the type cell.gcluster expects the EGB to be
/// EGC BackStop
///

Loading…
Cancel
Save