@@ -81,6 +81,7 @@ impl Object for Dict {
81
81
}
82
82
}
83
83
84
+ #[ allow( unused_doc_comments) ]
84
85
impl Dict {
85
86
pub fn new ( ) -> Self {
86
87
Self {
@@ -100,8 +101,25 @@ impl Dict {
100
101
self . shift_remove ( & RefValue :: from ( key) ) // fixme: improve lookup!
101
102
}
102
103
103
- tokay_method ! ( "dict : @" , Ok ( RefValue :: from ( Dict :: new( ) ) ) ) ;
104
+ /** Creates a new `dict`.
104
105
106
+ Any provided `nargs` become key-value-pairs in the newly created dict.
107
+
108
+ This can also be shortcut by the `()` syntax.
109
+ */
110
+ tokay_method ! (
111
+ "dict : @**nargs" ,
112
+ Ok ( RefValue :: from( if let Some ( nargs) = nargs {
113
+ nargs. clone( )
114
+ } else {
115
+ Dict :: new( )
116
+ } ) )
117
+ ) ;
118
+
119
+ /** Creates an iterator over a `dict`.
120
+
121
+ The iterator is a method-iterator calling `iter_values()`.
122
+ */
105
123
tokay_method ! ( "dict_iter : @dict" , {
106
124
// If index is void, create an iterator on keys.
107
125
if dict. is( "dict" ) {
@@ -121,6 +139,7 @@ impl Dict {
121
139
}
122
140
} ) ;
123
141
142
+ /// Returns the number of items in the `dict`.
124
143
tokay_method ! ( "dict_len : @dict" , {
125
144
let dict = dict. borrow( ) ;
126
145
@@ -136,6 +155,7 @@ impl Dict {
136
155
}
137
156
} ) ;
138
157
158
+ /// Clone `dict` into a standalone copy.
139
159
tokay_method ! ( "dict_clone : @dict" , {
140
160
let dict = dict. borrow( ) ;
141
161
@@ -151,7 +171,12 @@ impl Dict {
151
171
}
152
172
} ) ;
153
173
154
- // Method to retrieve or iterate the keys of a dict.
174
+ /** Retrieve or iterate the keys of a `dict`.
175
+
176
+ When no `index` is given, the method returns an iterator over the keys.
177
+ Otherwise, the key at the provided `index` is returned, or `default` in
178
+ case the `index` is out of bounds.
179
+ */
155
180
tokay_method ! ( "dict_keys : @dict, index=void, default=void" , {
156
181
// If index is void, create an iterator on keys.
157
182
if index. is_void( ) {
@@ -181,7 +206,12 @@ impl Dict {
181
206
}
182
207
} ) ;
183
208
184
- // Method to retrieve or iterate the values of a dict.
209
+ /** Retrieve or iterate the values of a `dict`.
210
+
211
+ When no `index` is given, the method returns an iterator over the values.
212
+ Otherwise, the value at the provided `index` is returned, or `default` in
213
+ case the `index` is out of bounds.
214
+ */
185
215
tokay_method ! ( "dict_values : @dict, index=void, default=void" , {
186
216
// If index is void, create an iterator on keys.
187
217
if index. is_void( ) {
@@ -211,7 +241,14 @@ impl Dict {
211
241
}
212
242
} ) ;
213
243
214
- // Method to retrieve or iterate a list of [key, value] from a dict by index
244
+ /** Retrieve or iterate both keys and values of a `dict`.
245
+
246
+ The function returns a list of key-value for each result.
247
+
248
+ When no `index` is given, the method returns an iterator over the key-value-pairs.
249
+ Otherwise, the key-value-pair at the provided `index` is returned, or `default` in
250
+ case the `index` is out of bounds.
251
+ */
215
252
tokay_method ! ( "dict_items : @dict, index=void, default=void" , {
216
253
// If index is void, create an iterator on items.
217
254
if index. is_void( ) {
@@ -241,21 +278,25 @@ impl Dict {
241
278
}
242
279
} ) ;
243
280
244
- tokay_method ! ( "dict_get_item : @dict, item, default=void" , {
245
- if !item. is_hashable( ) {
281
+ /** Retrieve item with `key` from `dict`. Returns `default` when key is not found.
282
+
283
+ This method is also invoked when using the `dict` item syntax.
284
+ */
285
+ tokay_method ! ( "dict_get_item : @dict, key, default=void" , {
286
+ if !key. is_hashable( ) {
246
287
return Err ( Error :: from( format!(
247
288
"{} unhashable type '{}'" ,
248
289
__function,
249
- item . name( )
290
+ key . name( )
250
291
) ) ) ;
251
292
}
252
293
253
294
// todo: alias dict_get
254
295
let dict = dict. borrow( ) ;
255
296
256
297
if let Some ( dict) = dict. object:: <Dict >( ) {
257
- if let Some ( item ) = dict. get( & item ) {
258
- Ok ( item . clone( ) )
298
+ if let Some ( key ) = dict. get( & key ) {
299
+ Ok ( key . clone( ) )
259
300
} else {
260
301
Ok ( default )
261
302
}
@@ -269,23 +310,32 @@ impl Dict {
269
310
}
270
311
} ) ;
271
312
272
- tokay_method ! ( "dict_set_item : @dict, item, value=void" , {
273
- if !item. is_hashable( ) {
313
+ /** Insert or replace `value` under the given `key` in `dict`.
314
+
315
+ When `value` is provided as void, the key is removed.
316
+
317
+ Returns the previous item's value if the key already existed in `dict`,
318
+ otherwise void.
319
+
320
+ This method is also invoked when assigning to a `dict` item.
321
+ */
322
+ tokay_method ! ( "dict_set_item : @dict, key, value=void" , {
323
+ if !key. is_hashable( ) {
274
324
return Err ( Error :: from( format!(
275
325
"{} unhashable type '{}'" ,
276
326
__function,
277
- item . name( )
327
+ key . name( )
278
328
) ) ) ;
279
329
}
280
330
281
331
let mut dict = dict. borrow_mut( ) ;
282
332
283
333
if let Some ( dict) = dict. object_mut:: <Dict >( ) {
284
334
if value. is_void( ) {
285
- dict. shift_remove( & item ) ;
335
+ dict. shift_remove( & key ) ;
286
336
Ok ( value![ void] )
287
337
} else {
288
- dict. insert( item , value. clone( ) ) ;
338
+ dict. insert( key , value. clone( ) ) ;
289
339
Ok ( value)
290
340
}
291
341
} else {
@@ -298,6 +348,7 @@ impl Dict {
298
348
}
299
349
} ) ;
300
350
351
+ /** Merges dict `other` into `dict`. */
301
352
tokay_method ! ( "dict_merge : @dict, other" , {
302
353
{
303
354
let dict = & mut * dict. borrow_mut( ) ;
@@ -330,25 +381,9 @@ impl Dict {
330
381
Ok ( dict)
331
382
} ) ;
332
383
333
- tokay_method ! ( "dict_push : @dict, key, value" , {
334
- let dict = & mut * dict. borrow_mut( ) ;
335
-
336
- if let Some ( dict) = dict. object_mut:: <Dict >( ) {
337
- Ok ( if let Some ( old) = dict. insert( key, value) {
338
- old
339
- } else {
340
- value!( void)
341
- } )
342
- } else {
343
- Err ( Error :: from( format!(
344
- "{} only accepts '{}' as parameter, not '{}'" ,
345
- __function,
346
- "dict" ,
347
- dict. name( )
348
- ) ) )
349
- }
350
- } ) ;
384
+ /** Returns and removes `key` from `dict`.
351
385
386
+ When the given `key` does not exist, `default` will be returned, */
352
387
tokay_method ! ( "dict_pop : @dict, key=void, default=void" , {
353
388
let dict = & mut * dict. borrow_mut( ) ;
354
389
0 commit comments