Skip to content

Allow "extension" of types from other modules #20912

@jethrogb

Description

@jethrogb

I'll start with some example code:

use std::intrinsics::cttz32;
use std::collections::{Bitv,BitvSet};

pub trait LowestBit
{
    fn lowest_bit(&self) -> Option<usize>;
}

impl LowestBit for Bitv
{
    fn lowest_bit(&self) -> Option<usize>
    {
        for (idx,&word) in self.storage.iter().enumerate()
        {
            if (word==0) { continue }
            return Some(idx*32+(unsafe {cttz32(word) as usize}));
        }
        return None;
    }
}

fn main() {
    let mut v=BitvSet::new();
    v.insert(1242);
    println!("{}",v.get_ref().lowest_bit().unwrap());
}

This currently fails on the privacy of the storage member of Bitv:

tmp.rs:22:22: 22:34 error: field `storage` of struct `collections::bit::Bitv` is private
tmp.rs:22       for (idx,&word) in self.storage.iter().enumerate()

I'm not sure if the code above should "just work", but I do believe there should be a way to say "I know what I'm doing, just give me access to private members," or perhaps something along the line of "protected" members. In the example above, it would be impractical to reimplement Bitv just to add the lowest_bit function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions