Skip to content

EncodeWide::size_hint doesn't account for self.extra #86414

@programmerjake

Description

@programmerjake

fn size_hint(&self) -> (usize, Option<usize>) {
let (low, high) = self.code_points.size_hint();
// every code point gets either one u16 or two u16,
// so this iterator is between 1 or 2 times as
// long as the underlying iterator.
(low, high.and_then(|n| n.checked_mul(2)))
}

I haven't tested it, but, on Windows the following test should pass with the current code, demonstrating how size_hint is currently broken:

#[test]
fn test() {
    let os_str: &OsStr = "\u{12345}".as_ref();
    let mut iter = os_str.encode_wide();
    iter.next().unwrap();
    // at this point iter.code_points is empty and iter.extra is non-zero
    assert_eq!((0, Some(0)), iter.size_hint());
    // size_hint returned 0, yet we didn't reach the end:
    iter.next().unwrap();
}

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.O-windowsOperating system: WindowsT-libsRelevant to the library team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions