using A for *; # 把 A 的函数附给任意类型使用
using A for B; # 意思是把 A 中的方法附给 B 使用
使用上面的方式,那么在我们的合约中定义了 B 类型的变量后,就可以像 B.xx() 这样使用 A 库的函数了。
举个局部例子:_miners 可以直接使用 has 方法。
library Roles { struct Role { mapping (address => bool) bearer; } /** * @dev Check if an account has this role. * @return bool */ function has(Role storage role, address account) internal view returns (bool) { require(account != address(0), "Roles: account is the zero address"); return role.bearer[account]; } } contract MinterRole is Context { using Roles for Roles.Role; event MinterAdded(address indexed account); event MinterRemoved(address indexed account); Roles.Role private _minters; constructor () internal { _addMinter(_msgSender()); } modifier onlyMinter() { require(isMinter(_msgSender()), "MinterRole: caller does not have the Minter role"); _; } function isMinter(address account) public view returns (bool) { return _minters.has(account); } }