How to implement custom EnumerableMap

I’m trying to create a custom EnumerableMap, similar to UintToAddressMap (but in my case is → uint256). I created a new library and copied and adapted the code of UintToAddressMap, that uses Map as inner implementation. But it doesn’t works becase Map’s functions are defined as private.

How we are supposed to implement custom (not UintToAddress) maps?

If only Map is made public, it can be easily reimplemented just adding the adaptation functions.

Hi gnarvaja welcome to Open Zeppelin!

Please post your code and we can take a look to see where the code is going wrong.

Sorry the delay, I was busy.

For example I want to do a AddressToUintMap, implemented with the inner Map the same way EnumerableMap.UintToAddressMap does…

pragma solidity ^0.8.0;

import {EnumerableMap} from "@openzeppelin/contracts/utils/structs/EnumerableMap.sol";

library DataTypes2 {

    // AddressToUintMap

    struct AddressToUintMap {
        EnumerableMap.Map _inner;
    }

    function _address_to_b32(address addr) internal pure returns (bytes32) {
      return bytes32(uint256(uint160(addr)));
    }

    function _b32_to_address(bytes32 b32) internal pure returns (address) {
      return address(uint160(uint256(b32)));
    }

    /**
     * @dev Adds a key-value pair to a map, or updates the value for an existing
     * key. O(1).
     *
     * Returns true if the key was added to the map, that is if it was not
     * already present.
     */
    function set(AddressToUintMap storage map, address key, uint256 value) internal returns (bool) {
        return EnumerableMap._set(map._inner, _address_to_b32(key), bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the key was removed from the map, that is if it was present.
     */
    function remove(AddressToUintMap storage map, address key) internal returns (bool) {
        return EnumerableMap._remove(map._inner, _address_to_b32(key));
    }

...

But when I try to compile I get this error:

TypeError: Member "_set" not found or not visible after argument-dependent lookup in type(library EnumerableMap).
  --> contracts/DataTypes-2.sol:31:16:
   |
31 |         return EnumerableMap._set(map._inner, _address_to_b32(key), bytes32(value));
   |                ^^^^^^^^^^^^^^^^^^

But if I edit manually EnumerableMap and change all private for public, then it compiles and allows me to have my custom enumerable maps without copying all the Map code.

Or perhaps there is another right way to produce custom EnumerableMaps…