Skip to content

Instantly share code, notes, and snippets.

View lawliet89's full-sized avatar
πŸ§‘β€πŸ€β€πŸ§‘
He/him

Yong Wen Chua lawliet89

πŸ§‘β€πŸ€β€πŸ§‘
He/him
View GitHub Profile
@lawliet89
lawliet89 / ice.rs
Created July 21, 2017 06:47
rustc ICE Cargo Expand
#![feature(prelude_import)]
#![no_std]
#![feature(plugin, custom_derive, conservative_impl_trait)]
#![plugin(rocket_codegen)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
extern crate rocket;
@lawliet89
lawliet89 / expand.rs
Created July 14, 2017 14:31
Rocket lifetime cargo expand
#![feature(prelude_import)]
#![no_std]
#![feature(plugin, custom_derive)]
#![plugin(rocket_codegen)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
extern crate rocket;
@lawliet89
lawliet89 / error.txt
Created July 14, 2017 14:29
Compile Error
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'r` due to conflicting requirements
--> tests/lifetimes.rs:37:1
|
37 | #[get("/string_state")]
| ^^^^^^^^^^^^^^^^^^^^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the function body at 37:1...
--> tests/lifetimes.rs:37:1
|
37 | #[get("/string_state")]
@lawliet89
lawliet89 / main.rs
Created July 12, 2017 14:03
Lifetime confusion
#[macro_use]
extern crate serde_derive;
extern crate csv;
extern crate serde;
use serde::{Serialize, Deserialize};
pub trait Record<'de>: Serialize + Deserialize<'de> {
fn is_valid(&self) -> Result<(), String>;
@lawliet89
lawliet89 / arg.rs
Last active July 12, 2017 04:12
Condensed borrowing from Arguments
// From https://github.com/Neo-Type/manifest/blob/35bcff0ee504b7a8007bd01cf455554bb4fa51aa/src/main.rs#L56
lazy_static! {
// Regular expression to strip invalid file name characters.
static ref INVALID_FILE_CHARAS: Regex = Regex::new("[^A-Za-z0-9._-]").unwrap();
}
fn main() {
// `clap` library is used to parse arguments
// The details are not relevant to our discussion
let args = make_parser().get_matches();
@lawliet89
lawliet89 / borrowed.rs
Last active July 10, 2017 07:39
Struct of borrowed data
// Source: https://github.com/Neo-Type/manifest/blob/35bcff0ee504b7a8007bd01cf455554bb4fa51aa/src/main.rs#L26-L33
/// An "Image" in our manifest
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug)]
struct Image<'a> {
image: &'a str,
repository: &'a str,
tag: &'a str,
tarball: Cow<'a, str>,
}
@lawliet89
lawliet89 / terrible.sh
Created July 10, 2017 07:23
Terrible, terrible bash
IMAGES="hello-world ubuntu:16.04 alpine:latest busybox:latest"
# docker-image is from https://github.com/lawliet89/docker-image
docker-image --always-array ${IMAGES} \
| jq '{ utils: [ .[] as $item | $item as { repository: $repo, tag: $tag } | ($repo + ":" + $tag) as $image | $item + { image: $image, tarball: ("images/" + ($image | gsub("[^A-Za-z0-9._-]"; "_")) + ".tar.gz") } ] }' \
>> manifest.json
@lawliet89
lawliet89 / eg.rs
Last active April 21, 2017 08:59
serde derive codegen v1 from cargo expand
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]
const _IMPL_DESERIALIZE_FOR_Compact: () = {
extern crate serde as _serde;
#[automatically_derived]
impl<'de, T: CompactPart, H: Serialize + for<'de_inner> Deserialize<'de_inner>> _serde::Deserialize<'de>
for Compact<T, H>
where T: _serde::Deserialize<'de>
{
fn deserialize<__D>(__deserializer: __D) -> _serde::export::Result<Self, __D::Error>
where __D: _serde::Deserializer<'de>
@lawliet89
lawliet89 / macros.rs
Created March 14, 2017 08:23
[Rust] Useful Test Macros
macro_rules! not_err {
($e:expr) => (match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {:?}", stringify!($e), e),
})
}
macro_rules! is_err {
($e:expr) => (match $e {
Ok(e) => panic!("{} did not return with an error, but with {:?}", stringify!($e), e),
@lawliet89
lawliet89 / base64.rs
Created March 2, 2017 03:01
Rust Base64 Conversion Stuff
extern crate rustc_serialize;
use rustc_serialize::base64::{self, ToBase64, FromBase64};
#[derive(Clone, Eq, PartialEq)]
pub enum Base64 {
Standard(String),
UrlSafe(String),
Mime(String),
}