Skip to content

A head-const (aka read-only, aka final) adapter for ranges.

License

Notifications You must be signed in to change notification settings

SirNickolas/readonly_range.d

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

readonly_range

This library provides a simple wrapper that makes the passed range head-const: it prevents reassigning the elements but otherwise does not affect their constness. This is essentially the opposite of Rebindable.

import readonly_range;

struct S {
    int field;
}

auto data = [new S(1), new S(2), new S(3)];
auto view = data.readonlyRange;
// view[0] = null; // Forbidden.
view[0].field = 13; // Allowed.
assert(data[0].field == 13);
const(S*)[ ] constView = view; // Implicit conversion.

It works by making front, back, and opIndex return the element by value—keep that in mind and avoid using it for types that are expensive to copy. Note that in the example above, data is S*[ ] so view[0] copies just the pointer, not the pointed-to object.

About

A head-const (aka read-only, aka final) adapter for ranges.

Topics

Resources

License

Stars

Watchers

Forks

Languages