Skip to content

Instantly share code, notes, and snippets.

View sakex's full-sized avatar
🇮🇱

Alexandre Senges sakex

🇮🇱
View GitHub Profile
struct Point {
x: f64,
y: f64,
z: f64,
}
impl Point {
pub fn mid_point(&self, other: &Point) -> Point {
Point {
x: (self.x + other.x) / 2.0,
// AbstractButton.hpp (in GUI library)
#ifndef MEDIUM_ABSTRACT_BUTTON_H
#define MEDIUM_ABSTRACT_BUTTON_H
#include <string>
class AbstractButton {
virtual void click() = 0;
virtual std::string innerText() = 0;
// CounterButton.hpp
#ifndef MEDIUM_COUNTERBUTTON_HPP
#define MEDIUM_COUNTERBUTTON_HPP
#include <GUI/AbstractButton.hpp>
class CounterButton: public AbstractButton {
public:
CounterButton(): count(0) {
}
// main.cpp
#include <GUI/engine.hpp>
#include "CounterButton.hpp"
int main() {
// Instantiate the hypothetical engine
Engine engine();
// Instantiate the button
auto * counter = new CounterButton();
// Adds button to the view
engine.addButton(counter);
pub trait Button {
fn click(&mut self);
fn inner_text(&mut self) -> String;
}
pub struct CounterButton {
count: i32
}
impl Button for CounterButton {
// src/main.rs
mod button;
mod engine;
mod bindings;
use button::Button;
pub struct CounterButton {
count: i32
}
// src/engine.rs
use std::ffi::c_void;
use std::os::raw::c_char;
use crate::bindings::{engine_factory, register_button, AbstractButtonBinding};
use crate::Button;
pub struct Engine {
ptr: *mut c_void
}
// src/bindings.rs
#[link(name = "gui", kind = "static")]
extern "C" {
pub fn register_button(engine: *mut c_void, button: *mut c_void);
}
// bindings.hpp
extern "C" {
GUI::Engine *engine_factory() {
return new GUI::Engine();
}
}
// src/bindings.rs
use std::ffi::c_void;
#[link(name = "gui", kind = "static")]
extern "C" {
/// Return a void pointer from the heap, the alternative would have been to write recursively
/// all the fields in a #[repr(C)] struct Engine ...
pub fn engine_factory() -> *mut c_void;
}