From 0d3e78ad944f61efdf908e26c7d6a24df6cafe77 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Mon, 29 Jul 2024 23:17:21 +0800 Subject: [PATCH] asyncio: x/io -> x/async --- x/{io => async}/README.md | 0 x/{io => async}/_demo/asyncdemo/async.go | 30 ++++++++++++------------ x/{io/io.go => async/async.go} | 2 +- x/{io => async}/naive/extra.go | 18 +++++++------- x/{io => async}/naive/naive.go | 0 5 files changed, 25 insertions(+), 25 deletions(-) rename x/{io => async}/README.md (100%) rename x/{io => async}/_demo/asyncdemo/async.go (94%) rename x/{io/io.go => async/async.go} (99%) rename x/{io => async}/naive/extra.go (92%) rename x/{io => async}/naive/naive.go (100%) diff --git a/x/io/README.md b/x/async/README.md similarity index 100% rename from x/io/README.md rename to x/async/README.md diff --git a/x/io/_demo/asyncdemo/async.go b/x/async/_demo/asyncdemo/async.go similarity index 94% rename from x/io/_demo/asyncdemo/async.go rename to x/async/_demo/asyncdemo/async.go index 3cccb9ed..a0d49fd9 100644 --- a/x/io/_demo/asyncdemo/async.go +++ b/x/async/_demo/asyncdemo/async.go @@ -7,8 +7,8 @@ import ( "strings" "time" - "github.com/goplus/llgo/x/io" - "github.com/goplus/llgo/x/io/naive" + "github.com/goplus/llgo/x/async" + "github.com/goplus/llgo/x/async/naive" "github.com/goplus/llgo/x/tuple" ) @@ -37,7 +37,7 @@ type Response struct { Body string } -func (r *Response) Text() (co *io.Promise[tuple.Tuple2[string, error]]) { +func (r *Response) Text() (co *async.Promise[tuple.Tuple2[string, error]]) { co.Return(tuple.Tuple2[string, error]{V1: r.Body, V2: nil}) return } @@ -63,8 +63,8 @@ func (r *Response) TextCompiled() *naive.PromiseImpl[tuple.Tuple2[string, error] // return resp, err // }) // } -func AsyncHttpGet(url string) *io.Promise[tuple.Tuple2[*Response, error]] { - co := &io.Promise[tuple.Tuple2[*Response, error]]{} +func AsyncHttpGet(url string) *async.Promise[tuple.Tuple2[*Response, error]] { + co := &async.Promise[tuple.Tuple2[*Response, error]]{} http("GET", url, func(resp *Response, err error) { co.Return(tuple.Tuple2[*Response, error]{V1: resp, V2: nil}) }) @@ -91,8 +91,8 @@ func AsyncHttpGetCompiled(url string) *naive.PromiseImpl[tuple.Tuple2[*Response, return co } -func AsyncHttpPost(url string) *io.Promise[tuple.Tuple2[*Response, error]] { - co := &io.Promise[tuple.Tuple2[*Response, error]]{} +func AsyncHttpPost(url string) *async.Promise[tuple.Tuple2[*Response, error]] { + co := &async.Promise[tuple.Tuple2[*Response, error]]{} http("POST", url, func(resp *Response, err error) { co.Return(tuple.Tuple2[*Response, error]{V1: resp, V2: nil}) }) @@ -472,7 +472,7 @@ func GenUsersCompiled() (resolve *naive.PromiseImpl[User]) { return co } -func Demo() (co *io.Promise[io.Void]) { +func Demo() (co *async.Promise[async.Void]) { user, err := GetUser("1").Await().Values() log.Println(user, err) @@ -482,7 +482,7 @@ func Demo() (co *io.Promise[io.Void]) { users := naive.All[tuple.Tuple2[User, error]]([]naive.AsyncCall[tuple.Tuple2[User, error]]{GetUser("5"), GetUser("6"), GetUser("7")}).Value() log.Println(users, err) - user, score, _ := naive.Await3Compiled[User, float64, io.Void](GetUser("8"), GetScore(), DoUpdate("update sth.")).Value().Values() + user, score, _ := naive.Await3Compiled[User, float64, async.Void](GetUser("8"), GetScore(), DoUpdate("update sth.")).Value().Values() log.Println(user, score, err) // for loop with generator @@ -513,16 +513,16 @@ func Demo() (co *io.Promise[io.Void]) { // log.Println("user:", user) // case score := <-GetScore().Chan(): // log.Println("score:", score) - // case <-io.Timeout(5 * time.Second).Chan(): + // case <-async.Timeout(5 * time.Second).Chan(): // log.Println("timeout") // } log.Println("Demo done") - co.Return(io.Void{}) + co.Return(async.Void{}) return } -func DemoCompiled() *naive.PromiseImpl[io.Void] { +func DemoCompiled() *naive.PromiseImpl[async.Void] { var state1 *naive.PromiseImpl[tuple.Tuple2[User, error]] var state2 *naive.PromiseImpl[tuple.Tuple2[User, error]] var state3 *naive.PromiseImpl[[]tuple.Tuple2[User, error]] @@ -530,7 +530,7 @@ func DemoCompiled() *naive.PromiseImpl[io.Void] { var g1 *naive.PromiseImpl[int] var g2 *naive.PromiseImpl[User] - P := &naive.PromiseImpl[io.Void]{} + P := &naive.PromiseImpl[async.Void]{} P.Debug = "Demo" P.Func = func() { switch P.Next { @@ -597,7 +597,7 @@ func DemoCompiled() *naive.PromiseImpl[io.Void] { if g2.Done() { P.Next = -1 log.Printf("Demo done\n") - P.Return(io.Void{}) + P.Return(async.Void{}) return } log.Printf("genUser: %+v, done: %v\n", g2.Value(), g2.Done()) @@ -612,7 +612,7 @@ func DemoCompiled() *naive.PromiseImpl[io.Void] { func main() { log.SetFlags(log.Lshortfile | log.LstdFlags) log.Printf("=========== Run Naive Demo ===========\n") - v := naive.RunImpl[io.Void](DemoCompiled()) + v := naive.RunImpl[async.Void](DemoCompiled()) log.Println(v) log.Printf("=========== Run Naive Demo finished ===========\n") diff --git a/x/io/io.go b/x/async/async.go similarity index 99% rename from x/io/io.go rename to x/async/async.go index b6afc62e..83b4af40 100644 --- a/x/io/io.go +++ b/x/async/async.go @@ -14,7 +14,7 @@ * limitations under the License. */ -package io +package async import ( "unsafe" diff --git a/x/io/naive/extra.go b/x/async/naive/extra.go similarity index 92% rename from x/io/naive/extra.go rename to x/async/naive/extra.go index e92bf37d..21c00af3 100644 --- a/x/io/naive/extra.go +++ b/x/async/naive/extra.go @@ -22,19 +22,19 @@ import ( "time" _ "unsafe" - "github.com/goplus/llgo/x/io" + "github.com/goplus/llgo/x/async" "github.com/goplus/llgo/x/tuple" ) // ----------------------------------------------------------------------------- -func TimeoutCompiled(d time.Duration) *PromiseImpl[io.Void] { - P := &PromiseImpl[io.Void]{} +func TimeoutCompiled(d time.Duration) *PromiseImpl[async.Void] { + P := &PromiseImpl[async.Void]{} P.Debug = "Timeout" P.Func = func() { go func() { time.Sleep(d) - P.Return(io.Void{}) + P.Return(async.Void{}) }() } return P @@ -79,7 +79,7 @@ func Race[OutT any](acs ...AsyncCall[OutT]) *PromiseImpl[OutT] { for _, p := range ps { if p.Done() { if debugAsync { - log.Printf("io.Race done: %+v won the race\n", p) + log.Printf("async.Race done: %+v won the race\n", p) } returned = true P.Return(p.value) @@ -122,7 +122,7 @@ func All[OutT any](acs []AsyncCall[OutT]) *PromiseImpl[[]OutT] { for _, p := range ps { if !p.Done() { - log.Fatalf("io.All: not done: %+v\n", p) + log.Fatalf("async.All: not done: %+v\n", p) } } @@ -131,7 +131,7 @@ func All[OutT any](acs []AsyncCall[OutT]) *PromiseImpl[[]OutT] { ret[idx] = p.value } if debugAsync { - log.Printf("io.All done: %+v\n", ret) + log.Printf("async.All done: %+v\n", ret) } P.Return(ret) return @@ -170,7 +170,7 @@ func Await2Compiled[OutT1, OutT2 any]( } P.Next = -1 if !p1.Done() || !p2.Done() { - log.Fatalf("io.Await2: not done: %+v, %+v\n", p1, p2) + log.Fatalf("async.Await2: not done: %+v, %+v\n", p1, p2) } P.Return(tuple.Tuple3[OutT1, OutT2, error]{ @@ -220,7 +220,7 @@ func Await3Compiled[OutT1, OutT2, OutT3 any]( P.Next = -1 // TODO(lijie): return every error? if !p1.Done() || !p2.Done() || !p3.Done() { - log.Fatalf("io.Await3: not done: %+v, %+v, %+v\n", p1, p2, p3) + log.Fatalf("async.Await3: not done: %+v, %+v, %+v\n", p1, p2, p3) } P.Return(tuple.Tuple3[OutT1, OutT2, OutT3]{ diff --git a/x/io/naive/naive.go b/x/async/naive/naive.go similarity index 100% rename from x/io/naive/naive.go rename to x/async/naive/naive.go