Skip to content

Instantly share code, notes, and snippets.

View felipesere's full-sized avatar
🦀
Doing Rust stuff

Felipe Seré felipesere

🦀
Doing Rust stuff
View GitHub Profile
@felipesere
felipesere / many_futures.rs
Created December 26, 2019 23:18
attempt at turning Vec of Futures (?) into a stream that will yield all items
use std::pin::Pin;
use pin_project_lite::pin_project;
use crate::stream::Stream;
use crate::task::{Context, Poll};
use crate::future::Future;
pin_project! {
/// A stream that was created from a bunch of futures.
#[derive(Debug, AsExpression)]
#[sql_type = "Text"]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum RepoEvents {
LatestCommitOnMaster(Commit),
}
impl ToSql<Text, Sqlite> for RepoEvents {
fn to_sql<W: Write>(&self, out: &mut Output<W, Sqlite>) -> serialize::Result {
use RepoEvents::*;
@felipesere
felipesere / sample.rs
Last active December 19, 2019 19:45
serde to to turn struct to JSON and insert into text
table! {
repo_activity_log (id) {
id -> Integer,
repo_id -> Integer,
event -> Text,
created_at -> Timestamp,
}
}
@felipesere
felipesere / static_files.rs
Created December 2, 2019 14:08
Trying to server static files from a subfolder that is given as part of a struct that implements `Endpoint`
pub struct StaticFilesV2 {
pub root: String,
}
pub(crate) type BoxFuture<'a, T> = std::pin::Pin<Box<dyn Future<Output = T> + Send + 'a>>;
impl <STATE: Send + Sync + 'static> Endpoint<STATE> for StaticFilesV2 {
type Fut = BoxFuture<'static, Response>;
fn call(&self, req: Request<STATE>) -> Self::Fut {
let protected_request = Arc::new(Mutex::new( (req, self.root.clone()) ));
@felipesere
felipesere / example.rs
Created September 29, 2019 13:49
Error I get when combining clap, runtime, surf, and async/await
error[E0277]: `std::rc::Rc<(dyn for<'r> std::ops::Fn(&'r std::ffi::OsStr) -> std::result::Result<(), std::ffi::OsString> + 'static)>` cannot be sent between threads safely
--> src/main.rs:115:1
|
115 | #[runtime::main]
| ^^^^^^^^^^^^^^^^ `std::rc::Rc<(dyn for<'r> std::ops::Fn(&'r std::ffi::OsStr) -> std::result::Result<(), std::ffi::OsString> + 'static)>` cannot be sent between threads safely
|
::: /Users/felipesere/.cargo/registry/src/github.com-1ecc6299db9ec823/runtime-raw-0.3.0-alpha.5/src/lib.rs:60:29
|
60 | F: Future<Output = T> + Send + 'static,
| ---- required by this bound in `runtime_raw::enter`
Folder structure:
Root
|--A
| |--a1.spec
| |--a2.spec
| |--a3.spec
|
|--B
| |--b1.spec
@felipesere
felipesere / questions.md
Last active July 8, 2018 16:32
Programmable Getter in Java 6

"Programmable" getter in Java 6.

The get(...) method takes a Question and evaluates it. Questions have a bunch of "modern" chainable methods like:

  • refineWith(Question q) or then (Question q)
  • or(Question) and orAsk(Question q) and orAsk(otherSubject, Question q) for a matching answer type
  • orDefaultTo(answer) in case of exceptions or null
@felipesere
felipesere / prepare-commit-msg
Created October 29, 2017 21:26
A script that prepends elm or ex depending on what gets commited
#!/usr/bin/env ruby
require 'set'
message = ARGV[0] # the file that contains the message (change this!)
source = ARGV[1] # can be template, merge, squash, commit
commit_id = ARGV[2]
if ["merge","squash"].include?(source)
return 1 # do nothing
@felipesere
felipesere / raw-sql-diesel-rust.rs
Created April 13, 2017 11:34
Error I get when using raw sql
#[derive(Queryable, Debug)]
pub struct DownloadAnswer {
pub timestamp: NaiveDateTime,
pub email: String,
pub requester: String,
pub answers: Vec<String>
}
impl FromSql<(diesel::types::Timestamp, diesel::types::Text, diesel::types::Text, diesel::types::Array<diesel::types::Text>), diesel::pg::Pg> for DownloadAnswer {
fn from_sql(bytes: Option<&[u8]>) -> Result<Self, Box<Error + Send + Sync>> {
lazy_static! {
pub static ref ROCKET: rocket::Rocket = {
println!("Creating a rocket");
super::build_rocket("postgres://advisor_test@localhost/advisor_test")
};
}
#[test]
fn through_the_rocket_infrastructure() {