1 // Copyright 2020 Google LLC All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package and provides helpers for adding Close to io.{Reader|Writer}. 16 package and 17 18 import ( 19 "io" 20 ) 21 22 // ReadCloser implements io.ReadCloser by reading from a particular io.Reader 23 // and then calling the provided "Close()" method. 24 type ReadCloser struct { 25 io.Reader 26 CloseFunc func() error 27 } 28 29 var _ io.ReadCloser = (*ReadCloser)(nil) 30 31 // Close implements io.ReadCloser 32 func (rac *ReadCloser) Close() error { 33 return rac.CloseFunc() 34 } 35 36 // WriteCloser implements io.WriteCloser by reading from a particular io.Writer 37 // and then calling the provided "Close()" method. 38 type WriteCloser struct { 39 io.Writer 40 CloseFunc func() error 41 } 42 43 var _ io.WriteCloser = (*WriteCloser)(nil) 44 45 // Close implements io.WriteCloser 46 func (wac *WriteCloser) Close() error { 47 return wac.CloseFunc() 48 } 49