Skip to content

meloalright/taitank-rs

Repository files navigation

Taitank Rust

License Rust CI Documentation Crates.io

Taitank Rust is a Rust implementation of Taitank, a cross-platform lightweight Flexbox layout engine.

✨ Features

  • Complete Flexbox Support - Supports all Flexbox layout properties, fully compatible with the C++ version
  • High Performance - Optimized layout algorithm with layout caching, performance comparable to the C++ version
  • Memory Safe - Rust's ownership system ensures memory safety without manual memory management
  • Type Safe - Strong type system reduces runtime errors and catches issues at compile time
  • API Compatible - Compatible with the C++ version API, easy to migrate
  • Test Coverage - Complete unit test coverage (30+ test files, 280+ test cases)
  • Multi-platform Support - Supports macOS, Linux, Windows, Android, iOS
  • Comprehensive Documentation - Complete API documentation and usage examples

🚀 Quick Start

Installation

Add the dependency to your Cargo.toml:

[dependencies]
taitank-rs = "0.1.2"

Or use from Git repository:

[dependencies]
taitank-rs = { git = "https://github.com/rustq/taitank-rs.git" }

Basic Usage

use taitank::*;

// Create a root node
let root = node_create();
set_width(&root, 500.0);
set_height(&root, 300.0);
set_flex_direction(&root, FlexDirection::Row);
set_padding(&root, CSSDirection::All, 20.0);

// Create a child node
let child1 = node_create();
set_width(&child1, 100.0);
set_height(&child1, 100.0);
set_margin(&child1, CSSDirection::Right, 10.0);
insert_child(&root, child1.clone(), 0);

// Create a child node with flex
let child2 = node_create();
set_flex(&child2, 1.0); // flex: 1 to automatically fill remaining space
set_height(&child2, 100.0);
insert_child(&root, child2.clone(), 0);

// Perform layout calculation
do_layout(&root, VALUE_UNDEFINED, VALUE_UNDEFINED, TaitankDirection::Ltr, None);

// Get layout results
println!("Root: {}x{}", get_width(&root), get_height(&root));
println!("Child1: {}x{} at ({}, {})", 
         get_width(&child1), get_height(&child1),
         get_left(&child1), get_top(&child1));
println!("Child2: {}x{} at ({}, {})", 
         get_width(&child2), get_height(&child2),
         get_left(&child2), get_top(&child2));

📚 Examples

The project includes multiple examples demonstrating different use cases:

# Run basic example
cargo run --example basic

# Run complex layout example
cargo run --example complex

# Run absolute positioning example
cargo run --example absolute_position

See the examples/ directory for more example code.

🧪 Testing

Run all tests:

cargo test

Run a specific test:

cargo test align_self_test

Run tests with output:

cargo test -- --nocapture

📊 Performance Benchmarks

Run performance benchmarks using Criterion:

cargo bench --bench benchmark

Benchmarks include:

  • Stack with flex - Stacked flex layout (~4.05 µs)
  • Align stretch in undefined axis - Stretch alignment in undefined axis (~4.04 µs)
  • Nested flex - Nested flex layout (~60.5 µs)
  • Huge nested layout - Large nested layout (~3.41 ms)
  • Huge nested layout (no style) - Large nested layout without styles (~8.06 ms)

Performance reports are generated in the target/criterion/ directory with detailed performance analysis.

📖 API Documentation

Generate and view API documentation:

cargo doc --open

Online documentation: https://docs.rs/taitank-rs

🌐 Platform Support

Rust Library

Use directly as a Rust library, supports the following platforms:

  • ✅ macOS (x86_64, arm64)
  • ✅ Linux (x86_64, arm64)
  • ✅ Windows (x86_64, MSVC/GNU)
  • ✅ Android (arm64-v8a, armeabi-v7a, x86, x86_64)
  • ✅ iOS (arm64, x86_64 simulator)

📁 Project Structure

