Skip to content
This repository was archived by the owner on Dec 28, 2021. It is now read-only.

Commit 0c67bba

Browse files
committed
Implementing fix for rustc bug: rust-lang/rust#65918
1 parent 9fac17b commit 0c67bba

File tree

7 files changed

+131
-42
lines changed

7 files changed

+131
-42
lines changed

lib/core/Cargo.toml

+18-19
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@ edition = "2018"
88
crate-type = ["cdylib"]
99

1010
[features]
11-
default = ["console_error_panic_hook"]
11+
default = ["no_unboxed_callbacks"]
12+
no_unboxed_callbacks = []
1213

1314
[dependencies]
14-
basegl-prelude = { version = "0.1.0" , path = "../prelude" }
15-
basegl-system-web = { version = "0.1.0" , path = "../system/web" }
16-
basegl-backend-webgl = { version = "0.1.0" , path = "../backend/webgl" }
17-
wasm-bindgen = { version = "^0.2" , features = ["nightly"] }
18-
js-sys = { version = "0.3.28" }
19-
failure = { version = "0.1.5" }
20-
derive_more = { version = "0.15.0" }
21-
shrinkwraprs = { version = "0.2.1" }
22-
itertools = { version = "0.8" }
23-
nalgebra = { version = "0.18.1" }
24-
bit_field = { version = "0.10.0" }
25-
paste = { version = "0.1.6" }
26-
enum_dispatch = { version = "= 0.1.3" } # https://gitlab.com/antonok/enum_dispatch/issues/10
27-
typenum = { version = "1.11.2" }
28-
rustc-hash = { version = "1.0.1" }
29-
30-
# TODO: disable for release
31-
console_error_panic_hook = { version = "0.1.1", optional = true }
15+
basegl-prelude = { version = "0.1.0" , path = "../prelude" }
16+
basegl-system-web = { version = "0.1.0" , path = "../system/web" }
17+
basegl-backend-webgl = { version = "0.1.0" , path = "../backend/webgl" }
18+
wasm-bindgen = { version = "^0.2" , features = ["nightly"] }
19+
js-sys = { version = "0.3.28" }
20+
failure = { version = "0.1.5" }
21+
derive_more = { version = "0.15.0" }
22+
shrinkwraprs = { version = "0.2.1" }
23+
itertools = { version = "0.8" }
24+
nalgebra = { version = "0.18.1" }
25+
bit_field = { version = "0.10.0" }
26+
paste = { version = "0.1.6" }
27+
enum_dispatch = { version = "= 0.1.3" } # https://gitlab.com/antonok/enum_dispatch/issues/10
28+
typenum = { version = "1.11.2" }
29+
rustc-hash = { version = "1.0.1" }
30+
console_error_panic_hook = { version = "0.1.6" }
3231

3332
[dependencies.web-sys]
3433
version = "0.3.4"

