package future

import (
	
)

type FutureResolver func(err error)

// A Future represents a value that will be available in the Future.
// It is always associated with a context that can be used to wait for the value to be available.
// When the parent context is canceled, the Future will be canceled as well.
type Future struct {
	ctx context.Context
}

func ( *Future) () <-chan struct{} {
	return .ctx.Done()
}

func ( *Future) () error {
	<-.ctx.Done()

	 := context.Cause(.ctx)
	if  != nil {
		if ,  := .(*futureResolution);  {
			return .err
		}
	}
	return 
}

// Wait waits for the future to complete and returns any error that occurred.
func ( *Future) () error {
	return .Err()
}

func ( context.Context) (*Future, FutureResolver) {
	,  := context.WithCancelCause()
	 := &Future{
		ctx: ,
	}
	return , func( error) {
		(&futureResolution{
			err: ,
		})
	}
}

type futureResolution struct {
	err error
}

func ( *futureResolution) () string {
	if .err != nil {
		return .err.Error()
	}
	return "future resolved"
}