Skip to content

Commit

Permalink
Use TaffyStyleConstRef and TaffyStyleMutRef types
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Sep 23, 2023
1 parent 52c2072 commit c9fa593
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 108 deletions.
9 changes: 4 additions & 5 deletions ctaffy/cbindgen.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ cpp_compat = true
documentation_style = "c99"
line_length = 120

# [parse]
# parse_deps = true
# include = ["taffy"]
# extra_bindings = ["taffy"]
[export]
include = ["TaffyNodeId"]

[parse.expand]
crates = ["ctaffy"]
crates = ["ctaffy"]

13 changes: 13 additions & 0 deletions ctaffy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ mod style;
mod style_enums;
mod value;

pub struct Taffy;
pub type TaffyMutRef = *mut Taffy;
pub type TaffyConstRef = *const Taffy;

pub struct TaffyNodeId(u64);

pub struct TaffyStyle;
pub type TaffyStyleMutRef = *mut TaffyStyle;
pub type TaffyStyleConstRef = *const TaffyStyle;

pub struct TaffyLayout;
pub type TaffyLayoutConstRef = *const TaffyLayout;

pub use error::*;
pub use style::*;
pub use style_enums::*;
Expand Down
29 changes: 13 additions & 16 deletions ctaffy/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
use super::{
FloatResult, GridPlacement, GridPlacementResult, IntResult, ReturnCode, StyleValue, StyleValueResult,
StyleValueUnit, TaffyAlignContent, TaffyAlignItems, TaffyDisplay, TaffyEdge, TaffyFFIResult, TaffyFlexDirection,
TaffyFlexWrap, TaffyGridAutoFlow, TaffyOverflow, TaffyPosition,
TaffyFlexWrap, TaffyGridAutoFlow, TaffyOverflow, TaffyPosition, TaffyStyleMutRef, TaffyStyleConstRef
};
use std::ffi::c_void;
use taffy::prelude as core;

pub struct TaffyStyle;
pub type TaffyStyleRef = *mut TaffyStyle;

