I'm not a really good swimmer. While swimming casually, when I get a cramp in my thighs or toes, it definitely makes me panic a bit and hampers my swimming in a bad way.
What should I be doing if I get a cramp when I am swimming?
A survival float-also know as a deadman float or jellyfish float–is when you relax your body, over-inflate your lungs, and try to stay afloat using natural buoyancy by either laying out on your back or your front. On your front you obviously need to come up for air regularly, but on your back you can relax and just focus on your breathing and relaxing the cramp. I know people that are so buoyant they can actually fall asleep and take naps floating on their backs on lakes.
A couple of summers ago I agreed to swim across a lake with my much more fit cousin. I tried keeping up with her for the first bit, but just past half way across I got a wicked cramp in my side. I was in the very middle of a lake so I had no choice but to keep on going. How I coped with the cramp was to first change my stroke (freestyle, to breast stroke, to side stroke, to back stroke). Eventually I had to stop kicking my legs and focus as hard as I could at relaxing the muscles where I was cramping. When the cramp got real bad, I ended up on my back in a survival float using gentle figure eight wrist motions to keep me moving towards shore.
@Sherwood Botsford shared a very specific survival floating technique in the comments called drownproofing. It's very similar to a deadman float except you are vertical and under the surface of the water, only using very gentle motions to just bring your mouth to the surface every 10-12 seconds for a new breathe of air.
Cramp in feet/legs is usually pretty easy to fix by stretching. You can do this by pulling up your toes with your hands. This is a technique I was taught during my PADI scuba diving course and is a standard part of the course. It's easier when wearing fins of course, because then you can just pull the fin tip upwards :)
I don't want to link to any specific site but googling 'scuba diving cramp removal' would do the job.