Julio Biason
3 years ago
50 changed files with 2711 additions and 0 deletions
@ -0,0 +1,11 @@ |
|||||||
|
fn count(bits: usize) -> u32 { |
||||||
|
let num_bits = usize::BITS; |
||||||
|
num_bits as u32 - bits.leading_zeros() + 1 |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("{}", count(0b111)); |
||||||
|
println!("{}", count(0b1111_1111)); |
||||||
|
println!("{}", count(64)); // 1000000
|
||||||
|
println!("{}", 1 >> 1); |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
#[derive(Debug, Default)] |
||||||
|
struct Example { |
||||||
|
exists: Option<u8>, |
||||||
|
} |
||||||
|
|
||||||
|
fn main() { |
||||||
|
let e = Example::default(); |
||||||
|
println!("Herro: {:?}", e); |
||||||
|
} |
@ -0,0 +1 @@ |
|||||||
|
DATABASE_URL=postgres://consig:config@localhost/candles |
@ -0,0 +1,102 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "bitflags" |
||||||
|
version = "1.3.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "byteorder" |
||||||
|
version = "1.4.3" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "diesel" |
||||||
|
version = "1.4.8" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d" |
||||||
|
dependencies = [ |
||||||
|
"bitflags", |
||||||
|
"byteorder", |
||||||
|
"diesel_derives", |
||||||
|
"pq-sys", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "diesel_derives" |
||||||
|
version = "1.4.1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"syn", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "dieseltest" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"diesel", |
||||||
|
"dotenv", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "dotenv" |
||||||
|
version = "0.15.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "pq-sys" |
||||||
|
version = "0.4.6" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "6ac25eee5a0582f45a67e837e350d784e7003bd29a5f460796772061ca49ffda" |
||||||
|
dependencies = [ |
||||||
|
"vcpkg", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "proc-macro2" |
||||||
|
version = "1.0.32" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" |
||||||
|
dependencies = [ |
||||||
|
"unicode-xid", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "quote" |
||||||
|
version = "1.0.10" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "syn" |
||||||
|
version = "1.0.81" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"unicode-xid", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "unicode-xid" |
||||||
|
version = "0.2.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "vcpkg" |
||||||
|
version = "0.2.15" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" |
@ -0,0 +1,10 @@ |
|||||||
|
[package] |
||||||
|
name = "dieseltest" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
diesel = { version = "1.4.8", features = ["postgres"] } |
||||||
|
dotenv = "0.15.0" |
@ -0,0 +1,5 @@ |
|||||||
|
# For documentation on how to configure this file, |
||||||
|
# see diesel.rs/guides/configuring-diesel-cli |
||||||
|
|
||||||
|
[print_schema] |
||||||
|
file = "src/schema.rs" |
@ -0,0 +1,6 @@ |
|||||||
|
-- This file was automatically created by Diesel to setup helper functions |
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future |
||||||
|
-- changes will be added to existing projects as new migrations. |
||||||
|
|
||||||
|
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); |
||||||
|
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
@ -0,0 +1,36 @@ |
|||||||
|
-- This file was automatically created by Diesel to setup helper functions |
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future |
||||||
|
-- changes will be added to existing projects as new migrations. |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Sets up a trigger for the given table to automatically set a column called |
||||||
|
-- `updated_at` whenever the row is modified (unless `updated_at` was included |
||||||
|
-- in the modified columns) |
||||||
|
-- |
||||||
|
-- # Example |
||||||
|
-- |
||||||
|
-- ```sql |
||||||
|
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); |
||||||
|
-- |
||||||
|
-- SELECT diesel_manage_updated_at('users'); |
||||||
|
-- ``` |
||||||
|
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ |
||||||
|
BEGIN |
||||||
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s |
||||||
|
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); |
||||||
|
END; |
||||||
|
$$ LANGUAGE plpgsql; |
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ |
||||||
|
BEGIN |
||||||
|
IF ( |
||||||
|
NEW IS DISTINCT FROM OLD AND |
||||||
|
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at |
||||||
|
) THEN |
||||||
|
NEW.updated_at := current_timestamp; |
||||||
|
END IF; |
||||||
|
RETURN NEW; |
||||||
|
END; |
||||||
|
$$ LANGUAGE plpgsql; |
@ -0,0 +1,2 @@ |
|||||||
|
-- This file should undo anything in `up.sql` |
||||||
|
DROP TABLE assets; |
@ -0,0 +1,9 @@ |
|||||||
|
CREATE TABLE assets ( |
||||||
|
id SERIAL PRIMARY KEY, |
||||||
|
name TEXT NOT NULL, |
||||||
|
source TEXT NOT NULL |
||||||
|
-- extras? |
||||||
|
); |
||||||
|
CREATE INDEX source_idx on assets (source); |
||||||
|
|
||||||
|
SELECT diesel_manage_updated_at('assets'); |
@ -0,0 +1,8 @@ |
|||||||
|
use diesel::prelude::*; |
||||||
|
|
||||||
|
mod models; |
||||||
|
mod schema; |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("Hello, world!"); |
||||||
|
} |
@ -0,0 +1,7 @@ |
|||||||
|
use diesel::Queryable; |
||||||
|
|
||||||
|
#[derive(Queryable)] |
||||||
|
pub struct Asset { |
||||||
|
pub name: String, |
||||||
|
pub source: String, |
||||||
|
} |
@ -0,0 +1,7 @@ |
|||||||
|
table! { |
||||||
|
assets (id) { |
||||||
|
id -> Int4, |
||||||
|
name -> Text, |
||||||
|
source -> Text, |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,86 @@ |
|||||||
|
<!DOCTYPE html> |
||||||
|
<html> |
||||||
|
<head> |
||||||
|
</head> |
||||||
|
|
||||||
|
<body> |
||||||
|
<table> |
||||||
|
<thread> |
||||||
|
<tr> |
||||||
|
<th>Cell</th> |
||||||
|
<th>Cell</th> |
||||||
|
<th>Cell</th> |
||||||
|
<th>Cell</th> |
||||||
|
</tr> |
||||||
|
</thead> |
||||||
|
|
||||||
|
<tbody> |
||||||
|
<tr draggable="true" class="dropzone"> |
||||||
|
<td draggable="false">1</td> |
||||||
|
<td draggable="false">1</td> |
||||||
|
<td draggable="false">1</td> |
||||||
|
<td draggable="false">1</td> |
||||||
|
</tr> |
||||||
|
<tr draggable="true" class="dropzone"> |
||||||
|
<td draggable="false">2</td> |
||||||
|
<td draggable="false">2</td> |
||||||
|
<td draggable="false">2</td> |
||||||
|
<td draggable="false">2</td> |
||||||
|
</tr> |
||||||
|
<tr draggable="true" class="dropzone"> |
||||||
|
<td draggable="false">3</td> |
||||||
|
<td draggable="false">3</td> |
||||||
|
<td draggable="false">3</td> |
||||||
|
<td draggable="false">3</td> |
||||||
|
</tr> |
||||||
|
</tbody> |
||||||
|
</table> |
||||||
|
</body> |
||||||
|
|
||||||
|
<script> |
||||||
|
(function() { |
||||||
|
let dragged = null; |
||||||
|
|
||||||
|
document.addEventListener("dragstart", function(event) { |
||||||
|
dragged = event.target; |
||||||
|
event.target.style.opacity = .5; |
||||||
|
}); |
||||||
|
|
||||||
|
document.addEventListener("dragenter", function(event) { |
||||||
|
const parent = event.target.parentNode; |
||||||
|
if (parent.className == "dropzone") { |
||||||
|
event.target.parentNode.style.background = "#7f7f7f"; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
document.addEventListener("dragleave", function(event) { |
||||||
|
const parent = event.target.parentNode; |
||||||
|
if (parent.className == "dropzone") { |
||||||
|
event.target.parentNode.style.background = "#ffffff"; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
document.addEventListener("dragover", function(event) { |
||||||
|
// Necessário para que o "drop" funcione. |
||||||
|
event.preventDefault(); |
||||||
|
}); |
||||||
|
|
||||||
|
document.addEventListener("drop", function(event) { |
||||||
|
dragged.style.opacity = 1; |
||||||
|
event.preventDefault(); |
||||||
|
const row = event.target.parentNode; |
||||||
|
const table = row.parentNode; |
||||||
|
if (row.className == "dropzone") { |
||||||
|
row.style.background = "#ffffff"; |
||||||
|
const movement = dragged.compareDocumentPosition(row); |
||||||
|
console.log('Movement: ', movement); |
||||||
|
if (movement === Node.DOCUMENT_POSITION_PRECEDING) { |
||||||
|
table.insertBefore(dragged, row); |
||||||
|
} else { |
||||||
|
table.insertBefore(dragged, row.nextSibling); |
||||||
|
} |
||||||
|
} |
||||||
|
}); |
||||||
|
})(); |
||||||
|
</script> |
||||||
|
</html> |
@ -0,0 +1,13 @@ |
|||||||
|
use std::env; |
||||||
|
use std::path::Path; |
||||||
|
|
||||||
|
fn main() { |
||||||
|
let execution_name = env::args().nth(0).unwrap_or("???".to_string()); |
||||||
|
let parser = Path::new(&execution_name) |
||||||
|
.file_name() |
||||||
|
.map(|x| x.to_str()) |
||||||
|
.flatten() |
||||||
|
.unwrap_or("???"); |
||||||
|
println!("I am {}", execution_name); |
||||||
|
println!("And the name is {}", parser); |
||||||
|
} |
@ -0,0 +1,358 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "aho-corasick" |
||||||
|
version = "0.7.18" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" |
||||||
|
dependencies = [ |
||||||
|
"memchr", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "atty" |
||||||
|
version = "0.2.14" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" |
||||||
|
dependencies = [ |
||||||
|
"hermit-abi", |
||||||
|
"libc", |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "autocfg" |
||||||
|
version = "1.0.1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "bincode" |
||||||
|
version = "1.3.3" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" |
||||||
|
dependencies = [ |
||||||
|
"serde", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "bytes" |
||||||
|
version = "1.1.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "cfg-if" |
||||||
|
version = "1.0.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "chrono" |
||||||
|
version = "0.4.19" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73" |
||||||
|
dependencies = [ |
||||||
|
"libc", |
||||||
|
"num-integer", |
||||||
|
"num-traits", |
||||||
|
"serde", |
||||||
|
"time", |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "env_logger" |
||||||
|
version = "0.9.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" |
||||||
|
dependencies = [ |
||||||
|
"atty", |
||||||
|
"humantime", |
||||||
|
"log", |
||||||
|
"regex", |
||||||
|
"termcolor", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "hermit-abi" |
||||||
|
version = "0.1.19" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" |
||||||
|
dependencies = [ |
||||||
|
"libc", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "humantime" |
||||||
|
version = "2.1.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "libc" |
||||||
|
version = "0.2.107" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "log" |
||||||
|
version = "0.4.14" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" |
||||||
|
dependencies = [ |
||||||
|
"cfg-if", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "memchr" |
||||||
|
version = "2.4.1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "mio" |
||||||
|
version = "0.7.14" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" |
||||||
|
dependencies = [ |
||||||
|
"libc", |
||||||
|
"log", |
||||||
|
"miow", |
||||||
|
"ntapi", |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "miow" |
||||||
|
version = "0.3.7" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21" |
||||||
|
dependencies = [ |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "ntapi" |
||||||
|
version = "0.3.6" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" |
||||||
|
dependencies = [ |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "num-integer" |
||||||
|
version = "0.1.44" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" |
||||||
|
dependencies = [ |
||||||
|
"autocfg", |
||||||
|
"num-traits", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "num-traits" |
||||||
|
version = "0.2.14" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" |
||||||
|
dependencies = [ |
||||||
|
"autocfg", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "pin-project-lite" |
||||||
|
version = "0.2.7" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "proc-macro2" |
||||||
|
version = "1.0.32" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43" |
||||||
|
dependencies = [ |
||||||
|
"unicode-xid", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "quote" |
||||||
|
version = "1.0.10" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "regex" |
||||||
|
version = "1.5.4" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" |
||||||
|
dependencies = [ |
||||||
|
"aho-corasick", |
||||||
|
"memchr", |
||||||
|
"regex-syntax", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "regex-syntax" |
||||||
|
version = "0.6.25" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "serde" |
||||||
|
version = "1.0.130" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" |
||||||
|
dependencies = [ |
||||||
|
"serde_derive", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "serde_derive" |
||||||
|
version = "1.0.130" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"syn", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "server" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"bincode", |
||||||
|
"chrono", |
||||||
|
"env_logger", |
||||||
|
"log", |
||||||
|
"serde", |
||||||
|
"shared", |
||||||
|
"tokio", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "shared" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"chrono", |
||||||
|
"serde", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "syn" |
||||||
|
version = "1.0.81" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "f2afee18b8beb5a596ecb4a2dce128c719b4ba399d34126b9e4396e3f9860966" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"unicode-xid", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "termcolor" |
||||||
|
version = "1.1.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" |
||||||
|
dependencies = [ |
||||||
|
"winapi-util", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "time" |
||||||
|
version = "0.1.44" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" |
||||||
|
dependencies = [ |
||||||
|
"libc", |
||||||
|
"wasi", |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "tokio" |
||||||
|
version = "1.14.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "70e992e41e0d2fb9f755b37446f20900f64446ef54874f40a60c78f021ac6144" |
||||||
|
dependencies = [ |
||||||
|
"autocfg", |
||||||
|
"bytes", |
||||||
|
"libc", |
||||||
|
"memchr", |
||||||
|
"mio", |
||||||
|
"pin-project-lite", |
||||||
|
"tokio-macros", |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "tokio-macros" |
||||||
|
version = "1.6.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "c9efc1aba077437943f7515666aa2b882dfabfbfdf89c819ea75a8d6e9eaba5e" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"syn", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "txtclient" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"bincode", |
||||||
|
"env_logger", |
||||||
|
"log", |
||||||
|
"serde", |
||||||
|
"shared", |
||||||
|
"tokio", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "unicode-xid" |
||||||
|
version = "0.2.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "wasi" |
||||||
|
version = "0.10.0+wasi-snapshot-preview1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi" |
||||||
|
version = "0.3.9" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" |
||||||
|
dependencies = [ |
||||||
|
"winapi-i686-pc-windows-gnu", |
||||||
|
"winapi-x86_64-pc-windows-gnu", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi-i686-pc-windows-gnu" |
||||||
|
version = "0.4.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi-util" |
||||||
|
version = "0.1.5" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" |
||||||
|
dependencies = [ |
||||||
|
"winapi", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "winapi-x86_64-pc-windows-gnu" |
||||||
|
version = "0.4.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" |
@ -0,0 +1,9 @@ |
|||||||
|
[workspace] |
||||||
|
members = [ |
||||||
|
"shared", |
||||||
|
"server", |
||||||
|
"txtclient", |
||||||
|
] |
||||||
|
|
||||||
|
[profile.release] |
||||||
|
lto = true |
@ -0,0 +1,5 @@ |
|||||||
|
Stupid microservices architecture. |
||||||
|
|
||||||
|
What I wanted was to make a pubsub-like architecture, in which the clients, |
||||||
|
once connected, receive the data, and we could plug as many clients as we |
||||||
|
need. |
@ -0,0 +1,15 @@ |
|||||||
|
[package] |
||||||
|
name = "server" |
||||||
|
description = "A server that keeps producing content and sends them to connected clients" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
bincode = "1.3.3" |
||||||
|
chrono = { version = "0.4.19", features = ["serde"] } |
||||||
|
env_logger = "0.9.0" |
||||||
|
log = "0.4.14" |
||||||
|
serde = { version = "1.0.130", features = ["derive"] } |
||||||
|
tokio = { version = "1.14.0", features = ["rt", "net", "macros", "sync", "time", "io-util"] } |
||||||
|
|
||||||
|
shared = { path = "../shared" } |
@ -0,0 +1,65 @@ |
|||||||
|
use std::net::SocketAddr; |
||||||
|
use tokio::io::AsyncWriteExt; |
||||||
|
use tokio::net::TcpListener; |
||||||
|
use tokio::net::TcpStream; |
||||||
|
use tokio::sync::broadcast; |
||||||
|
use tokio::time::sleep; |
||||||
|
use tokio::time::Duration; |
||||||
|
|
||||||
|
use shared::Message; |
||||||
|
|
||||||
|
#[tokio::main(flavor = "current_thread")] |
||||||
|
async fn main() { |
||||||
|
env_logger::init(); |
||||||
|
let (tx, rx) = broadcast::channel::<Message>(30); |
||||||
|
|
||||||
|
tokio::join!(consumer(rx), producer(tx.clone()), listener(tx)); |
||||||
|
} |
||||||
|
|
||||||
|
/// Produtor de conteúdo
|
||||||
|
async fn producer(sink: broadcast::Sender<Message>) { |
||||||
|
let messages = ["Hello world", "Goodbye Earth", "See you later alligator"]; |
||||||
|
for message in messages.iter().cycle() { |
||||||
|
match sink.send(Message::new(message)) { |
||||||
|
Ok(_) => {} // log::debug!("Send: {}", message),
|
||||||
|
Err(err) => log::error!("Error sending message: {:?}", err), |
||||||
|
} |
||||||
|
// Como o sink não tem await, o runtime não consegue escutar pelas portas
|
||||||
|
sleep(Duration::from_millis(1)).await; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// Aguada conexões de clientes
|
||||||
|
async fn listener(connector: broadcast::Sender<Message>) { |
||||||
|
let listener = TcpListener::bind("127.0.0.1:4435") |
||||||
|
.await |
||||||
|
.expect("Failed binding"); |
||||||
|
loop { |
||||||
|
if let Ok((socket, addr)) = listener.accept().await { |
||||||
|
log::debug!("Got new client: {:?}", addr); |
||||||
|
let new_receiver = connector.subscribe(); |
||||||
|
tokio::spawn(async move { worker(new_receiver, socket, addr).await }); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// Worker que envia as mensagens para os clientes
|
||||||
|
async fn worker(mut source: broadcast::Receiver<Message>, mut stream: TcpStream, addr: SocketAddr) { |
||||||
|
loop { |
||||||
|
let message = source.recv().await.expect("No message"); |
||||||
|
log::debug!("Sending {:?} to {:?}", message, addr); |
||||||
|
stream |
||||||
|
.write_all(&bincode::serialize(&message).unwrap()) |
||||||
|
.await |
||||||
|
.expect("Failed to send"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// Consumidor interno
|
||||||
|
/// Essa função existe porque o sender do broadcast precisa de pelo menos um listener.
|
||||||
|
async fn consumer(mut receiver: broadcast::Receiver<Message>) { |
||||||
|
loop { |
||||||
|
let _message = receiver.recv().await.expect("Invalid message"); |
||||||
|
// log::debug!("Message: {:?}", message);
|
||||||
|
} |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
[package] |
||||||
|
name = "shared" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
chrono = { version = "0.4.19", features = ["serde"] } |
||||||
|
serde = { version = "1.0.130", features = ["derive"] } |
@ -0,0 +1,19 @@ |
|||||||
|
use chrono::offset::Utc; |
||||||
|
use chrono::DateTime; |
||||||
|
use serde::Deserialize; |
||||||
|
use serde::Serialize; |
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)] |
||||||
|
pub struct Message { |
||||||
|
received: DateTime<Utc>, |
||||||
|
message: String, |
||||||
|
} |
||||||
|
|
||||||
|
impl Message { |
||||||
|
pub fn new(message: &str) -> Self { |
||||||
|
Self { |
||||||
|
received: Utc::now(), |
||||||
|
message: message.into(), |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,16 @@ |
|||||||
|
[package] |
||||||
|
name = "txtclient" |
||||||
|
description = "A client that keeps listened to whatever the server is producing and shows on stdout" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
bincode = "1.3.3" |
||||||
|
env_logger = "0.9.0" |
||||||
|
log = "0.4.14" |
||||||
|
serde = { version = "1.0.130", features = ["derive"] } |
||||||
|
tokio = { version = "1.14.0", features = ["rt", "net", "macros", "io-util"] } |
||||||
|
|
||||||
|
shared = { path = "../shared" } |
@ -0,0 +1,20 @@ |
|||||||
|
use tokio::io::AsyncReadExt; |
||||||
|
use tokio::net::TcpStream; |
||||||
|
|
||||||
|
use shared::Message; |
||||||
|
|
||||||
|
#[tokio::main(flavor = "current_thread")] |
||||||
|
async fn main() { |
||||||
|
env_logger::init(); |
||||||
|
|
||||||
|
let mut stream = TcpStream::connect("127.0.0.1:4435") |
||||||
|
.await |
||||||
|
.expect("Failed to connect to server"); |
||||||
|
loop { |
||||||
|
let mut buffer = [0; 1024]; |
||||||
|
let bytes = stream.read(&mut buffer).await.expect("Failed to read data"); |
||||||
|
let decoded: Message = |
||||||
|
bincode::deserialize(&buffer[..bytes]).expect("Failed to convert message"); |
||||||
|
log::info!("Got {:?}", decoded); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,69 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "cc" |
||||||
|
version = "1.0.72" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "client" |
||||||
|
version = "0.1.0" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "cmake" |
||||||
|
version = "0.1.48" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "e8ad8cef104ac57b68b89df3208164d228503abbdce70f6880ffa3d970e7443a" |
||||||
|
dependencies = [ |
||||||
|
"cc", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "gcc" |
||||||
|
version = "0.3.55" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "libc" |
||||||
|
version = "0.2.113" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "eef78b64d87775463c549fbd80e19249ef436ea3bf1de2a1eb7e717ec7fab1e9" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "nanomsg" |
||||||
|
version = "0.7.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "617e0160fba522f8667df7bc79f3b4a74a0e3968d08023ebb3ce717a5f3bd3ac" |
||||||
|
dependencies = [ |
||||||
|
"libc", |
||||||
|
"nanomsg-sys", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "nanomsg-sys" |
||||||
|
version = "0.7.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "78aa3ccb6d007dfecb4f7070725c4b1670a87677babb6621cb0c8cce9cfdc004" |
||||||
|
dependencies = [ |
||||||
|
"cmake", |
||||||
|
"gcc", |
||||||
|
"libc", |
||||||
|
"pkg-config", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "pkg-config" |
||||||
|
version = "0.3.24" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "58893f751c9b0412871a09abd62ecd2a00298c6c83befa223ef98c52aef40cbe" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "server" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"nanomsg", |
||||||
|
] |
@ -0,0 +1,8 @@ |
|||||||
|
[package] |
||||||
|
name = "client" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
@ -0,0 +1,9 @@ |
|||||||
|
[package] |
||||||
|
name = "server" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
nanomsg = "0.7.2" |
@ -0,0 +1,8 @@ |
|||||||
|
use nanomsg::Protocol; |
||||||
|
use nanomsg::Socket; |
||||||
|
|
||||||
|
fn main() { |
||||||
|
println!("Starting server..."); |
||||||
|
|
||||||
|
let mut socket = Socket::new(Protocol::Sub).unwrap(); |
||||||
|
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,13 @@ |
|||||||
|
[package] |
||||||
|
name = "notifytest" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
clap = { version = "3.0.13", features = ["derive", "env"] } |
||||||
|
env_logger = "0.9.0" |
||||||
|
log = "0.4.14" |
||||||
|
sqlx = { version = "0.5.10", features = ["runtime-tokio-rustls", "postgres", "migrate", "macros"] } |
||||||
|
tokio = { version = "1.16.1", features = ["rt", "net", "macros", "io-util"] } |
@ -0,0 +1,3 @@ |
|||||||
|
# NotifyTest |
||||||
|
|
||||||
|
Testing Postgres notification features |
@ -0,0 +1,50 @@ |
|||||||
|
CREATE TABLE targets ( |
||||||
|
level_1 TEXT, |
||||||
|
level_2 TEXT, |
||||||
|
channel_name TEXT, |
||||||
|
UNIQUE (level_1, level_2) |
||||||
|
); |
||||||
|
|
||||||
|
CREATE TABLE commands ( |
||||||
|
id SERIAL PRIMARY KEY, |
||||||
|
level_1 TEXT, |
||||||
|
level_2 TEXT, |
||||||
|
payload TEXT, |
||||||
|
channel_name TEXT, |
||||||
|
channel_failure BOOLEAN |
||||||
|
); |
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION notify_channel() |
||||||
|
RETURNS TRIGGER |
||||||
|
LANGUAGE PLPGSQL |
||||||
|
AS |
||||||
|
$$ |
||||||
|
DECLARE |
||||||
|
target_channel TEXT; |
||||||
|
BEGIN |
||||||
|
SELECT channel_name |
||||||
|
INTO target_channel |
||||||
|
FROM targets |
||||||
|
WHERE level_1 = NEW.level_1 |
||||||
|
AND level_2 = NEW.level_2; |
||||||
|
|
||||||
|
if found then |
||||||
|
SELECT pg_notify(target_channel, NEW.playload); |
||||||
|
UPDATE commands |
||||||
|
SET channel_name = channel_name, channel_failure = false |
||||||
|
WHERE id = NEW.id; |
||||||
|
else |
||||||
|
UPDATE commands |
||||||
|
SET channel_failure = true |
||||||
|
WHERE id = NEW.id; |
||||||
|
end if; |
||||||
|
|
||||||
|
RETURN NEW; |
||||||
|
END; |
||||||
|
$$; |
||||||
|
|
||||||
|
CREATE TRIGGER notify_channel |
||||||
|
AFTER INSERT |
||||||
|
ON commands |
||||||
|
FOR EACH ROW |
||||||
|
EXECUTE PROCEDURE notify_channel(); |
@ -0,0 +1,29 @@ |
|||||||
|
CREATE OR REPLACE FUNCTION notify_channel() |
||||||
|
RETURNS TRIGGER |
||||||
|
LANGUAGE PLPGSQL |
||||||
|
AS |
||||||
|
$$ |
||||||
|
DECLARE |
||||||
|
target_channel TEXT; |
||||||
|
BEGIN |
||||||
|
SELECT channel_name |
||||||
|
INTO target_channel |
||||||
|
FROM targets |
||||||
|
WHERE level_1 = NEW.level_1 |
||||||
|
AND level_2 = NEW.level_2; |
||||||
|
|
||||||
|
if found then |
||||||
|
SELECT pg_notify(target_channel, NEW.payload); |
||||||
|
UPDATE commands |
||||||
|
SET channel_name = channel_name, channel_failure = false |
||||||
|
WHERE id = NEW.id; |
||||||
|
else |
||||||
|
UPDATE commands |
||||||
|
SET channel_failure = true |
||||||
|
WHERE id = NEW.id; |
||||||
|
end if; |
||||||
|
|
||||||
|
RETURN NEW; |
||||||
|
END; |
||||||
|
$$; |
||||||
|
|
@ -0,0 +1,28 @@ |
|||||||
|
CREATE OR REPLACE FUNCTION notify_channel() |
||||||
|
RETURNS TRIGGER |
||||||
|
LANGUAGE PLPGSQL |
||||||
|
AS |
||||||
|
$$ |
||||||
|
DECLARE |
||||||
|
target_channel TEXT; |
||||||
|
BEGIN |
||||||
|
SELECT channel_name |
||||||
|
INTO target_channel |
||||||
|
FROM targets |
||||||
|
WHERE level_1 = NEW.level_1 |
||||||
|
AND level_2 = NEW.level_2; |
||||||
|
|
||||||
|
if found then |
||||||
|
SELECT pg_notify(target_channel, NEW.payload); |
||||||
|
UPDATE commands |
||||||
|
SET channel_name = channel_name, channel_failure = false |
||||||
|
WHERE id = NEW.id; |
||||||
|
else |
||||||
|
UPDATE commands |
||||||
|
SET channel_failure = true |
||||||
|
WHERE id = NEW.id; |
||||||
|
end if; |
||||||
|
END; |
||||||
|
$$; |
||||||
|
|
||||||
|
|
@ -0,0 +1,26 @@ |
|||||||
|
CREATE OR REPLACE FUNCTION notify_channel() |
||||||
|
RETURNS TRIGGER |
||||||
|
LANGUAGE PLPGSQL |
||||||
|
AS |
||||||
|
$$ |
||||||
|
DECLARE |
||||||
|
target_channel TEXT; |
||||||
|
BEGIN |
||||||
|
SELECT channel_name |
||||||
|
INTO target_channel |
||||||
|
FROM targets |
||||||
|
WHERE level_1 = NEW.level_1 |
||||||
|
AND level_2 = NEW.level_2; |
||||||
|
|
||||||
|
if found then |
||||||
|
PERFORM pg_notify(target_channel, NEW.payload); |
||||||
|
UPDATE commands |
||||||
|
SET channel_name = channel_name, channel_failure = false |
||||||
|
WHERE id = NEW.id; |
||||||
|
else |
||||||
|
UPDATE commands |
||||||
|
SET channel_failure = true |
||||||
|
WHERE id = NEW.id; |
||||||
|
end if; |
||||||
|
END; |
||||||
|
$$; |
@ -0,0 +1,29 @@ |
|||||||
|
CREATE OR REPLACE FUNCTION notify_channel() |
||||||
|
RETURNS TRIGGER |
||||||
|
LANGUAGE PLPGSQL |
||||||
|
AS |
||||||
|
$$ |
||||||
|
DECLARE |
||||||
|
target_channel TEXT; |
||||||
|
BEGIN |
||||||
|
SELECT channel_name |
||||||
|
INTO target_channel |
||||||
|
FROM targets |
||||||
|
WHERE level_1 = NEW.level_1 |
||||||
|
AND level_2 = NEW.level_2; |
||||||
|
|
||||||
|
if found then |
||||||
|
PERFORM pg_notify(target_channel, NEW.payload); |
||||||
|
UPDATE commands |
||||||
|
SET channel_name = channel_name, channel_failure = false |
||||||
|
WHERE id = NEW.id; |
||||||
|
else |
||||||
|
UPDATE commands |
||||||
|
SET channel_failure = true |
||||||
|
WHERE id = NEW.id; |
||||||
|
end if; |
||||||
|
|
||||||
|
RETURN NEW; |
||||||
|
END; |
||||||
|
$$; |
||||||
|
|
@ -0,0 +1,57 @@ |
|||||||
|
use clap::Parser; |
||||||
|
use sqlx::postgres::PgListener; |
||||||
|
use sqlx::postgres::PgPoolOptions; |
||||||
|
use sqlx::postgres::Postgres; |
||||||
|
use sqlx::Pool; |
||||||
|
|
||||||
|
async fn connect(url: &str) -> Pool<Postgres> { |
||||||
|
PgPoolOptions::new() |
||||||
|
.max_connections(3) |
||||||
|
.connect(url) |
||||||
|
.await |
||||||
|
.expect("Failed to connect to the database") |
||||||
|
} |
||||||
|
|
||||||
|
async fn create_listener(channel: &str, url: &str) { |
||||||
|
let mut listener = PgListener::connect(&url) |
||||||
|
.await |
||||||
|
.expect("Failed to connect listener {channel}"); |
||||||
|
listener |
||||||
|
.listen(channel) |
||||||
|
.await |
||||||
|
.expect("Failed to listen to channel {channel}"); |
||||||
|
|
||||||
|
log::info!("Listener on {channel} ready..."); |
||||||
|
loop { |
||||||
|
let notification = listener |
||||||
|
.recv() |
||||||
|
.await |
||||||
|
.expect("Failed to receive data from channel {channel}"); |
||||||
|
log::info!("Notification: {:?}", notification); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
#[derive(Parser)] |
||||||
|
#[clap(version, about)] |
||||||
|
struct Args { |
||||||
|
/// URL for the database connection.
|
||||||
|
#[clap(short, long, env = "DATABASE_URL")] |
||||||
|
database: String, |
||||||
|
} |
||||||
|
|
||||||
|
#[tokio::main] |
||||||
|
async fn main() { |
||||||
|
env_logger::init(); |
||||||
|
let args = Args::parse(); |
||||||
|
let pool = connect(&args.database).await; |
||||||
|
sqlx::migrate!() |
||||||
|
.run(&pool) |
||||||
|
.await |
||||||
|
.expect("Migration failure"); |
||||||
|
|
||||||
|
let listener_type_1 = create_listener("type_1", &args.database); |
||||||
|
let listener_type_2 = create_listener("type_2", &args.database); |
||||||
|
let listener_type_3 = create_listener("type_3", &args.database); |
||||||
|
|
||||||
|
tokio::join!(listener_type_1, listener_type_2, listener_type_3); |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
//! Test if one can create a generic and a specific impl for the same trait.
|
||||||
|
|
||||||
|
trait SomeTrait { |
||||||
|
fn say(&self) -> String; |
||||||
|
} |
||||||
|
|
||||||
|
struct SomeStruct<T> {} |
||||||
|
|
||||||
|
impl<T> for SomeStruct<T> |
||||||
|
where T: SomeTrait |
@ -0,0 +1,16 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "spmc" |
||||||
|
version = "0.3.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "02a8428da277a8e3a15271d79943e80ccc2ef254e78813a166a08d65e4c3ece5" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "spmctest" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"spmc", |
||||||
|
] |
@ -0,0 +1,7 @@ |
|||||||
|
[package] |
||||||
|
name = "spmctest" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
spmc = "0.3" |
@ -0,0 +1,24 @@ |
|||||||
|
use std::thread; |
||||||
|
|
||||||
|
fn main() { |
||||||
|
let (mut tx, rx) = spmc::channel(); |
||||||
|
|
||||||
|
let mut handles = Vec::new(); |
||||||
|
for n in 0..5 { |
||||||
|
let rx = rx.clone(); |
||||||
|
handles.push(thread::spawn(move || { |
||||||
|
while let Ok(msg) = rx.recv() { |
||||||
|
// println!("Hello");
|
||||||
|
println!("worker {} recvd: {}", n, msg); |
||||||
|
} |
||||||
|
})); |
||||||
|
} |
||||||
|
|
||||||
|
for i in 0..15 { |
||||||
|
tx.send(i).unwrap(); |
||||||
|
} |
||||||
|
|
||||||
|
for handle in handles { |
||||||
|
handle.join().unwrap(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
fn main() { |
||||||
|
let phrase = std::env::args().skip(1).collect::<Vec<String>>().join(" "); |
||||||
|
let mapper = [ |
||||||
|
Box::new(|x: char| x.to_uppercase()), |
||||||
|
Box::new(|x: char| x.to_lowercase()), |
||||||
|
] |
||||||
|
.iter() |
||||||
|
.cycle(); |
||||||
|
println!("{}", phrase.chars().map(mapper.next()).collect().join("")); |
||||||
|
} |
@ -0,0 +1,98 @@ |
|||||||
|
# This file is automatically @generated by Cargo. |
||||||
|
# It is not intended for manual editing. |
||||||
|
version = 3 |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "hermit-abi" |
||||||
|
version = "0.1.19" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" |
||||||
|
dependencies = [ |
||||||
|
"libc", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "libc" |
||||||
|
version = "0.2.117" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "num_cpus" |
||||||
|
version = "1.13.1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1" |
||||||
|
dependencies = [ |
||||||
|
"hermit-abi", |
||||||
|
"libc", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "pin-project-lite" |
||||||
|
version = "0.2.8" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "e280fbe77cc62c91527259e9442153f4688736748d24660126286329742b4c6c" |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "proc-macro2" |
||||||
|
version = "1.0.36" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029" |
||||||
|
dependencies = [ |
||||||
|
"unicode-xid", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "quote" |
||||||
|
version = "1.0.15" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "syn" |
||||||
|
version = "1.0.86" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"unicode-xid", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "tokio" |
||||||
|
version = "1.16.1" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "0c27a64b625de6d309e8c57716ba93021dccf1b3b5c97edd6d3dd2d2135afc0a" |
||||||
|
dependencies = [ |
||||||
|
"num_cpus", |
||||||
|
"pin-project-lite", |
||||||
|
"tokio-macros", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "tokio-macros" |
||||||
|
version = "1.7.0" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" |
||||||
|
dependencies = [ |
||||||
|
"proc-macro2", |
||||||
|
"quote", |
||||||
|
"syn", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "tokioselect" |
||||||
|
version = "0.1.0" |
||||||
|
dependencies = [ |
||||||
|
"tokio", |
||||||
|
] |
||||||
|
|
||||||
|
[[package]] |
||||||
|
name = "unicode-xid" |
||||||
|
version = "0.2.2" |
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index" |
||||||
|
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" |
@ -0,0 +1,7 @@ |
|||||||
|
[package] |
||||||
|
name = "tokioselect" |
||||||
|
version = "0.1.0" |
||||||
|
edition = "2021" |
||||||
|
|
||||||
|
[dependencies] |
||||||
|
tokio = { version = "1.16.1", features = ["rt", "rt-multi-thread", "macros", "time", "sync"] } |
@ -0,0 +1,46 @@ |
|||||||
|
use tokio::sync::mpsc::channel; |
||||||
|
use tokio::time::sleep; |
||||||
|
use tokio::time::Duration; |
||||||
|
|
||||||
|
#[tokio::main] |
||||||
|
async fn main() { |
||||||
|
let (tx1, mut rx1) = channel(100); |
||||||
|
let (tx2, mut rx2) = channel(100); |
||||||
|
|
||||||
|
tokio::spawn(async move { |
||||||
|
println!("I'm worker 1"); |
||||||
|
let mut loop_id = 1; |
||||||
|
let sleep_time = Duration::from_millis(200); |
||||||
|
loop { |
||||||
|
let message = format!("This is loop 1-{loop_id}"); |
||||||
|
tx1.send(message).await.unwrap(); |
||||||
|
sleep(sleep_time).await; |
||||||
|
loop_id += 1; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
tokio::spawn(async move { |
||||||
|
println!("I'm worker 2"); |
||||||
|
let mut loop_id = 1; |
||||||
|
let sleep_time = Duration::from_millis(300); |
||||||
|
loop { |
||||||
|
let message = format!("This is loop 2-{loop_id}"); |
||||||
|
tx2.send(message).await.unwrap(); |
||||||
|
sleep(sleep_time).await; |
||||||
|
loop_id += 1; |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
// This is main
|
||||||
|
loop { |
||||||
|
tokio::select! { |
||||||
|
Some(msg) = rx1.recv() => { |
||||||
|
println!("Worker 1 said \"{}\"", msg); |
||||||
|
} |
||||||
|
Some(msg) = rx2.recv() => { |
||||||
|
println!("Worker 2 said \"{}\"", msg); |
||||||
|
} |
||||||
|
else => { break } |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,9 @@ |
|||||||
|
fn main() { |
||||||
|
let data = [0b1000_0001u8, 0b0000_0010, 0b1000_0011]; |
||||||
|
let mut main_iter = data.iter(); |
||||||
|
let mut simple_iter = main_iter.take_while(|x| *x & 0b1000_000 != 0b1000_0000); |
||||||
|
println!("{:?}", simple_iter); |
||||||
|
|
||||||
|
let mut next_iter = main_iter.take_while(|x| *x & 0b1000_000 != 0b1000_0000); |
||||||
|
println!("{:?}", next_iter); |
||||||
|
} |
Loading…
Reference in new issue