code: func (c *Ctrie) snapshot(readOnly bool) *Ctrie {
  	if readOnly && c.readOnly {
  		return c
  	}
  	for {
  		root := c.readRoot()
  		main := gcasRead(root, c)
  		if c.rdcssRoot(root, main, root.copyToGen(&generation{}, c)) {
  			if readOnly {
  				// For a read-only snapshot, we can share the old generation
  				// root.
  				return newCtrie(root, c.hashFactory, readOnly)
  			}
  			// For a read-write snapshot, we need to take a copy of the root
  			// in the new generation.
  			return newCtrie(c.readRoot().copyToGen(&generation{}, c), c.hashFactory, readOnly)
  		}
  	}
  }
docs: // snapshot wraps up the CAS logic to make a snapshot or a read-only snapshot.