github.com/pgavlin/text@v0.0.0-20240419000839-8438d0a47805/writer.go (about)

     1  package text
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/pgavlin/text/internal/bytealg"
     7  )
     8  
     9  // Writer is the interface that wraps the WriteText method.
    10  type Writer[S String] interface {
    11  	WriteText(s S) (int, error)
    12  }
    13  
    14  type asWriter[S String] struct {
    15  	w io.StringWriter
    16  }
    17  
    18  func (w asWriter[S]) WriteText(s S) (int, error) {
    19  	return w.w.WriteString(bytealg.AsString(s))
    20  }
    21  
    22  // AsWriter projects an io.StringWriter as a Writer[S].
    23  func AsWriter[S String](w io.StringWriter) Writer[S] {
    24  	return asWriter[S]{w: w}
    25  }