latticexyz / mud

MUD is a framework for building autonomous worlds
https://mud.dev
MIT License
716 stars 178 forks source link

codegen Bytes.sol #2407

Open holic opened 6 months ago

holic commented 6 months ago

This defines the ~same function multiple times for each bytesN type: https://github.com/latticexyz/mud/blob/main/packages/store/src/Bytes.sol

Probably best if we codegen this and also codegen corresponding tests.

yonadaaa commented 6 months ago

I think it's probably fine to manually define this one because it will never change? Also seems gnarly to codegen

holic commented 6 months ago

I think it's probably fine to manually define this one because it will never change? Also seems gnarly to codegen

new Array(32).fill(null).map((, i) => {
  const byteLength = i + 1;
  return `
    function getBytes${byteLength}(bytes32 data, uint256 start) internal pure returns (bytes${byteLength} output) {
      assembly {
        output := shl(mul(8, start), data)
      }
    }
    function getBytes${byteLength}(bytes memory data, uint256 start) internal pure returns (bytes${byteLength} output) {
      assembly {
        output := mload(add(add(data, 0x20), start))
      }
    }
  `;
}).join('\n\n');