Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[breakchange] refact lazy local variable #403

Merged
merged 18 commits into from
Nov 26, 2024

Conversation

poor-circle
Copy link
Contributor

@poor-circle poor-circle commented Nov 21, 2024

Why

Now, the lazy local is a void*, which is dangerous, and user need manage life-time manually.

What is changing

Refact lazy local. Now we allow user add local variable derived from LazyLocalBaseImpl. we aslo support SimpleLazyLocal for simple local variable case.

Example

struct mylocal : public LazyLocalBaseImpl<mylocal> {
    mylocal(std::string name) : _name(std::move(name)) {}
    std::string& name() { return _name; }
    std::string _name;
};

Lazy<void> task() {
  // if no local or local type is not corret, return nullptr
  mylocal* local =co_await CurrentLazyLocals<mylocal>{};
  assert(base->name()=="Hello");
  local->name()="HI";

  // if no local variable, return nullptr
  LazyLocalBase* local =co_await CurrentLazyLocals{};
  assert(local->empty()==false);
}
// local-variable will life until callback finished. User dont need to manage it.
task().setLazyLocal<mylocal>("Hello"s).start([](auto&&){
});

The type check is very fast (only need compare a pointer). Dont need RTTI.

async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
@poor-circle poor-circle force-pushed the refact_lazy_local branch 2 times, most recently from 246ff34 to 4289594 Compare November 21, 2024 12:23
async_simple/coro/LazyLocalBase.h Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/Lazy.h Outdated Show resolved Hide resolved
async_simple/coro/Lazy.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Show resolved Hide resolved
async_simple/coro/Lazy.h Outdated Show resolved Hide resolved
async_simple/coro/Lazy.h Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Show resolved Hide resolved
async_simple/coro/LazyLocalBase.h Outdated Show resolved Hide resolved
@ChuanqiXu9 ChuanqiXu9 merged commit 80730e1 into alibaba:main Nov 26, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants