固体 - openzeppeling/utils/counters问题

发布于 2025-02-03 18:45:32 字数 577 浏览 4 评论 0原文

当我们使用计数器库时,我们通常会这样启动

 using Counters for Counters.Counter;
 Counters.Counter private _tokenIds;

。使用计数器库方法进行计数器。Counter(库中的结构),并分配_tokenIDS指向该结构。 (+ - ?酷。)

是什么让我感到困惑的是计数器内部的函数定义?即

function current(Counter storage counter) internal view returns (uint256) {
  return counter._value;
}

function increment(Counter storage counter) internal {
  unchecked {
    counter._value += 1;
  }
}

功能所采用的可变称为计数器?它不是期望的论点吗? 我们定义的_ tokenids与较小案例计数器之间的链接在哪里? 我不知道为什么我觉得这么困惑,但似乎我缺少某些东西(即使我知道它没有缺少,只是没有理解)。

提前致谢。

When we use the Counters library, we init it usually as such

 using Counters for Counters.Counter;
 Counters.Counter private _tokenIds;

so far all good. Using Counters library methods for Counters.Counter (the struct in the library) and assigning _tokenIds to point to that struct. (+-? cool.)

What confuses me is the function definitions inside Counters; i.e

function current(Counter storage counter) internal view returns (uint256) {
  return counter._value;
}

function increment(Counter storage counter) internal {
  unchecked {
    counter._value += 1;
  }
}

The function takes in a varaible called counter ? is it not expecting an argument ?
Where is the link between our defined _tokenIds to the smaller-case counter ?
I don't know why I find this so confusing but it seems like something's missing to me (even tho I know its not missing, just failing to understand).

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2025-02-10 18:45:32

使用< library>对于< type>表达式允许您在此类型的变量上使用库的功能。当您将其称为成员函数时,它会自动将变量作为函数的第一个参数传递。

因此,就您而言,counters.current(_tokenIDS)(库函数)与_TokenIds.Current()(成员函数)相同。

文档: https:// https://docs.solitylang.orgg/en/ en/ v0.8.14/contracts.html#使用for

The using <library> for <type> expression allows you to use functions of the library on variables of this type. And it automatically passes the variable as the first argument of the function when you're calling it as a member function.

So in your case, Counters.current(_tokenIds) (library function) is the same as _tokenIds.current() (member function).

Docs: https://docs.soliditylang.org/en/v0.8.14/contracts.html#using-for

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文