From d1796345a42f8066e5075c14a5c45872aed0ece3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Wawrzyniec=20Urba=C5=84czyk?= Date: Tue, 19 Nov 2019 22:26:54 +0100 Subject: [PATCH] The prelude library, coming from basegl, in preparation for AST. (#342) --- Cargo.toml | 3 +- common/rust/parser/Cargo.toml | 3 +- common/rust/parser/src/api.rs | 2 +- common/rust/parser/src/jsclient.rs | 2 +- common/rust/parser/src/lib.rs | 5 +-- common/rust/parser/src/wsclient.rs | 4 +-- common/rust/prelude/Cargo.toml | 16 ++++++++++ common/rust/prelude/src/lib.rs | 50 ++++++++++++++++++++++++++++++ 8 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 common/rust/prelude/Cargo.toml create mode 100644 common/rust/prelude/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index e56c8faf5a7..80ca7e91016 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ - "common/rust/parser" + "common/rust/parser", + "common/rust/prelude" ] \ No newline at end of file diff --git a/common/rust/parser/Cargo.toml b/common/rust/parser/Cargo.toml index 19f35049cf4..f2f766e8cfa 100644 --- a/common/rust/parser/Cargo.toml +++ b/common/rust/parser/Cargo.toml @@ -10,7 +10,8 @@ crate-type = ["cdylib", "rlib"] [dependencies] failure = "0.1" matches = "0.1" -serde = { version = "1.0", features = ["derive"] } +prelude = { version = "0.1.0", path = "../prelude" } +serde = { version = "1.0" , features = ["derive"] } serde_json = "1.0" shrinkwraprs = "0.2.1" wasm-bindgen = "0.2" diff --git a/common/rust/parser/src/api.rs b/common/rust/parser/src/api.rs index c0954cf5b01..8b74a0cc6e1 100644 --- a/common/rust/parser/src/api.rs +++ b/common/rust/parser/src/api.rs @@ -1,4 +1,4 @@ -use failure::Fail; +use prelude::*; // ============ // == Parser == diff --git a/common/rust/parser/src/jsclient.rs b/common/rust/parser/src/jsclient.rs index 6fad0852a7f..930df5070dd 100644 --- a/common/rust/parser/src/jsclient.rs +++ b/common/rust/parser/src/jsclient.rs @@ -1,5 +1,5 @@ use crate::{api, api::IsParser}; -use failure::Fail; +use prelude::*; pub type Result = std::result::Result; diff --git a/common/rust/parser/src/lib.rs b/common/rust/parser/src/lib.rs index bf11c354d3b..d9ecdc315bf 100644 --- a/common/rust/parser/src/lib.rs +++ b/common/rust/parser/src/lib.rs @@ -1,16 +1,17 @@ pub mod api; +use prelude::*; + mod jsclient; mod wsclient; -use std::ops::DerefMut; /// Handle to a parser implementation. /// /// Currently this component is implemented as a wrapper over parser written /// in Scala. Depending on compilation target (native or wasm) it uses either /// implementation provided by `wsclient` or `jsclient`. -#[derive(shrinkwraprs::Shrinkwrap)] +#[derive(Shrinkwrap)] #[shrinkwrap(mutable)] pub struct Parser(pub Box); diff --git a/common/rust/parser/src/wsclient.rs b/common/rust/parser/src/wsclient.rs index 7e8c51862ac..a332b0915d1 100644 --- a/common/rust/parser/src/wsclient.rs +++ b/common/rust/parser/src/wsclient.rs @@ -1,7 +1,7 @@ #![cfg(not(target_arch = "wasm32"))] -use failure::Fail; -use std::default::Default; +use prelude::*; + use websocket::{ stream::sync::TcpStream, ClientBuilder, Message, OwnedMessage, }; diff --git a/common/rust/prelude/Cargo.toml b/common/rust/prelude/Cargo.toml new file mode 100644 index 00000000000..e660e5aaa1b --- /dev/null +++ b/common/rust/prelude/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "prelude" +version = "0.1.0" +authors = ["Wojciech Danilo "] +edition = "2018" + +[lib] + +[dependencies] +failure = "0.1.5" +derive_more = "0.15.0" +shrinkwraprs = "0.2.1" +itertools = "0.8" +derivative = "1.0.3" +num = "0.2.0" +boolinator = "2.4.0" \ No newline at end of file diff --git a/common/rust/prelude/src/lib.rs b/common/rust/prelude/src/lib.rs new file mode 100644 index 00000000000..e6d7aa29c49 --- /dev/null +++ b/common/rust/prelude/src/lib.rs @@ -0,0 +1,50 @@ +#![feature(trait_alias)] + +pub use boolinator::Boolinator; +pub use core::{any::type_name, fmt::Debug}; +pub use derivative::Derivative; +pub use derive_more::*; +pub use failure::Fail; +pub use itertools::Itertools; +pub use num::Num; +pub use shrinkwraprs::Shrinkwrap; +pub use std::{ + cell::{Ref, RefCell}, + collections::{HashMap, HashSet}, + convert::{identity, TryFrom, TryInto}, + fmt::Display, + hash::Hash, + iter, + iter::FromIterator, + marker::PhantomData, + ops::{Deref, DerefMut, Index, IndexMut}, + rc::{Rc, Weak}, + slice, + slice::SliceIndex, +}; + +pub trait Str = AsRef; + +pub fn default() -> T { + Default::default() +} + +#[derive(Derivative, Shrinkwrap)] +#[shrinkwrap(mutable)] +#[derivative(Clone(bound = "T: Clone"))] +pub struct WithPhantomType { + #[shrinkwrap(main_field)] + pub t: T, + phantom: PhantomData

, +} + +impl WithPhantomType { + pub fn new(t: T) -> Self { + let phantom = PhantomData; + Self { t, phantom } + } +} + +pub fn with Out, Out>(t: T, f: F) -> Out { + f(t) +}