quartiq / miniconf

Rust serialize/deserialize/access reflection for trees (no_std, no alloc)
MIT License
23 stars 2 forks source link

`Miniconf::iter()` method conflicts with `core::slice::iter()` #87

Closed ryan-summers closed 2 years ago

ryan-summers commented 2 years ago

The iter() method can cause confusion when being called on array objects.

Because of Rust's expression resolution ordering, an array does not natively implement the iter() method. Instead, that function is implemented on slices. Because of this, calling iter() on an array will find Miniconf::iter() instead of trying to convert the array to a slice and then looking up functions.

In this case:

  1. Rust fails to find the iter() method on the array object because it indeed has no iter() function.
  2. Rust looks at trait impls for iter() on the array object. In our case, it finds iter() because of trait Miniconf and uses that

However, this may be undesirable because the user might be trying to coerce the array object into a slice to call the slice iterator method. Because of the resolution ordering, the Miniconf iter() method is selected instead.

The workaround for this defect is to first explicitly convert the array object into a slice as follows:

let mut array = [0f32; 5];
// Finds Miniconf::iter();
array.iter()
// Finds core::slice::iter
array[..].iter();

We likely want to rename the iter() method to prevent this confusion in the future.