Skip to content

Commit ee55a45

Browse files
more wip
1 parent 694da1c commit ee55a45

1 file changed

Lines changed: 43 additions & 16 deletions

File tree

src/types/frozendict.rs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::ffi::Py_ssize_t;
33
use crate::ffi_ptr_ext::FfiPtrExt;
44
use crate::instance::Bound;
55
use crate::py_result_ext::PyResultExt;
6-
use crate::types::{PyAny, PyList, PyMapping};
6+
use crate::types::{PyAny, PyList, PyListMethods, PyMapping};
77
use crate::{ffi, Borrowed, BoundObject, IntoPyObject, IntoPyObjectExt, Python};
88
#[cfg(RustPython)]
99
use crate::{
@@ -222,7 +222,7 @@ impl<'py> PyFrozenDictMethods<'py> for Bound<'py, PyFrozenDict> {
222222
where
223223
K: IntoPyObject<'py>,
224224
{
225-
fn inner(
225+
fn inner<'py>(
226226
fd: &Bound<'py, PyFrozenDict>,
227227
key: Borrowed<'_, '_, PyAny>,
228228
) -> PyResult<Option<Bound<'py, PyAny>>> {
@@ -314,13 +314,12 @@ impl<'py> Iterator for BoundFrozenDictIterator<'py> {
314314

315315
fn next(&mut self) -> Option<Self::Item> {
316316
unsafe {
317-
let mut ppos: Py_ssize_t = self.ppos as Py_ssize_t;
317+
let mut ppos: *mut ffi::Py_ssize_t = &mut self.ppos;
318318
let mut key: *mut ffi::PyObject = core::ptr::null_mut();
319319
let mut value: *mut ffi::PyObject = core::ptr::null_mut();
320320

321-
if unsafe { ffi::PyDict_Next(dict.as_ptr(), ppos, &mut key, &mut value) != 0 } {
322-
*remaining -= 1;
323-
let py = dict.py();
321+
if unsafe { ffi::PyDict_Next(self.fd.as_ptr(), ppos, &mut key, &mut value) != 0 } {
322+
let py = self.py();
324323
// Safety:
325324
// - PyDict_Next returns borrowed values
326325
// - we have already checked that `PyDict_Next` succeeded, so we can assume these to be non-null
@@ -333,6 +332,25 @@ impl<'py> Iterator for BoundFrozenDictIterator<'py> {
333332
}
334333
}
335334
}
335+
336+
fn size_hint(&self) -> (usize, Option<usize>) {
337+
let len = ExactSizeIterator::len(self);
338+
(len, Some(len))
339+
}
340+
341+
#[inline]
342+
fn count(self) -> usize
343+
where
344+
Self: Sized,
345+
{
346+
self.fd.len()
347+
}
348+
}
349+
350+
impl ExactSizeIterator for BoundFrozenDictIterator<'_> {
351+
fn len(&self) -> usize {
352+
self.fd.len()
353+
}
336354
}
337355

338356
#[cfg(Py_3_15)]
@@ -346,22 +364,31 @@ impl<'py> IntoIterator for Bound<'py, PyFrozenDict> {
346364
}
347365
}
348366

367+
impl<'py> IntoIterator for &Bound<'py, PyFrozenDict> {
368+
type Item = (Bound<'py, PyAny>, Bound<'py, PyAny>);
369+
type IntoIter = BoundFrozenDictIterator<'py>;
370+
371+
fn into_iter(self) -> Self::IntoIter {
372+
self.iter()
373+
}
374+
}
375+
349376
#[cfg(all(Py_3_15, test))]
350377
mod tests {
351378
use super::*;
352379

353380
#[test]
354381
fn test_frozendict_new() {
355382
Python::attach(|py| {
356-
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)])?;
383+
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)]).unwrap();
357384
assert_eq!(fd.len(), 2);
358385
})
359386
}
360387

361388
#[test]
362389
fn test_frozendict_empty() {
363390
Python::attach(|py| {
364-
let fd = PyFrozenDict::empty(py)?;
391+
let fd = PyFrozenDict::empty(py).unwrap();
365392
assert!(fd.is_empty());
366393
assert_eq!(fd.len(), 0);
367394
})
@@ -370,7 +397,7 @@ mod tests {
370397
#[test]
371398
fn test_frozendict_contains() {
372399
Python::attach(|py| {
373-
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)])?;
400+
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)]).unwrap();
374401
assert!(fd.contains("a")?);
375402
assert!(!fd.contains("c")?);
376403
})
@@ -379,7 +406,7 @@ mod tests {
379406
#[test]
380407
fn test_frozendict_get_item() {
381408
Python::attach(|py| {
382-
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)])?;
409+
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)]).unwrap();
383410
let val = fd.get_item("a")?;
384411
assert!(val.is_some());
385412
})
@@ -388,7 +415,7 @@ mod tests {
388415
#[test]
389416
fn test_frozendict_keys() {
390417
Python::attach(|py| {
391-
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)])?;
418+
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)]).unwrap();
392419
let keys = fd.keys();
393420
assert_eq!(keys.len(), 2);
394421
})
@@ -397,7 +424,7 @@ mod tests {
397424
#[test]
398425
fn test_frozendict_values() {
399426
Python::attach(|py| {
400-
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)])?;
427+
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)]).unwrap();
401428
let values = fd.values();
402429
assert_eq!(values.len(), 2);
403430
})
@@ -406,7 +433,7 @@ mod tests {
406433
#[test]
407434
fn test_frozendict_items() {
408435
Python::attach(|py| {
409-
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)])?;
436+
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)]).unwrap();
410437
let items = fd.items();
411438
assert_eq!(items.len(), 2);
412439
})
@@ -415,7 +442,7 @@ mod tests {
415442
#[test]
416443
fn test_frozendict_iter() {
417444
Python::attach(|py| {
418-
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)])?;
445+
let fd = PyFrozenDict::new(py, vec![("a", 1), ("b", 2)]).unwrap();
419446
let mut count = 0;
420447
for (_k, _v) in &fd {
421448
count += 1;
@@ -427,15 +454,15 @@ mod tests {
427454
#[test]
428455
fn test_frozendict_as_mapping() {
429456
Python::attach(|py| {
430-
let fd = PyFrozenDict::new(py, vec![("a", 1)])?;
457+
let fd = PyFrozenDict::new(py, vec![("a", 1)]).unwrap();
431458
let _mapping = fd.as_mapping();
432459
})
433460
}
434461

435462
#[test]
436463
fn test_frozendict_hash() {
437464
Python::attach(|py| {
438-
let fd = PyFrozenDict::new(py, vec![("a", 1)])?;
465+
let fd = PyFrozenDict::new(py, vec![("a", 1)]).unwrap();
439466
let h = fd.hash()?;
440467
assert!(h != 0 || fd.is_empty());
441468
})

0 commit comments

Comments
 (0)