Skip to content

Commit 1da05eb

Browse files
committed
spec: add unsafe.Add and unsafe.Slice
Updates #19367. Updates #40481. Change-Id: I578066ad68d2cd6bea50df1a534cf799e4404a7f Reviewed-on: https://go-review.googlesource.com/c/go/+/312212 Trust: Matthew Dempsky <[email protected]> Trust: Robert Griesemer <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 7405968 commit 1da05eb

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

doc/go_spec.html

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!--{
22
"Title": "The Go Programming Language Specification",
3-
"Subtitle": "Version of Apr 20, 2021",
3+
"Subtitle": "Version of Apr 21, 2021",
44
"Path": "/ref/spec"
55
}-->
66

@@ -6711,6 +6711,10 @@ <h3 id="Package_unsafe">Package <code>unsafe</code></h3>
67116711
func Alignof(variable ArbitraryType) uintptr
67126712
func Offsetof(selector ArbitraryType) uintptr
67136713
func Sizeof(variable ArbitraryType) uintptr
6714+
6715+
type IntegerType int // shorthand for an integer type; it is not a real type
6716+
func Add(ptr Pointer, len IntegerType) Pointer
6717+
func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType
67146718
</pre>
67156719

67166720
<p>
@@ -6767,6 +6771,32 @@ <h3 id="Package_unsafe">Package <code>unsafe</code></h3>
67676771
<code>Sizeof</code> are compile-time constant expressions of type <code>uintptr</code>.
67686772
</p>
67696773

6774+
<p>
6775+
The function <code>Add</code> adds <code>len</code> to <code>ptr</code>
6776+
and returns the updated pointer <code>unsafe.Pointer(uintptr(ptr) + uintptr(len))</code>.
6777+
The <code>len</code> argument must be of integer type or an untyped <a href="#Constants">constant</a>.
6778+
A constant <code>len</code> argument must be <a href="#Representability">representable</a> by a value of type <code>int</code>;
6779+
if it is an untyped constant it is given type <code>int</code>.
6780+
The rules for <a href="/pkg/unsafe#Pointer">valid uses</a> of <code>Pointer</code> still apply.
6781+
</p>
6782+
6783+
<p>
6784+
The function <code>Slice</code> returns a slice whose underlying array starts at <code>ptr</code>
6785+
and whose length and capacity are <code>len</code>:
6786+
</p>
6787+
6788+
<pre>
6789+
(*[len]ArbitraryType)(unsafe.Pointer(ptr))[:]
6790+
</pre>
6791+
6792+
<p>
6793+
The <code>len</code> argument must be of integer type or an untyped <a href="#Constants">constant</a>.
6794+
A constant <code>len</code> argument must be non-negative and <a href="#Representability">representable</a> by a value of type <code>int</code>;
6795+
if it is an untyped constant it is given type <code>int</code>.
6796+
If <code>ptr</code> is <code>nil</code> or <code>len</code> is negative at run time,
6797+
a <a href="#Run_time_panics">run-time panic</a> occurs.
6798+
</p>
6799+
67706800
<h3 id="Size_and_alignment_guarantees">Size and alignment guarantees</h3>
67716801

67726802
<p>

src/unsafe/unsafe.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ package unsafe
1414
// part of the unsafe package. It represents the type of an arbitrary Go expression.
1515
type ArbitraryType int
1616

17+
// IntegerType is here for the purposes of documentation only and is not actually
18+
// part of the unsafe package. It represents any arbitrary integer type.
19+
type IntegerType int
20+
1721
// Pointer represents a pointer to an arbitrary type. There are four special operations
1822
// available for type Pointer that are not available for other types:
1923
// - A pointer value of any type can be converted to a Pointer.
@@ -203,3 +207,22 @@ func Offsetof(x ArbitraryType) uintptr
203207
// value returned by reflect.TypeOf(s.f).FieldAlign().
204208
// The return value of Alignof is a Go constant.
205209
func Alignof(x ArbitraryType) uintptr
210+
211+
// The function Add adds len to ptr and returns the updated pointer
212+
// Pointer(uintptr(ptr) + uintptr(len)).
213+
// The len argument must be of integer type or an untyped constant.
214+
// A constant len argument must be representable by a value of type int;
215+
// if it is an untyped constant it is given type int.
216+
// The rules for valid uses of Pointer still apply.
217+
func Add(ptr Pointer, len IntegerType) Pointer
218+
219+
// The function Slice returns a slice whose underlying array starts at ptr
220+
// and whose length and capacity are len:
221+
//
222+
// (*[len]ArbitraryType)(unsafe.Pointer(ptr))[:]
223+
//
224+
// The len argument must be of integer type or an untyped constant.
225+
// A constant len argument must be non-negative and representable by a value of type int;
226+
// if it is an untyped constant it is given type int.
227+
// If ptr is nil or len is negative at run time, a run-time panic occurs.
228+
func Slice(ptr *ArbitraryType, len IntegerType) []ArbitraryType

0 commit comments

Comments
 (0)