Huh. In my world, both real and imagined, "watching like a hawk" and fighting effectively against other enemies are mutually exclusive. If you literally never take your eye off the barrel, I'm going to give you Disadvantage on your attacks, and Advantage on attacks against you.
D&D doesn't exactly have fine-grained attention rules, but it seems reasonable to say that in the time it takes a halfling to scurry 5 ft., a guard can reasonably turn its head to look in that direction (and thus a reasonable assumption that the guard is generally peeking). It's a simplification, but a reasonable one, I feel, especially when you allow actions to manipulate that (ie, "I make a Sleight of Hand check to feint and draw his attention away from the barrel" is AWESOME in my book!)
One interesting point of comparison might be the petrifying gaze of a basilisk or a medusa - "averting your eyes" here is something you need to explicitly declare, and something that means you can't see the basilisk/medusa. The default assumption is "if you see each other, you lock gazes." Even if you're fighting some skeletons a few feet away, you're not considered to be "averting your eyes" from these creatures unless you specify that.
The scenario I thought you were going to describe was the guard watching the barrel without any other distractions. (Or, for example, if you're willing to take the Disadvantage penalty described above.) This is a tricky one.
If the guard actually approaches the barrel to stab the little bugger, you could argue that he's got to either go one way or the other around it, and that's when the rogue slips away.
Or he could just kick the barrel backwards and see what scurries out. IMC, I'd probably rule that this was essentially destroying cover - attack the object, deal damage, and if the barrel breaks, you see what is behind it (and if you have more attacks, you can attack whatever is there). Might not even HAVE TO destroy it (use an attack to "grapple" the barrel and then chuck it away, same ultimate effect).
If the guard was going to treat the barrel as an immovable object, I'd say he'd need to use his action to make a Perception check to see if he can beat your last Stealth roll. If he wins, he can't see you (the barrel's in the way still), but he knows you're back there. If he loses, all he knows is that he last saw you run back there.
But what if he just stays 20' and watches the barrel? The rogue is Hidden, so he can stealth, right? The sidebar on Hiding is specifically about Hiding, about moving from the Seen to the Not-Seen state, not about using Stealth. And our rogue is already Not-Seen.
Yeah, that'd be fine. The rogue would need to keep using the Hide action (and re-rolling Stealth every turn), but he could do that as often as he wanted if he stayed in the same place (and, as a rogue, Cunning Action means that he could do it as a bonus action and use his normal action to ranged attack or even melee attack something next to the barrel - getting advantage on the roll, and thus sneak attack damage to boot, or alternately using his normal action to make the distraction - throwing a pebble using Sleight of Hand - that allows him to move without being seen).
On the other hand, it seems rather...improbable...that the rogue can actually sneak away from the barrel while the guard is watching it.
What I'd probably rule is that the Rogue gets Disadvantage on his Stealth, and that if he succeeds it means he somehow managed to trick the guard...the old throwing a pebble trick or Jedi Mind Trick or something. Or maybe have him first roll Deception against the guard's Intelligence, and if he succeeds then roll Stealth with Disadvantage. Are those odds "realistic"? Not terribly...but it happens all the freaking time in fiction that we all love, so I'll allow somebody who invests in Stealth to try it, too.
I mean, what's the problem? So he gets a Sneak Attack on the hapless NPC guard and the hero wins against all odds. Isn't that why we play D&D and read terrible Forgotten Realms novels?
I think your ruling is fair, though it actually seems a bit harsher than my ruling on the stealth-er! "You can do it but with Disadvantage" is a much bigger failure chance than "You know you can't do it unless you or someone else makes a distraction!"
I think it comes down to the DM's feelings about stealth and rogues. If it's a trope that you like in your fiction you'll lean toward the more fictional interpretation. If you're sick of the rules-lawyering little buggers you're probably going to be more boringly "realistic".
The goal with my ruling is to make the choice of Stealth an
interesting decision. Part of what that means is that I want a rogue player
paying attention to where cover and concealment are, and using those as an actual sneaking person would - knowing that movement into a place where you can be seen means breaking stealth. So then it becomes a question of "Okay,
how can I not be seen in this open space?" and that becomes an interesting use of tactics and strategy that depend on the environment and party synergy to pull off.
My ruling only comes up if you're moving between points of cover (or maybe out from behind cover to stab a chump), and in that case, it means you're seen as you move between them unless you somehow distract the enemy. This lets you do the
Last of Us thing of hucking a bottle across the room to get their attention off of you (Sleight of Hand vs. Perception) if you're a rogue, or gets your party involved (Deception vs. Insight to get the enemy's attention for a few moments), or means spending resources on
darkness or
invisibility and all of those sound like more interesting decision points to me than "you just do it" is. "You might do it with Disadvantage" doesn't encourage interacting with the world's particulars much, either.
Which isn't to say it's bad or unfair, it just doesn't make the particular details of the scene quite as relevant, and I like it when a player/party has to be creative in the moment!