Skip to content

Instantly share code, notes, and snippets.

@trimoq
trimoq / df_callback_sample.rs
Last active August 30, 2019 18:43
Callback with gtk-rs, capturing passed references
btn_load_file.connect_clicked( move | widget | {
  // …
});
@trimoq
trimoq / df_callback_sample_lines.rs
Created August 30, 2019 18:44
Callback with move, capturing window but also moving lines
btn_load_file.connect_clicked( clone!(
 lines, window => move | widget | {
 // ..
}));
@trimoq
trimoq / df_prefix_len.rs
Last active October 14, 2019 13:43
Calculate the length of the common prefix of two slices
/*
Calculats the length of the common pfix of two slices.
The slices a and b are zipped, leading to an iterator containing a tuple with a component of each of the input iterators
The iterators take care of handling different-sized slices (also slices of zero size)
*/
fn common_prefix_len(a: &str, b: &str) -> usize {
a.chars().zip(
b.chars()
).take_while(
|(x, y)| x == y
@trimoq
trimoq / clumsy_function.rs
Last active October 14, 2019 14:44
Parameter declarations can be clumy at times
// Parameter declarations can be clumy at times
pub fn handle_motion(
evt: &EventMotion,
fixed_container: &Fixed,
lines: RefMut<Vec<TextArea>>,
background_dimensions: RefMut<(u32, u32)>,
text_idx: RefMut<i32>,
){
...
}
// the internal function that is not exposed otherwise
fn generate_preview(image: &RgbaImage) -> RgbaImage {
// ...
}
// ...
#[cfg(test)] // this ensures that this module is only included if you build a `test` target
mod tests { // the tests live in their own module
// creates a mpsc-channel for thread communication.
// the type of the channel is inferred at compile-time
let (tx, rx) = mpsc::channel();
// (not shown) make the transmitter available to the other thread(s)
// ...
// start a new thread and capture the environment
thread::spawn(move || {
let mut lines = Vec::new();
// .. other variables omitted
fn main() {
// define the gtk application with a unique name and default parameters
let application = gtk::Application::new("The.name.goes.here", Default::default())
.expect("Initialization failed");
// this registers a closure (executing our setup_gui function)
//that has to be run on a `activate` event, triggered when the UI is loaded
application.connect_activate(move |app| {
setup_gui(app);
});
@trimoq
trimoq / rocket_cargo.toml
Created October 21, 2019 08:35
rocket_cargo_toml
[dependencies.rocket]
version = "0.4.2"
features = ["private-cookies"]
[dependencies.rocket_contrib]
default-features = false
features = ["json","diesel_postgres_pool","serve"]
@trimoq
trimoq / health.rs
Created October 21, 2019 08:43
minimalistic example
#[get("/health")]
fn health() -> &'static str {
"ok"
}
fn main() {
rocket::ignite()
.mount("/", routes![health])
.launch();
}
@trimoq
trimoq / tiny_hello.rs
Created October 21, 2019 08:55
Tiny hello world code
#[macro_use]
extern crate rocket;
#[get("/hello/<name>")]
fn hello(name: String) -> String {
format!("hello {}", name)
}
fn main() {
rocket::ignite()