taitank-rust/
├── src/                    # Source code
│   ├── api.rs              # Public API
│   ├── node.rs             # Core node implementation
│   ├── style.rs            # Style management
│   ├── flex.rs             # Flexbox types and enums
│   ├── flexline.rs         # FlexLine implementation
│   ├── cache.rs            # Layout caching
│   ├── config.rs           # Configuration management
│   ├── util.rs             # Utility functions
│   └── lib.rs              # Library entry point
├── tests/                  # Unit tests (30+ test files)
├── examples/               # Example code
│   ├── basic.rs            # Basic usage example
│   ├── complex.rs          # Complex layout example
│   └── absolute_position.rs # Absolute positioning example
├── benches/                # Performance benchmarks
│   └── benchmark.rs        # Criterion benchmarks
├── .cargo/                 # Cargo configuration
│   └── config.toml         # Cross-compilation configuration
├── .github/                # GitHub configuration
│   └── workflows/          # CI/CD workflows
├── build.sh                # Build script
├── Cargo.toml              # Project configuration
├── README.md               # This file
├── README_ZH.md            # Chinese version
├── PLATFORMS.md            # Platform build guide
└── INTEGRATION.md          # Integration guide

🎯 Supported Layout Properties

Dimensions

  • set_width() / get_width() - Width
  • set_height() / get_height() - Height
  • set_min_width() / set_min_height() - Minimum dimensions
  • set_max_width() / set_max_height() - Maximum dimensions

Flex Properties

  • set_flex() - Flex shorthand
  • set_flex_grow() - Flex-grow
  • set_flex_shrink() - Flex-shrink
  • set_flex_basis() - Flex-basis
  • set_flex_direction() - Flex-direction
  • set_flex_wrap() - Flex-wrap

Alignment

  • set_justify_content() - Justify-content
  • set_align_items() - Align-items
  • set_align_content() - Align-content
  • set_align_self() - Align-self

Spacing

  • set_margin() / get_margin() - Margins
  • set_padding() / get_padding() - Padding
  • set_border() / get_border() - Borders
  • set_margin_auto() - Auto margins

Positioning

  • set_position_type() - Position (relative/absolute)
  • set_position() - Position setting

Others

  • set_display() - Display (flex/none)
  • set_overflow() - Overflow
  • set_direction() - Layout direction (LTR/RTL)
  • set_node_type() - Node type (Default/Text)

Debugging

  • print_node() - Print node tree structure
  • reset() - Reset node state
  • mark_dirty() / is_dirty() - Dirty marking management

🔄 Differences from C++ Version

Advantages

  • Memory Safety: Rust's ownership system ensures memory safety without manual memory deallocation
  • Type Safety: Stronger type checking reduces runtime errors
  • Concurrency Safety: Rust's concurrency model ensures thread safety
  • Zero-cost Abstractions: Rust's zero-cost abstractions guarantee performance

API Differences

  • Naming Convention: Follows Rust naming conventions (snake_case)
  • Memory Management: Uses Rc<RefCell<>> for reference counting, automatic memory management
  • Error Handling: Uses Rust's Result type for error handling
  • Optional Parameters: Uses Option<T> instead of C++ optional parameters

Compatibility

  • ✅ All C++ API features are implemented
  • ✅ Layout algorithm is fully consistent with the C++ version
  • ✅ All test cases pass
  • ✅ Performance is comparable to the C++ version

📝 Contributing

Contributions are welcome! Please ensure:

  1. Code Formatting: Run cargo fmt to ensure code formatting is correct
  2. Code Checking: Run cargo clippy to ensure there are no warnings
  3. Tests Pass: Run cargo test to ensure all tests pass
  4. Documentation Updates: Update relevant documentation and comments
  5. Commit Messages: Use clear commit messages

Development Workflow

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Create a Pull Request

Code Standards

  • Follow Rust official code style
  • Use meaningful variable and function names
  • Add necessary documentation comments
  • Write unit tests

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

🙏 Acknowledgments

  • Taitank C++ - Original C++ implementation
  • Yoga - Reference implementation of Flexbox layout algorithm

🔗 Related Links

📊 Project Status

  • ✅ Core functionality complete
  • ✅ All tests passing
  • ✅ API documentation complete
  • ✅ Example code complete
  • ✅ Performance benchmarks
  • ✅ Multi-platform support

🗺️ Roadmap

  • Add more examples
  • Performance optimizations
  • Extended documentation
  • Community feedback and improvements

License

MIT

About

Experiment Rust port of tencent/taitank provides pure rust layout library.

Resources

License

Stars

Watchers

Forks

Packages

No packages published