/// Return [`ReturnCode::NullStylePointer`] if the passed pointer is null
macro_rules! assert_style_pointer_is_non_null {
($raw_style_ptr:expr) => {{
Expand Down Expand Up @@ -72,7 +69,7 @@ macro_rules! try_from_raw {
macro_rules! enum_prop_getter {
($func_name:ident; $($props:ident).+) => {
#[no_mangle]
pub unsafe extern "C" fn $func_name(raw_style: *const TaffyStyle) -> IntResult {
pub unsafe extern "C" fn $func_name(raw_style: TaffyStyleConstRef) -> IntResult {
get_style!(raw_style, style, style.$($props).* as i32)
}
};
Expand All @@ -81,7 +78,7 @@ macro_rules! enum_prop_getter {
macro_rules! option_enum_prop_getter {
($func_name:ident; $($props:ident).+) => {
#[no_mangle]
pub unsafe extern "C" fn $func_name(raw_style: *const TaffyStyle) -> IntResult {
pub unsafe extern "C" fn $func_name(raw_style: TaffyStyleConstRef) -> IntResult {
get_style!(raw_style, style, style.$($props).*.map(|v| v as i32).unwrap_or(0))
}
};
Expand All @@ -91,7 +88,7 @@ macro_rules! option_enum_prop_getter {
macro_rules! enum_prop_setter {
($func_name:ident; $($props:ident).+; $enum:ident) => {
#[no_mangle]
pub unsafe extern "C" fn $func_name(raw_style: *mut TaffyStyle, value: $enum) -> ReturnCode {
pub unsafe extern "C" fn $func_name(raw_style: TaffyStyleMutRef, value: $enum) -> ReturnCode {
with_style_mut!(raw_style, style, style.$($props).* = value.into())
}
};
Expand Down Expand Up @@ -141,7 +138,7 @@ enum_prop_setter!(TaffyStyle_SetGridAutoFlow; grid_auto_flow; TaffyGridAutoFlow)
macro_rules! style_value_prop_getter {
($func_name:ident; $($props:ident).+) => {
#[no_mangle]
pub unsafe extern "C" fn $func_name(raw_style: *const TaffyStyle) -> StyleValueResult {
pub unsafe extern "C" fn $func_name(raw_style: TaffyStyleConstRef) -> StyleValueResult {
get_style!(raw_style, style, style.$($props).*)
}
};
Expand All @@ -151,7 +148,7 @@ macro_rules! style_value_prop_getter {
macro_rules! style_value_prop_setter {
($func_name:ident; $($props:ident).+) => {
#[no_mangle]
pub unsafe extern "C" fn $func_name(raw_style: *mut TaffyStyle, value: f32, unit: StyleValueUnit) -> ReturnCode {
pub unsafe extern "C" fn $func_name(raw_style: TaffyStyleMutRef, value: f32, unit: StyleValueUnit) -> ReturnCode {
with_style_mut!(raw_style, style, style.$($props).* = try_from_raw!(unit, value))
}
};
Expand Down Expand Up @@ -223,11 +220,11 @@ style_value_prop_setter!(TaffyStyle_SetRowGap; gap.height);

// Aspect ratio
#[no_mangle]
pub unsafe extern "C" fn TaffyStyle_GetAspectRatio(raw_style: *const TaffyStyle) -> FloatResult {
pub unsafe extern "C" fn TaffyStyle_GetAspectRatio(raw_style: TaffyStyleConstRef) -> FloatResult {
get_style!(raw_style, style, style.aspect_ratio.unwrap_or(f32::NAN))
}
#[no_mangle]
pub unsafe extern "C" fn TaffyStyle_SetAspectRatio(raw_style: *mut TaffyStyle, value: f32) -> ReturnCode {
pub unsafe extern "C" fn TaffyStyle_SetAspectRatio(raw_style: TaffyStyleMutRef, value: f32) -> ReturnCode {
with_style_mut!(raw_style, style, {
if value.is_finite() && value > 0.0 {
style.aspect_ratio = Some(value)
Expand All @@ -241,7 +238,7 @@ pub unsafe extern "C" fn TaffyStyle_SetAspectRatio(raw_style: *mut TaffyStyle, v
macro_rules! float_prop_getter {
($func_name:ident; $($props:ident).+) => {
#[no_mangle]
pub unsafe extern "C" fn $func_name(raw_style: *const TaffyStyle) -> FloatResult {
pub unsafe extern "C" fn $func_name(raw_style: TaffyStyleConstRef) -> FloatResult {
get_style!(raw_style, style, style.$($props).*)
}
};
Expand All @@ -251,7 +248,7 @@ macro_rules! float_prop_getter {
macro_rules! float_prop_setter {
($func_name:ident; $($props:ident).+) => {
#[no_mangle]
pub unsafe extern "C" fn $func_name(raw_style: *mut TaffyStyle, value: f32) -> ReturnCode {
pub unsafe extern "C" fn $func_name(raw_style: TaffyStyleMutRef, value: f32) -> ReturnCode {
with_style_mut!(raw_style, style, style.$($props).* = value)
}
};
Expand All @@ -272,7 +269,7 @@ float_prop_setter!(TaffyStyle_SetFlexShrink; flex_shrink);
/// Function to set all the value of margin
#[no_mangle]
pub unsafe extern "C" fn TaffyStyle_SetMargin(
raw_style: *mut TaffyStyle,
raw_style: TaffyStyleMutRef,
edge: TaffyEdge,
value: StyleValue,
) -> ReturnCode {
Expand Down Expand Up @@ -305,12 +302,12 @@ pub unsafe extern "C" fn TaffyStyle_SetMargin(

/// Get grid item's column placement
#[no_mangle]
pub unsafe extern "C" fn TaffyStyleGetGridColumn(raw_style: *mut TaffyStyle) -> GridPlacementResult {
pub unsafe extern "C" fn TaffyStyleGetGridColumn(raw_style: TaffyStyleMutRef) -> GridPlacementResult {
get_style!(raw_style, style, style.grid_column)
}

/// Set grid item's column placement
#[no_mangle]
pub unsafe extern "C" fn TaffyStyleSetGridColumn(raw_style: *mut TaffyStyle, placement: GridPlacement) -> ReturnCode {
pub unsafe extern "C" fn TaffyStyleSetGridColumn(raw_style: TaffyStyleMutRef, placement: GridPlacement) -> ReturnCode {
with_style_mut!(raw_style, style, style.grid_column = placement.into())
}
Loading

0 comments on commit c9fa593

Please sign in to comment.