Skip to content

Commit 5b7af83

Browse files
committed
Use prefered solution for local crate references from procedural macros
rust-lang/rust#55275
1 parent 7a5bdf6 commit 5b7af83

File tree

4 files changed

+66
-79
lines changed

4 files changed

+66
-79
lines changed

kas-macros/src/layout_cw.rs

+22-22
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ use syn::Ident;
99
use syn::parse::{Error, Result};
1010
use crate::args::Child;
1111

12-
pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
12+
pub(crate) fn fns(children: &Vec<Child>, layout: Option<Ident>)
1313
-> Result<TokenStream>
1414
{
1515
let (constraints, appls) = if children.is_empty() {
1616
// TODO: warn on invalid layout specification
1717
(quote!{
18-
let v_w = #c::cw_var!(self, w);
19-
let v_h = #c::cw_var!(self, h);
18+
let v_w = kas::cw_var!(self, w);
19+
let v_h = kas::cw_var!(self, h);
2020

2121
let (min, hint) = tk.size_hints(self.tkd());
2222

@@ -54,11 +54,11 @@ pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
5454
let ident = &children[0].ident;
5555
(quote!{
5656
s.add_constraint(cw::Constraint::new(
57-
cw::Expression::from(#c::cw_var!(self, w)) - #c::cw_var!(self.#ident, w),
57+
cw::Expression::from(kas::cw_var!(self, w)) - kas::cw_var!(self.#ident, w),
5858
cw::RelationalOperator::Equal,
5959
cw::strength::STRONG)).unwrap();
6060
s.add_constraint(cw::Constraint::new(
61-
cw::Expression::from(#c::cw_var!(self, h)) - #c::cw_var!(self.#ident, h),
61+
cw::Expression::from(kas::cw_var!(self, h)) - kas::cw_var!(self.#ident, h),
6262
cw::RelationalOperator::Equal,
6363
cw::strength::STRONG)).unwrap();
6464
self.#ident.init_constraints(tk, s, _use_default);
@@ -67,17 +67,17 @@ pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
6767
if let Some(l) = layout {
6868
if l == "horizontal" {
6969
let mut constr = quote!{
70-
let mut width = cw::Expression::from(#c::cw_var!(self, w));
71-
let height = cw::Expression::from(#c::cw_var!(self, h));
70+
let mut width = cw::Expression::from(kas::cw_var!(self, w));
71+
let height = cw::Expression::from(kas::cw_var!(self, h));
7272
};
7373
let mut appls = quote!{ let mut cpos = pos; };
7474

7575
for child in children {
7676
let ident = &child.ident;
7777

7878
constr.append_all(quote!{
79-
let child_v_w = #c::cw_var!(self.#ident, w);
80-
let child_v_h = #c::cw_var!(self.#ident, h);
79+
let child_v_w = kas::cw_var!(self.#ident, w);
80+
let child_v_h = kas::cw_var!(self.#ident, h);
8181
width -= child_v_w;
8282
s.add_constraint(cw::Constraint::new(
8383
height.clone() - child_v_h,
@@ -92,7 +92,7 @@ pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
9292

9393
appls.append_all(quote!{
9494
self.#ident.apply_constraints(tk, s, cpos);
95-
cpos.0 += s.get_value(#c::cw_var!(self.#ident, w)) as i32;
95+
cpos.0 += s.get_value(kas::cw_var!(self.#ident, w)) as i32;
9696
});
9797
}
9898

@@ -106,17 +106,17 @@ pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
106106
(constr, appls)
107107
} else if l == "vertical" {
108108
let mut constr = quote!{
109-
let width = cw::Expression::from(#c::cw_var!(self, w));
110-
let mut height = cw::Expression::from(#c::cw_var!(self, h));
109+
let width = cw::Expression::from(kas::cw_var!(self, w));
110+
let mut height = cw::Expression::from(kas::cw_var!(self, h));
111111
};
112112
let mut appls = quote!{ let mut cpos = pos; };
113113

114114
for child in children {
115115
let ident = &child.ident;
116116

117117
constr.append_all(quote!{
118-
let child_v_w = #c::cw_var!(self.#ident, w);
119-
let child_v_h = #c::cw_var!(self.#ident, h);
118+
let child_v_w = kas::cw_var!(self.#ident, w);
119+
let child_v_h = kas::cw_var!(self.#ident, h);
120120
s.add_constraint(cw::Constraint::new(
121121
width.clone() - child_v_w,
122122
cw::RelationalOperator::GreaterOrEqual,
@@ -131,7 +131,7 @@ pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
131131

132132
appls.append_all(quote!{
133133
self.#ident.apply_constraints(tk, s, cpos);
134-
cpos.1 += s.get_value(#c::cw_var!(self.#ident, h)) as i32;
134+
cpos.1 += s.get_value(kas::cw_var!(self.#ident, h)) as i32;
135135
});
136136
}
137137

@@ -154,20 +154,20 @@ pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
154154
}
155155
};
156156
Ok(quote! {
157-
fn init_constraints(&self, tk: &#c::TkWidget,
158-
s: &mut #c::cw::Solver, _use_default: bool)
157+
fn init_constraints(&self, tk: &kas::TkWidget,
158+
s: &mut kas::cw::Solver, _use_default: bool)
159159
{
160-
use #c::cw;
160+
use kas::cw;
161161
#constraints
162162
}
163163

164-
fn apply_constraints(&mut self, tk: &#c::TkWidget,
165-
s: &#c::cw::Solver, pos: #c::Coord)
164+
fn apply_constraints(&mut self, tk: &kas::TkWidget,
165+
s: &kas::cw::Solver, pos: kas::Coord)
166166
{
167167
#appls
168168

169-
let w = s.get_value(#c::cw_var!(self, w)) as i32;
170-
let h = s.get_value(#c::cw_var!(self, h)) as i32;
169+
let w = s.get_value(kas::cw_var!(self, w)) as i32;
170+
let h = s.get_value(kas::cw_var!(self, h)) as i32;
171171
let tkd = self.tkd();
172172
let rect = self.rect_mut();
173173
rect.pos = pos;

kas-macros/src/layout_extern.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,24 @@ use syn::{parse_quote, Path, Ident};
99
use syn::parse::{Error, Result};
1010
use crate::args::Child;
1111

12-
pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
12+
pub(crate) fn fns(children: &Vec<Child>, layout: Option<Ident>)
1313
-> Result<TokenStream>
1414
{
1515
let layout: Path = if let Some(l) = layout {
1616
if l == "single" {
17-
parse_quote!{ #c::ChildLayout::None }
17+
parse_quote!{ kas::ChildLayout::None }
1818
} else if l == "horizontal" {
19-
parse_quote!{ #c::ChildLayout::Horizontal }
19+
parse_quote!{ kas::ChildLayout::Horizontal }
2020
} else if l == "vertical" {
21-
parse_quote!{ #c::ChildLayout::Vertical }
21+
parse_quote!{ kas::ChildLayout::Vertical }
2222
} else if l == "grid" {
23-
parse_quote!{ #c::ChildLayout::Grid }
23+
parse_quote!{ kas::ChildLayout::Grid }
2424
} else {
2525
return Err(Error::new(l.span(),
2626
"expected one of: single, horizontal, vertical, grid"));
2727
}
2828
} else {
29-
parse_quote!{ #c::ChildLayout::None }
29+
parse_quote!{ kas::ChildLayout::None }
3030
};
3131

3232
let mut pos_rules = TokenStream::new();
@@ -40,19 +40,19 @@ pub(crate) fn fns(c: &TokenStream, children: &Vec<Child>, layout: Option<Ident>)
4040
}
4141

4242
Ok(quote! {
43-
fn child_layout(&self) -> #c::ChildLayout {
43+
fn child_layout(&self) -> kas::ChildLayout {
4444
#layout
4545
}
4646

47-
fn grid_pos(&self, _index: usize) -> Option<#c::GridPos> {
47+
fn grid_pos(&self, _index: usize) -> Option<kas::GridPos> {
4848
match _index {
4949
#pos_rules
5050
_ => None
5151
}
5252
}
5353

54-
fn sync_size(&mut self, tk: &#c::TkWidget) {
55-
use #c::Core;
54+
fn sync_size(&mut self, tk: &kas::TkWidget) {
55+
use kas::Core;
5656
let new_rect = tk.get_rect(self.tkd());
5757
*self.rect_mut() = new_rect;
5858

kas-macros/src/lib.rs

+32-47
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ extern crate proc_macro;
1010

1111
mod args;
1212

13-
use std::env;
1413
use std::fmt::Write;
1514
use proc_macro2::{Span, TokenStream};
1615
use quote::{quote, TokenStreamExt};
@@ -146,7 +145,6 @@ use self::args::{Class, ChildType};
146145
#[proc_macro_derive(Widget, attributes(core, widget, handler))]
147146
pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
148147
let mut ast = parse_macro_input!(input as DeriveInput);
149-
let c = c();
150148

151149
let args = match args::read_attrs(&mut ast) {
152150
Ok(w) => w,
@@ -159,7 +157,7 @@ pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
159157
let class = args.widget.class;
160158
let count = args.children.len();
161159

162-
let layout_fns = match layout::fns(&c, &args.children, args.widget.layout) {
160+
let layout_fns = match layout::fns(&args.children, args.widget.layout) {
163161
Ok(fns) => fns,
164162
Err(err) => return err.to_compile_error().into(),
165163
};
@@ -176,58 +174,58 @@ pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
176174
let get_mut_rules = make_match_rules(&args.children, quote!{mut});
177175

178176
let mut toks = quote! {
179-
impl #impl_generics #c::Core
177+
impl #impl_generics kas::Core
180178
for #name #ty_generics #where_clause
181179
{
182180
fn number(&self) -> u32 {
183-
use #c::Core;
181+
use kas::Core;
184182
self.#core.number()
185183
}
186184
fn set_number(&mut self, number: u32) {
187-
use #c::Core;
185+
use kas::Core;
188186
self.#core.set_number(number);
189187
}
190188

191-
fn tkd(&self) -> #c::TkData {
192-
use #c::Core;
189+
fn tkd(&self) -> kas::TkData {
190+
use kas::Core;
193191
self.#core.tkd()
194192
}
195-
fn set_tkd(&mut self, tkd: #c::TkData) {
196-
use #c::Core;
193+
fn set_tkd(&mut self, tkd: kas::TkData) {
194+
use kas::Core;
197195
self.#core.set_tkd(tkd)
198196
}
199197

200-
fn rect(&self) -> &#c::Rect {
201-
use #c::Core;
198+
fn rect(&self) -> &kas::Rect {
199+
use kas::Core;
202200
self.#core.rect()
203201
}
204-
fn rect_mut(&mut self) -> &mut #c::Rect {
205-
use #c::Core;
202+
fn rect_mut(&mut self) -> &mut kas::Rect {
203+
use kas::Core;
206204
self.#core.rect_mut()
207205
}
208206
}
209207

210-
impl #impl_generics #c::Layout
208+
impl #impl_generics kas::Layout
211209
for #name #ty_generics #where_clause
212210
{
213211
#layout_fns
214212
}
215213

216-
impl #impl_generics #c::Widget
214+
impl #impl_generics kas::Widget
217215
for #name #ty_generics #where_clause
218216
{
219-
fn class(&self) -> #c::Class { #class }
217+
fn class(&self) -> kas::Class { #class }
220218

221219
fn len(&self) -> usize {
222220
#count
223221
}
224-
fn get(&self, _index: usize) -> Option<&#c::Widget> {
222+
fn get(&self, _index: usize) -> Option<&kas::Widget> {
225223
match _index {
226224
#get_rules
227225
_ => None
228226
}
229227
}
230-
fn get_mut(&mut self, _index: usize) -> Option<&mut #c::Widget> {
228+
fn get_mut(&mut self, _index: usize) -> Option<&mut kas::Widget> {
231229
match _index {
232230
#get_mut_rules
233231
_ => None
@@ -276,15 +274,15 @@ pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
276274
}
277275

278276
toks.append_all(quote! {
279-
impl #impl_generics #c::event::Handler
277+
impl #impl_generics kas::event::Handler
280278
for #name #ty_generics #where_clause
281279
{
282280
type Response = #response;
283281

284-
fn handle_action(&mut self, _tk: &#c::TkWidget, action: #c::event::Action,
282+
fn handle_action(&mut self, _tk: &kas::TkWidget, action: kas::event::Action,
285283
num: u32) -> Self::Response
286284
{
287-
use #c::{Core, event::{Handler, err_unhandled, err_num}};
285+
use kas::{Core, event::{Handler, err_unhandled, err_num}};
288286

289287
if num == self.number() {
290288
// we may want to allow custom handlers on self here?
@@ -442,10 +440,8 @@ pub fn make_widget(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
442440
// Used to make fresh identifiers for generic types
443441
let mut name_buf = String::with_capacity(32);
444442

445-
let c = c();
446-
447443
// fields of anonymous struct:
448-
let mut field_toks = quote!{ #[core] core: #c::CoreData, };
444+
let mut field_toks = quote!{ #[core] core: kas::CoreData, };
449445
// initialisers for these fields:
450446
let mut field_val_toks = quote!{ core: Default::default(), };
451447
// debug impl
@@ -463,10 +459,10 @@ pub fn make_widget(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
463459

464460
let widget_args = match args.class {
465461
Class::Container(layout) => quote!{
466-
class = #c::Class::Container, layout = #layout
462+
class = kas::Class::Container, layout = #layout
467463
},
468464
Class::Frame => quote!{
469-
class = #c::Class::Frame
465+
class = kas::Class::Frame
470466
},
471467
};
472468

@@ -492,32 +488,32 @@ pub fn make_widget(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
492488
gen_tys.push(ty.clone());
493489
if let Some(ref wattr) = attr {
494490
if let Some(tyr) = gen_response {
495-
handler_clauses.push(quote!{ #ty: #c::event::Handler<Response = #tyr> });
491+
handler_clauses.push(quote!{ #ty: kas::event::Handler<Response = #tyr> });
496492
} else {
497493
// No typing. If a handler is specified, then the child must implement
498494
// Handler<Response = X> where the handler takes type X; otherwise
499495
// we use `msg.into()` and this conversion must be supported.
500496
if let Some(ref handler) = wattr.args.handler {
501497
if let Some(ty_bound) = find_handler_ty(handler, &args.impls) {
502-
handler_clauses.push(quote!{ #ty: #c::event::Handler<Response = #ty_bound> });
498+
handler_clauses.push(quote!{ #ty: kas::event::Handler<Response = #ty_bound> });
503499
} else {
504500
return quote!{}.into(); // exit after emitting error
505501
}
506502
} else {
507503
name_buf.push_str("R");
508504
let tyr = Ident::new(&name_buf, Span::call_site());
509505
handler_extra.push(tyr.clone());
510-
handler_clauses.push(quote!{ #ty: #c::event::Handler<Response = #tyr> });
511-
handler_clauses.push(quote!{ #tyr: From<#c::event::NoResponse> });
506+
handler_clauses.push(quote!{ #ty: kas::event::Handler<Response = #tyr> });
507+
handler_clauses.push(quote!{ #tyr: From<kas::event::NoResponse> });
512508
handler_clauses.push(quote!{ #response: From<#tyr> });
513509
}
514510
}
515511

516512
if let Some(mut bound) = gen_bound {
517-
bound.bounds.push(parse_quote!{ #c::Widget });
513+
bound.bounds.push(parse_quote!{ kas::Widget });
518514
gen_ptrs.push(quote!{ #ty: #bound });
519515
} else {
520-
gen_ptrs.push(quote!{ #ty: #c::Widget });
516+
gen_ptrs.push(quote!{ #ty: kas::Widget });
521517
}
522518
} else {
523519
gen_ptrs.push(quote!{ #ty });
@@ -563,7 +559,7 @@ pub fn make_widget(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
563559
let toks = (quote!{ {
564560
#[widget(#widget_args)]
565561
#[handler(response = #response, generics = < #handler_extra > #handler_where)]
566-
#[derive(Clone, Debug, #c::macros::Widget)]
562+
#[derive(Clone, Debug, kas::macros::Widget)]
567563
struct AnonWidget<#gen_ptrs> {
568564
#field_toks
569565
}
@@ -585,28 +581,17 @@ pub fn make_widget(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
585581
#[proc_macro_derive(NoResponse)]
586582
pub fn derive_no_response(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
587583
let ast = parse_macro_input!(input as DeriveInput);
588-
let c = c();
589584
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
590585
let name = &ast.ident;
591586

592587
let toks = quote!{
593-
impl #impl_generics From<#c::event::NoResponse>
588+
impl #impl_generics From<kas::event::NoResponse>
594589
for #name #ty_generics #where_clause
595590
{
596-
fn from(_: #c::event::NoResponse) -> Self {
591+
fn from(_: kas::event::NoResponse) -> Self {
597592
#name::None
598593
}
599594
}
600595
};
601596
toks.into()
602597
}
603-
604-
// Our stand-in for $crate. Imperfect, but works (excepting other crates in
605-
// the same package, i.e. doc-tests, examples, integration tests, benches).
606-
fn c() -> TokenStream {
607-
if env::var("CARGO_PKG_NAME") == Ok("kas".to_string()) {
608-
parse_quote!( crate )
609-
} else {
610-
parse_quote!( kas )
611-
}
612-
}

0 commit comments

Comments
 (0)