lib/core/src/data/function/callback.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<Func> fmt::Debug for Callback<Func> {
2929
// === Callback Interface ===
3030
// ==========================
3131

32-
pub trait Callback0 {
32+
pub trait Callback0: 'static {
3333
fn call(&mut self);
3434
}
3535

@@ -81,7 +81,14 @@ impl<Arg1, Arg2, Arg3, Arg4, Arg5> Callback5<Arg1, Arg2, Arg3, Arg4, Arg5> for (
8181

8282
// === FnMut Implementations ===
8383

84-
impl<F: FnMut() -> T, T> Callback0 for F {
84+
// FIXME: How to make it more generic?
85+
impl<T: 'static, P: 'static> Callback0 for WithPhantomType<Rc<Fn() -> T>, P> {
86+
fn call(&mut self) {
87+
(self.t)();
88+
}
89+
}
90+
91+
impl<F: FnMut() -> T + 'static, T> Callback0 for F {
8592
fn call(&mut self) {
8693
self();
8794
}

lib/core/src/data/function/closure.rs

+49
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,38 @@
1+
// #[macro_export]
2+
// macro_rules! closure {
3+
// ($name:ident
4+
// <$($param:ident : $param_type:ty),*>
5+
// ($($arg:ident : $arg_type:ty),*)
6+
// |$($larg:ident : $larg_type:ty),*|
7+
// $body:tt
8+
// ) => {
9+
// closure!( $name<$($param:$param_type),*>
10+
// ($($arg:$arg_type),*)
11+
// ($($larg:$larg_type)*)
12+
// $body
13+
// );
14+
// };
15+
// ($name:ident
16+
// <$($param:ident : $param_type:ty),*>
17+
// ($($arg:ident : $arg_type:ty),*)
18+
// || $body:tt) => {
19+
// closure!($name<$($param:$param_type),*>($($arg:$arg_type),*)()$body);
20+
// };
21+
// ($name:ident
22+
// <$($param:ident : $param_type:ty),*>
23+
// ($($arg:ident : $arg_type:ty),*)
24+
// ($($larg:ident : $larg_type:ty),*)
25+
// $body:tt
26+
// ) => { paste::item! {
27+
// pub type [<Closure_ $name>]<$($param),*> =
28+
// impl Fn($($larg_type),*) + Clone;
29+
// pub fn $name<$($param:$param_type),*>
30+
// ($($arg:$arg_type),*) -> [<Closure_ $name>]<$($param),*> {
31+
// move |$($larg),*| $body
32+
// }
33+
// }};
34+
// }
35+
136
#[macro_export]
237
macro_rules! closure {
338
($name:ident
@@ -24,11 +59,25 @@ macro_rules! closure {
2459
($($larg:ident : $larg_type:ty),*)
2560
$body:tt
2661
) => { paste::item! {
62+
#[cfg(not(feature = "no_unboxed_callbacks"))]
2763
pub type [<Closure_ $name>]<$($param),*> =
2864
impl Fn($($larg_type),*) + Clone;
65+
66+
#[cfg(not(feature = "no_unboxed_callbacks"))]
2967
pub fn $name<$($param:$param_type),*>
3068
($($arg:$arg_type),*) -> [<Closure_ $name>]<$($param),*> {
3169
move |$($larg),*| $body
3270
}
71+
72+
#[cfg(feature = "no_unboxed_callbacks")]
73+
pub type [<Closure_ $name>]<$($param),*> =
74+
WithPhantomType<Rc<dyn Fn($($larg_type),*)>, $($param),*>;
75+
76+
#[cfg(feature = "no_unboxed_callbacks")]
77+
pub fn $name<$($param:$param_type),*>
78+
($($arg:$arg_type),*)
79+
-> WithPhantomType<Rc<dyn Fn($($larg_type),*)>, $($param),*> {
80+
WithPhantomType::new(Rc::new(move |$($larg),*| $body))
81+
}
3382
}};
3483
}

lib/core/src/display/symbol/attribute.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -258,10 +258,18 @@ pub trait ResizeDirtyCtx <Callback> = dirty::BoolCtx<Callback>;
258258
pub type SetDirty <Callback> = dirty::SharedRange<usize, Callback>;
259259
pub type ResizeDirty <Callback> = dirty::SharedBool<Callback>;
260260

261+
// pub type Buffer <T, OnSet, OnResize> = Observable
262+
// < Vec<T>
263+
// , Closure_buffer_on_set_handler <OnSet>
264+
// , Closure_buffer_on_resize_handler <OnResize>
265+
// >;
266+
267+
268+
261269
pub type Buffer <T, OnSet, OnResize> = Observable
262270
< Vec<T>
263-
, Closure_buffer_on_set_handler <OnSet>
264-
, Closure_buffer_on_resize_handler <OnResize>
271+
, Closure_buffer_on_set_handler<OnSet>
272+
, Closure_buffer_on_resize_handler<OnResize>
265273
>;
266274

267275
// === Callbacks ===
@@ -272,9 +280,17 @@ closure!(buffer_on_resize_handler<Callback: Callback0>
272280
closure!(buffer_on_set_handler<Callback: Callback0>
273281
(dirty: SetDirty<Callback>) |ix: usize| { dirty.set(ix) });
274282

283+
// pub type Closure_buffer_on_set_handler<Callback> = impl Fn(usize) + Clone;
284+
// pub fn buffer_on_set_handler<Callback: Callback0>
285+
// (dirty: SetDirty<Callback>) -> Closure_buffer_on_set_handler<Callback> {
286+
// move |ix| { dirty.set(ix) }
287+
// }
288+
289+
// pub type Closure_buffer_on_set_handler<Callback> = impl Fn(usize) + Clone;
290+
275291
// === Instances ===
276292

277-
impl<T: Shape, OnSet: Callback0, OnResize: Callback0>
293+
impl<T: Shape, OnSet: Callback0 + 'static, OnResize: Callback0 + 'static>
278294
Attribute<T, OnSet, OnResize> {
279295
pub fn new_from
280296
(vec: Vec<T>, logger: Logger, on_set: OnSet, on_resize: OnResize) -> Self {
@@ -319,7 +335,8 @@ Attribute<T, OnSet, OnResize> {
319335
}
320336

321337
pub fn add_elements(&mut self, elem_count: usize) {
322-
self.extend(iter::repeat(T::empty()).take(elem_count));
338+
unimplemented!()
339+
// self.extend(iter::repeat(T::empty()).take(elem_count));
323340
}
324341
}
325342

@@ -505,9 +522,6 @@ type Identity<T> = T;
505522
mk_any_shape!([Identity, Vector2, Vector3, Vector4], [f32, i32]);
506523

507524

508-
509-
510-
511525
#[enum_dispatch]
512526
pub trait IsAttribute<OnSet, OnResize> {
513527
fn add_element(&mut self);

lib/core/src/display/symbol/scope.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,15 @@ impl<OnDirty: Callback0 + 'static> Scope<OnDirty> {
126126
}
127127

128128

129-
// impl<T, OnDirty> Index<AttributeIndex<T, OnDirty>> for Scope<OnDirty> {
130-
// type Output = Attribute<T, OnDirty>;
131-
// fn index(&self, ix: AttributeIndex<T, OnDirty>) -> &Self::Output {
132-
// unimplemented!()
133-
// // self.attributes.index(ix)
134-
// }
135-
// }
136-
137129
impl<T, OnDirty>
138130
Index<TypedIndex<usize, T>> for Scope<OnDirty>
139131
where for<'t> &'t T: TryFrom<&'t AnyAttribute<OnDirty>> {
140132
type Output = T;
141133
fn index(&self, t: TypedIndex<usize, T>) -> &Self::Output {
142-
self.attributes.index(t.ix).try_into().ok().unwrap()
134+
match self.attributes.index(t.ix).try_into() {
135+
Ok(t) => t,
136+
_ => panic!("Unmatched types for given index.")
137+
}
143138
}
144139
}
145140

@@ -148,7 +143,10 @@ IndexMut<TypedIndex<usize, T>> for Scope<OnDirty>
148143
where for<'t> &'t T: TryFrom<&'t AnyAttribute<OnDirty>>,
149144
for<'t> &'t mut T: TryFrom<&'t mut AnyAttribute<OnDirty>> {
150145
fn index_mut(&mut self, t: TypedIndex<usize, T>) -> &mut Self::Output {
151-
self.attributes.index_mut(t.ix).try_into().ok().unwrap()
146+
match self.attributes.index_mut(t.ix).try_into() {
147+
Ok(t) => t,
148+
_ => panic!("Unmatched types for given index.")
149+
}
152150
}
153151
}
154152

lib/core/src/lib.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,17 @@ use std::ops::Index;
108108
use rustc_hash::FxHashSet;
109109
use std::collections::HashSet;
110110
use crate::display::mesh_registry::MeshRegistry;
111+
use console_error_panic_hook;
111112

112113
#[wasm_bindgen(start)]
113114
pub fn start() {
115+
console_error_panic_hook::set_once();
116+
114117
let logger = Logger::new("root");
115118

119+
type PositionID = AttributeIndex<Vector2<f32>>;
120+
type Position = Attribute<Vector2<f32>>;
121+
116122
let world : World = World::new();
117123
let wspace_id : WorkspaceID = world.add_workspace("canvas");
118124
let workspace : &mut Workspace = &mut world.data.borrow_mut()[wspace_id];
@@ -121,9 +127,8 @@ pub fn start() {
121127
let geo : &mut Geometry = &mut mesh.geometry;
122128
let scopes : &mut Scopes = &mut geo.scopes;
123129
let pt_scope : &mut AttributeScope = &mut scopes.point;
124-
let pos_id : AttributeIndex<Vector2<f32>> = pt_scope.add_attribute("position", Attribute::builder());
125-
let pos_id : AttributeIndex<Vector2<i32>> = AttributeIndex::unsafe_new(0);
126-
let pos : &mut Attribute<Vector2<i32>> = &mut pt_scope[pos_id];
130+
let pos_id : PositionID = pt_scope.add_attribute("position", Attribute::builder());
131+
let pos : &mut Position = &mut pt_scope[pos_id];
127132

128133
// let logger = Logger::new("test");
129134
//
@@ -134,7 +139,7 @@ pub fn start() {
134139
// let logger = Logger::new("point");
135140
// let mut point_scope: Scope = Scope::new(logger,());
136141
// point_scope.add("position", Attr::builder());
137-
let logger = Logger::new("mesh_registryxxx");
142+
let logger = Logger::new("mesh_registry");
138143

139144

140145
let mut mesh_registry = MeshRegistry::new(logger, ());

lib/prelude/src/lib.rs

+17
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,20 @@ pub trait Str = AsRef<str>;
3232
pub fn default<T: Default>() -> T {
3333
Default::default()
3434
}
35+
36+
#[derive(Derivative)]
37+
#[derive(Shrinkwrap)]
38+
#[shrinkwrap(mutable)]
39+
#[derivative(Clone(bound="T: Clone"))]
40+
pub struct WithPhantomType<T, P=()> {
41+
#[shrinkwrap(main_field)]
42+
pub t: T,
43+
phantom: PhantomData<P>
44+
}
45+
46+
impl<T, P> WithPhantomType<T, P> {
47+
pub fn new(t: T) -> Self {
48+
let phantom = PhantomData;
49+
Self { t, phantom }
50+
}
51+
}

0 commit comments

Comments
 (0)