Skip to content

Instantly share code, notes, and snippets.

extension BinaryInteger {
subscript(bit index: Int) -> Bool {
get {
((self >> index) & Self(1)) == 1
}
set {
if newValue {
self |= (Self(1) << index)
} else {
self &= ~(Self(1) << index)

I recently started a private Minecraft server on my Linode. I was dismayed to discover that it caused the CPU graph on the Dashboard to spike to 100% despite the CPU usage as reported by top being only around 40-50% when a couple of players were present and around 10% when idle. I spent a lot of time worrying about this and even opened a support ticket to make sure I wasn't doing anything terribly wrong. Support pointed me to a post that discussed the same issue. The conclusion appears to be that it's a bug either in Minecraft or Java and it is confusing the reporting but isn't a real problem.

Despite this, I decided I didn't really like seeing the CPU graph maxed out all of the time, nor did I like the idea that Minecraft sits there consuming 10% of the CPU even when idle. While looking for solutions I ran across a b

@BigZaphod
BigZaphod / ContentView.swift
Last active July 1, 2024 04:31
Moving views between stacks with SwiftUI while preserving view identity
// This is an experiment for moving a view between different stacks and having SwiftUI animate it properly.
// By "drawing" all of the cards in one place and moving their geometry, it preserves the card view's
// identifity from SwiftUI's POV. This means when things change, SwiftUI can understand how they changed
// and animate it properly. Is there a better way to do this?
class Card : Identifiable, ObservableObject, Equatable {
@Published var name: String
@Published var tagged = false
init(_ name: String) {
@propertyWrapper
struct Wrapper<T> {
var actualValue: T!
var wrappedValue: T {
get {
actualValue
}
set {
actualValue = newValue
-(BOOL)containsActiveAutoRenewableSubscriptionOfProductIdentifier:(NSString *)productIdentifier forDate:(NSDate *)date
{
// IMPORTANT NOTE!
//
// I, Sean, have modified this from the original algo that came with this library.
//
// The original implementation would get the newest purchase record by sorting based on the subscriptionExpirationDate.
// Then it would check /only/ that one newest purchase record to see if the date was within the purchase record's range.
//
// This was VERY WRONG because it made multiple bad assumptions - one is that it assumed the incoming date was always
  • Identity being tied to the instance you join is very limiting. While a lot of instances have public feeds you could at least browse without logging in, in theory, many don't and require you to have an account. So if you wanted to get that "small town" feel in one small instance, you have to actually make an account and join it to do so. That's cumbersome and feels very unnecessary. I think identity could be sharable between instances in some fashion. Imagine you could join instance X and then click a join button on instance Y that takes your X identity and links it over to Y in much the same way that remote follows work. Perhaps as part of this process you could even optionally take on a new local name on the Y instance if you wanted and appear as if you are a truly local user, but under the hood there'd be a linkage between the two identities creating a kind of web of trust (if that's the right term). Thus you could someday later join instance Z using either your X or Y identity but doing so would link all
//
// A Swift property wrapper for adding "indirect" to struct properties.
// Enum supports this out of the box, but for some reason struct doesn't.
//
// This is useful when you want to do something recursive with structs like:
//
// struct Node {
// var next: Node?
// }
//
//
// A simplified implementation of https://gist.github.com/sturdysturge/e5163a9e95826adbeff9824d5aa1d111
// Which has an associated article here: https://betterprogramming.pub/build-a-secure-swiftui-property-wrapper-for-the-keychain-e0f8e39d554b
// Requires the Keychain Access package: https://github.com/kishikawakatsumi/KeychainAccess
//
import SwiftUI
import KeychainAccess
@propertyWrapper
//
// View+Assign.swift
// Created by Sean on 7/29/22.
//
import SwiftUI
// You cannot safely assign to a state variable during view update - such as inside the block of a GeometryReader.
// Rather than do an unsafe hack like DispatchQueue.main.async or resorting to a PreferenceKey or even Combine, we
// can simply defer the assignment to a time when it *is* safe to update State- such as inside of the task block!
// It is absolutely insane that velocity isn't public.
// INSANE.
// I don't get it.
// So anyway, this works but is kind of horrible.
// It's probably pretty safe to use and it should fallback to 0 if something goes wrong.
// Maybe.
extension DragGesture.Value {
var secretVelocity: CGSize {
let mirror = Mirror(reflecting: self)
guard let velocity = mirror.children.first(where: { $0.label == "velocity" }) else {