Skip to content

感觉reset和rewind方法的名字可以互换一下 #2

Open

Description

@abevol

如果不考虑与Java的功能一致性的话

Activity

  1. CPythoner commented on Apr 3, 2026

    @CPythoner
    Owner

    @abevol 您好!

    关于您提出的 reset 和 rewind 方法名字互换的建议,我仔细分析了 Java ByteBuffer 的实现和本文档的说明。

    Java ByteBuffer 的定义

    根据 Java ByteBuffer 的官方文档:

    • rewind() - Rewinds this buffer. The position is set to zero and the mark is discarded.

      • 将 position 置 0,mark 置为 -1
      • 用于重新读取数据,不改变 limit
    • reset() - Resets this buffer's position to the previously-marked position.

      • 将 position 恢复到之前 mark 的位置
      • 需要先调用 mark() 设置 mark 值

    当前实现

    当前 C++ 实现与 Java 版本保持一致:

    ByteBuffer& reset()
    {
        if (mark_ >= 0)
            position_ = mark_;
        return *this;
    }
    
    ByteBuffer& rewind()
    {
        mark_ = -1;
        position_ = 0;
        return *this;
    }

    结论

    虽然方法名字从字面理解可能会产生混淆,但为了:

    1. 与 Java ByteBuffer API 保持一致 - 便于从 Java 迁移的开发者理解
    2. 符合原文档说明 - README 第 4.5 和 4.6 节已经详细说明了这两个方法的行为
    3. 避免破坏现有代码 - 如果已有代码使用这两个方法,互换名字会导致兼容性问题

    因此建议保持当前命名不变,关闭此 issue。

    如果您觉得方法名不够直观,我们可以考虑:

    1. 在代码注释中更清楚地说明两个方法的区别
    2. 添加别名方法(如 resetToMark() 和 rewindToStart())作为补充

    期待您的想法!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions