Skip to content

Commit 0039204

Browse files
authored
Style nits and rework the entry traits (#32)
* Style nits and rework the entry traits * Remove bounds which are not used
1 parent 254ffbd commit 0039204

File tree

16 files changed

+421
-369
lines changed

16 files changed

+421
-369
lines changed

fixed-map-derive/src/any_variants.rs

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::context::{Ctxt, FieldKind, FieldSpec};
88
pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()> {
99
let vis = &cx.ast.vis;
1010
let ident = &cx.ast.ident;
11+
let lt = cx.lt;
1112

1213
let clone = &cx.toks.clone;
1314
let copy = &cx.toks.copy;
@@ -192,11 +193,11 @@ pub(crate) fn implement(cx: &Ctxt<'_>, en: &DataEnum) -> Result<TokenStream, ()>
192193

193194
#[automatically_derived]
194195
impl<V> #storage_trait<#ident, V> for Storage<V> {
195-
type Iter<'this> = Iter<'this, V> where Self: 'this;
196-
type Keys<'this> = Keys<'this, V> where Self: 'this;
197-
type Values<'this> = Values<'this, V> where Self: 'this;
198-
type IterMut<'this> = IterMut<'this, V> where Self: 'this;
199-
type ValuesMut<'this> = ValuesMut<'this, V> where Self: 'this;
196+
type Iter<#lt> = Iter<#lt, V> where V: #lt;
197+
type Keys<#lt> = Keys<#lt, V> where V: #lt;
198+
type Values<#lt> = Values<#lt, V> where V: #lt;
199+
type IterMut<#lt> = IterMut<#lt, V> where V: #lt;
200+
type ValuesMut<#lt> = ValuesMut<#lt, V> where V: #lt;
200201
type IntoIter = IntoIter<V>;
201202

202203
#[inline]
@@ -356,15 +357,15 @@ fn build_iter_next(
356357
FieldKind::Simple => {
357358
step_forward.next.push(quote! {
358359
#index => {
359-
if let #option::Some(value) = self.#name.take() {
360+
if let #option::Some(value) = #option::take(&mut self.#name) {
360361
return #option::Some((#ident::#var, value));
361362
}
362363
}
363364
});
364365

365366
step_backward.next.push(quote! {
366367
#index => {
367-
if let #option::Some(value) = self.#name.take() {
368+
if let #option::Some(value) = #option::take(&mut self.#name) {
368369
return #option::Some((#ident::#var, value));
369370
}
370371
}
@@ -681,15 +682,15 @@ fn build_values_impl(
681682

682683
step_forward.next.push(quote! {
683684
#index => {
684-
if let #option::Some(value) = self.#name.take() {
685+
if let #option::Some(value) = #option::take(&mut self.#name) {
685686
return #option::Some(value);
686687
}
687688
}
688689
});
689690

690691
step_backward.next.push(quote! {
691692
#index => {
692-
if let #option::Some(value) = self.#name.take() {
693+
if let #option::Some(value) = #option::take(&mut self.#name) {
693694
return #option::Some(value);
694695
}
695696
}
@@ -900,16 +901,16 @@ fn build_values_mut_impl(
900901

901902
step_forward.next.push(quote! {
902903
#index => {
903-
if let #option::Some(v) = self.#name.take() {
904-
return #option::Some(v);
904+
if let #option::Some(value) = #option::take(&mut self.#name) {
905+
return #option::Some(value);
905906
}
906907
}
907908
});
908909

909910
step_backward.next.push(quote! {
910911
#index => {
911-
if let #option::Some(v) = self.#name.take() {
912-
return #option::Some(v);
912+
if let #option::Some(value) = #option::take(&mut self.#name) {
913+
return #option::Some(value);
913914
}
914915
}
915916
});
@@ -1141,6 +1142,7 @@ impl ToTokens for IteratorNextBack {
11411142
fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<TokenStream, ()> {
11421143
let ident = &cx.ast.ident;
11431144
let vis = &cx.ast.vis;
1145+
let lt = cx.lt;
11441146
let option = &cx.toks.option;
11451147
let storage_entry_trait = &cx.toks.storage_entry_trait;
11461148
let occupied_entry_trait = &cx.toks.occupied_entry_trait;
@@ -1179,8 +1181,8 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
11791181
} => {
11801182
let as_storage_entry = quote!(<#storage as #storage_entry_trait<#element, V>>);
11811183

1182-
occupied_variant.push(quote!( #name(#as_storage_entry::Occupied<'this>) ));
1183-
vacant_variant.push(quote!( #name(#as_storage_entry::Vacant<'this>) ));
1184+
occupied_variant.push(quote!( #name(#as_storage_entry::Occupied<#lt>) ));
1185+
vacant_variant.push(quote!( #name(#as_storage_entry::Vacant<#lt>) ));
11841186

11851187
init.push(quote! {
11861188
#pattern(key) => match #storage_entry_trait::entry(&mut self.#name, key) {
@@ -1189,7 +1191,7 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
11891191
}
11901192
});
11911193

1192-
let as_vacant_entry = quote!(<#as_storage_entry::Vacant<'this> as #vacant_entry_trait<'this, #element, V>>);
1194+
let as_vacant_entry = quote!(<#as_storage_entry::Vacant<#lt> as #vacant_entry_trait<#lt, #element, V>>);
11931195

11941196
vacant_key.push(
11951197
quote!( VacantEntry::#name(entry) => #pattern(#as_vacant_entry::key(entry)) ),
@@ -1198,7 +1200,7 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
11981200
quote!( VacantEntry::#name(entry) => #as_vacant_entry::insert(entry, value) ),
11991201
);
12001202

1201-
let as_occupied_entry = quote!(<#as_storage_entry::Occupied<'this> as #occupied_entry_trait<'this, #element, V>>);
1203+
let as_occupied_entry = quote!(<#as_storage_entry::Occupied<#lt> as #occupied_entry_trait<#lt, #element, V>>);
12021204

12031205
occupied_key.push(quote!( OccupiedEntry::#name(entry) => #pattern(#as_occupied_entry::key(entry)) ));
12041206
occupied_get
@@ -1218,82 +1220,72 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
12181220
}
12191221

12201222
let entry_impl = quote! {
1221-
#vis struct SimpleVacantEntry<'this, V> {
1222-
key: #ident,
1223-
inner: #option_bucket_none<'this, V>,
1224-
}
1225-
1226-
#vis struct SimpleOccupiedEntry<'this, V> {
1223+
#vis struct SimpleVacantEntry<#lt, V> {
12271224
key: #ident,
1228-
inner: #option_bucket_some<'this, V>,
1225+
inner: #option_bucket_none<#lt, V>,
12291226
}
12301227

1231-
#vis enum VacantEntry<'this, V> {
1232-
Simple(SimpleVacantEntry<'this, V>),
1233-
#(#vacant_variant,)*
1234-
}
1235-
1236-
#vis enum OccupiedEntry<'this, V> {
1237-
Simple(SimpleOccupiedEntry<'this, V>),
1238-
#(#occupied_variant,)*
1239-
}
1240-
1241-
impl<'this, V> SimpleVacantEntry<'this, V> {
1242-
#[inline]
1243-
fn key(&self) -> #ident {
1244-
self.key
1245-
}
1246-
1228+
impl<#lt, V> SimpleVacantEntry<#lt, V> {
12471229
#[inline]
1248-
fn insert(self, value: V) -> &'this mut V {
1249-
self.inner.insert(value)
1230+
fn insert(self, value: V) -> &#lt mut V {
1231+
#option_bucket_none::insert(self.inner, value)
12501232
}
12511233
}
12521234

1253-
impl<'this, V> SimpleOccupiedEntry<'this, V> {
1254-
#[inline]
1255-
fn key(&self) -> #ident {
1256-
self.key
1257-
}
1235+
#vis struct SimpleOccupiedEntry<#lt, V> {
1236+
key: #ident,
1237+
inner: #option_bucket_some<#lt, V>,
1238+
}
12581239

1240+
impl<#lt, V> SimpleOccupiedEntry<#lt, V> {
12591241
#[inline]
12601242
fn get(&self) -> &V {
1261-
self.inner.as_ref()
1243+
#option_bucket_some::as_ref(&self.inner)
12621244
}
12631245

12641246
#[inline]
12651247
fn get_mut(&mut self) -> &mut V {
1266-
self.inner.as_mut()
1248+
#option_bucket_some::as_mut(&mut self.inner)
12671249
}
12681250

12691251
#[inline]
1270-
fn into_mut(self) -> &'this mut V {
1271-
self.inner.into_mut()
1252+
fn into_mut(self) -> &#lt mut V {
1253+
#option_bucket_some::into_mut(self.inner)
12721254
}
12731255

12741256
#[inline]
12751257
fn insert(&mut self, value: V) -> V {
1276-
self.inner.replace(value)
1258+
#option_bucket_some::replace(&mut self.inner, value)
12771259
}
12781260

12791261
#[inline]
12801262
fn remove(self) -> V {
1281-
self.inner.take()
1263+
#option_bucket_some::take(self.inner)
12821264
}
12831265
}
12841266

1267+
#vis enum VacantEntry<#lt, V> {
1268+
Simple(SimpleVacantEntry<#lt, V>),
1269+
#(#vacant_variant,)*
1270+
}
1271+
1272+
#vis enum OccupiedEntry<#lt, V> {
1273+
Simple(SimpleOccupiedEntry<#lt, V>),
1274+
#(#occupied_variant,)*
1275+
}
1276+
12851277
#[automatically_derived]
1286-
impl<'this, V> #vacant_entry_trait<'this, #ident, V> for VacantEntry<'this, V> {
1278+
impl<#lt, V> #vacant_entry_trait<#lt, #ident, V> for VacantEntry<#lt, V> {
12871279
#[inline]
12881280
fn key(&self) -> #ident {
12891281
match self {
1290-
VacantEntry::Simple(entry) => entry.key(),
1282+
VacantEntry::Simple(entry) => entry.key,
12911283
#(#vacant_key,)*
12921284
}
12931285
}
12941286

12951287
#[inline]
1296-
fn insert(self, value: V) -> &'this mut V {
1288+
fn insert(self, value: V) -> &#lt mut V {
12971289
match self {
12981290
VacantEntry::Simple(entry) => entry.insert(value),
12991291
#(#vacant_insert,)*
@@ -1302,11 +1294,11 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
13021294
}
13031295

13041296
#[automatically_derived]
1305-
impl<'this, V> #occupied_entry_trait<'this, #ident, V> for OccupiedEntry<'this, V> {
1297+
impl<#lt, V> #occupied_entry_trait<#lt, #ident, V> for OccupiedEntry<#lt, V> {
13061298
#[inline]
13071299
fn key(&self) -> #ident {
13081300
match self {
1309-
OccupiedEntry::Simple(entry) => entry.key(),
1301+
OccupiedEntry::Simple(entry) => entry.key,
13101302
#(#occupied_key,)*
13111303
}
13121304
}
@@ -1328,7 +1320,7 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
13281320
}
13291321

13301322
#[inline]
1331-
fn into_mut(self) -> &'this mut V {
1323+
fn into_mut(self) -> &#lt mut V {
13321324
match self {
13331325
OccupiedEntry::Simple(entry) => entry.into_mut(),
13341326
#(#occupied_into_mut,)*
@@ -1353,7 +1345,7 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
13531345
}
13541346

13551347
#[inline]
1356-
fn option_to_entry<V>(opt: &mut #option<V>, key: #ident) -> #entry_enum<OccupiedEntry<'_, V>, VacantEntry<'_, V>> {
1348+
fn option_to_entry<V>(opt: &mut #option<V>, key: #ident) -> #entry_enum<'_, Storage<V>, #ident, V> {
13571349
match #option_bucket_option::new(opt) {
13581350
#option_bucket_option::Some(inner) => #entry_enum::Occupied(OccupiedEntry::Simple(SimpleOccupiedEntry { key, inner })),
13591351
#option_bucket_option::None(inner) => #entry_enum::Vacant(VacantEntry::Simple(SimpleVacantEntry { key, inner })),
@@ -1362,11 +1354,11 @@ fn build_entry_impl(cx: &Ctxt<'_>, field_specs: &[FieldSpec<'_>]) -> Result<Toke
13621354

13631355
#[automatically_derived]
13641356
impl<V> #storage_entry_trait<#ident, V> for Storage<V> {
1365-
type Occupied<'this> = OccupiedEntry<'this, V> where V: 'this;
1366-
type Vacant<'this> = VacantEntry<'this, V> where V: 'this;
1357+
type Occupied<#lt> = OccupiedEntry<#lt, V> where V: #lt;
1358+
type Vacant<#lt> = VacantEntry<#lt, V> where V: #lt;
13671359

13681360
#[inline]
1369-
fn entry(&mut self, key: #ident) -> #entry_enum<Self::Occupied<'_>, Self::Vacant<'_>> {
1361+
fn entry(&mut self, key: #ident) -> #entry_enum<'_, Self, #ident, V> {
13701362
match key {
13711363
#(#init,)*
13721364
}

fixed-map-derive/src/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ impl Toks {
5858
occupied_entry_trait: quote!(#krate::storage::entry::OccupiedEntry),
5959
vacant_entry_trait: quote!(#krate::storage::entry::VacantEntry),
6060
entry_enum: quote!(#krate::storage::entry::Entry),
61-
option_bucket_option: quote!(#krate::storage::entry::option_bucket::OptionBucket),
62-
option_bucket_some: quote!(#krate::storage::entry::option_bucket::SomeBucket),
63-
option_bucket_none: quote!(#krate::storage::entry::option_bucket::NoneBucket),
61+
option_bucket_option: quote!(#krate::option_bucket::OptionBucket),
62+
option_bucket_some: quote!(#krate::option_bucket::SomeBucket),
63+
option_bucket_none: quote!(#krate::option_bucket::NoneBucket),
6464
}
6565
}
6666
}

0 commit comments

Comments
 (0)